【问题标题】:Python paramiko hangs when trying to run bash shell尝试运行 bash shell 时 Python paramiko 挂起
【发布时间】:2015-12-11 21:20:50
【问题描述】:

我需要在远程服务器上的 bash shell 中运行回归脚本。我能够使用 paramiko 成功连接并执行不同的命令。但是当我尝试执行“/bin/bash”时,我的 Python 脚本永远挂起:

stdin,stdout,stderr = ssh.exec_command("pwd;/bin/bash;echo $SHELL")

没有 /bin/bash echo $SHELL 效果很好并返回以下内容:

[u'/home/akar\n', u'/tools/cfr/bin/tcsh\n']

有什么解决方法吗?

【问题讨论】:

    标签: python bash python-2.7 python-3.x paramiko


    【解决方案1】:

    我的第一个疑问是您执行 bash 的目的是什么。字面意思是:

    pwd; #print '/home/akar\n' as it results
    /bin/bash; #/bin/bash will take over of console(stdin/stdout/stderr) from here
    echo $SHELL #when you input exit to exit the bash from line 2, line 3 will print 
    

    【讨论】:

    • 我需要执行的是这样的 bash 脚本:stdin,stdout,stderr = ssh.exec_command("cd /project/fcd_psv/akar/neptune_psv/fw;./Do_Regr.sh -i Testlist_Regression.in -m 135.21.235.177 -g");不做 /bin/bash 它不会运行
    • stdin,stdout,stderr = ssh.exec_command("cd /project/fcd_psv/akar/neptune_psv/fw;./Do_Regr.sh -i Testlist_Regression.in -m 135.21. 235.177-g"); ?您可以通过传递 sh 文件的路径来调用 /bin/bash。但是不要勉强调用/bin/bash;