最近在用Paramiko 开发一款远程测试调试框架,结果发现目录怎么都无法切换,查了下原因,在http://bbs.chinaunix.net/thread-1675446-1-1.html看到了
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 ') aa.recv(100)
所以解决办法就是目录连在一起写,用;隔开,或者用invoke_shell方法
在一些命令会有多行输出时,建议也使用invoke_shell,否则会不出结果