execute local python script over sshClient() with Paramiko in remote machine
You cannot just execute python function through ssh. ssh is just a tunnel with your code on one side (client) and shell on another (server). You should execute shell commands on remote side.
If using raw ssh code is not critical, i suggest fabric
as library for writing administration tools. It contains tools for easy ssh handling, file transferring, sudo, parallel execution and other.
I think you might want change the paramaters you're passing into ssh.exec_command
Here's an idea:
Instead of doing:
def get_dir():
return ', '.join(os.listdir(os.getcwd()))
i, o, e = ssh.exec_command(getDir())
You might want to try:
i, o, e = ssh.exec_command('pwd')
o.printlines()
And other things to explore:
- Writing a bash script or a Python that lives on your servers. You can use Paramiko to log onto the server and executing the script with
ssh.exec_command(some_script.sh)
orssh.exec_command(some_script.py)
- Paramiko has some FTP/SFTP utilities so you can actually use it to put the script on the server and then execute it.