全部博文(921)
分类: Python/Ruby
2013-08-16 23:09:16
What I've tried:
To my mind it would be much easier and secure to create a script which has sudoer rights.
For example, add this to sudoers:
myuser ALL=NOPASSWD:/home/myuser/somescript.sh
Python Remote SSH with Paramiko
I am using Paramiko to do some remote ssh work and could not figure out how to change directories and execute a script with the SSHClient.execute_command() function. I finally figured out that .execute_command() is basically a single session, so doing a .execute_command('cd scripts') and then executing the script with another .execute_command() reverts back to your default directory. The alternatives are to send all the commands at once separated by a ; .execute_command('cd scripts; ./myscript.sh'), or to use the .interactive() shell support. Since I only needed to fire off this script I used the first solution.
意思就是execute_command() 他是a single session,每次执行完后都要回到缺省目录。所以可以 .execute_command('cd /var; pwd')
或者使用aa = ssh.invoke_shell()
aa.send('cd /var\n')