简体   繁体   中英

Running a python script on a remote machine through ssh

The remote machine has Cygwin installed and I have done

$echo "PATH=\$PATH:/cygdrive/c/Python27" >> .bash_profile
then, source .bash_profile (after doing this I am able to run a Python script from cygwin terminal).

Now, from Pyscripter installed in my laptop, I am trying to run hello_world in the remote machine through paramiko:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('135.24.237.167',username = 'cyg_server',password = 'force')

stdin,stdout,stderr = ssh.exec_command("/cygdrive/c/Python27/python /cygdrive/c/cygwin64/home/hello_world.py")

##But I get the following error:

stderr.readlines()

[u"C:\\Python27\\python.exe: can't open file '/cygdrive/c/cygwin64/home/hello_world.py': [Errno 2] No such file or directory\r\n"]

Please help.

Paramiko is too raw, as I believe. Try using fabric.

Sample code would be:

from fabric.api import *

env.key_filename = /path/to/your/pem/file

def mem_usage():
   run('free -m')

execute(mem_usage, host="user@IP_or_hostname")

Or if you do not have a pem file, you can leave that line and just enter the password when prompted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM