Linux 如何通过ssh连接执行python或bash脚本并获取返回代码
我在\tmp\位置有一个python文件,该文件打印一些内容并返回退出代码22。我可以用putty完美地运行这个脚本,但不能用paramiko模块 这是我的执行代码Linux 如何通过ssh连接执行python或bash脚本并获取返回代码,linux,bash,python-2.7,ssh,paramiko,Linux,Bash,Python 2.7,Ssh,Paramiko,我在\tmp\位置有一个python文件,该文件打印一些内容并返回退出代码22。我可以用putty完美地运行这个脚本,但不能用paramiko模块 这是我的执行代码 import paramiko def main(): remote_ip = '172.xxx.xxx.xxx' remote_username = 'root' remote_password = 'xxxxxxx' remote_path = '/tmp/ab.py' sub_t
import paramiko
def main():
remote_ip = '172.xxx.xxx.xxx'
remote_username = 'root'
remote_password = 'xxxxxxx'
remote_path = '/tmp/ab.py'
sub_type = 'py'
commands = ['echo $?']
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(remote_ip, username=remote_username,password=remote_password)
i,o,e = ssh_client.exec_command('/usr/bin/python /tmp/ab.py')
print o.read(), e.read()
i,o,e = ssh_client.exec_command('echo $?')
print o.read(), e.read()
main()
这是我要在远程机器上执行的python脚本
#!/usr/bin/python
import sys
print "hello world"
sys.exit(20)
我无法理解我的逻辑到底错在哪里。另外,当我执行cd\tmp和ls时,我仍然会在根文件夹中 每次运行exec_命令时,都会启动一个新的bash子进程 这就是为什么当你运行类似于:
exec_command("cd /tmp");
exec_command("mkdir hello");
dir“hello”是在dir中创建的,而不是在tmp中
尝试在同一exec_命令调用中运行多个命令
另一种方法是使用python的每次运行exec_命令时,都会启动一个新的bash子进程 这就是为什么当你运行类似于:
exec_command("cd /tmp");
exec_command("mkdir hello");
dir“hello”是在dir中创建的,而不是在tmp中
尝试在同一exec_命令调用中运行多个命令
另一种方法是使用python的每次运行exec_命令时,都会启动一个新的bash子进程 这就是为什么当你运行类似于:
exec_command("cd /tmp");
exec_command("mkdir hello");
dir“hello”是在dir中创建的,而不是在tmp中
尝试在同一exec_命令调用中运行多个命令
另一种方法是使用python的每次运行exec_命令时,都会启动一个新的bash子进程 这就是为什么当你运行类似于:
exec_command("cd /tmp");
exec_command("mkdir hello");
dir“hello”是在dir中创建的,而不是在tmp中
尝试在同一exec_命令调用中运行多个命令
另一种方法是使用python的以下示例通过ssh运行命令,然后获取命令stdout、stderr和返回代码:
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='hostname', username='username', password='password')
channel = client.get_transport().open_session()
command = "import sys; sys.stdout.write('stdout message'); sys.stderr.write(\'stderr message\'); sys.exit(22)"
channel.exec_command('/usr/bin/python -c "%s"' % command)
channel.shutdown_write()
stdout = channel.makefile().read()
stderr = channel.makefile_stderr().read()
exit_code = channel.recv_exit_status()
channel.close()
client.close()
print 'stdout:', stdout
print 'stderr:', stderr
print 'exit_code:', exit_code
希望对您有所帮助以下示例通过ssh运行命令,然后获取命令stdout、stderr和返回代码:
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='hostname', username='username', password='password')
channel = client.get_transport().open_session()
command = "import sys; sys.stdout.write('stdout message'); sys.stderr.write(\'stderr message\'); sys.exit(22)"
channel.exec_command('/usr/bin/python -c "%s"' % command)
channel.shutdown_write()
stdout = channel.makefile().read()
stderr = channel.makefile_stderr().read()
exit_code = channel.recv_exit_status()
channel.close()
client.close()
print 'stdout:', stdout
print 'stderr:', stderr
print 'exit_code:', exit_code
希望对您有所帮助以下示例通过ssh运行命令,然后获取命令stdout、stderr和返回代码:
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='hostname', username='username', password='password')
channel = client.get_transport().open_session()
command = "import sys; sys.stdout.write('stdout message'); sys.stderr.write(\'stderr message\'); sys.exit(22)"
channel.exec_command('/usr/bin/python -c "%s"' % command)
channel.shutdown_write()
stdout = channel.makefile().read()
stderr = channel.makefile_stderr().read()
exit_code = channel.recv_exit_status()
channel.close()
client.close()
print 'stdout:', stdout
print 'stderr:', stderr
print 'exit_code:', exit_code
希望对您有所帮助以下示例通过ssh运行命令,然后获取命令stdout、stderr和返回代码:
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='hostname', username='username', password='password')
channel = client.get_transport().open_session()
command = "import sys; sys.stdout.write('stdout message'); sys.stderr.write(\'stderr message\'); sys.exit(22)"
channel.exec_command('/usr/bin/python -c "%s"' % command)
channel.shutdown_write()
stdout = channel.makefile().read()
stderr = channel.makefile_stderr().read()
exit_code = channel.recv_exit_status()
channel.close()
client.close()
print 'stdout:', stdout
print 'stderr:', stderr
print 'exit_code:', exit_code
希望它有帮助好吧,我知道EXECèU命令不能帮助我完成我要做的事情,但为什么我的python/批处理脚本没有运行?另外,如果我能够这样做,我将如何获得scriptok的退出代码或返回代码?我知道EXECC_命令不会帮助我完成我试图做的事情,但为什么我的python/批处理脚本没有运行?另外,如果我能够这样做,我将如何获得scriptok的退出代码或返回代码?我知道EXECC_命令不会帮助我完成我试图做的事情,但为什么我的python/批处理脚本没有运行?另外,如果我能够这样做,我将如何获得scriptok的退出代码或返回代码?我知道EXECC_命令不会帮助我完成我试图做的事情,但为什么我的python/批处理脚本没有运行?另外,如果我能这样做,我将如何获得脚本的退出代码或返回代码谢谢你的想法。我通过使用管道将python脚本的输出重定向到一个文件,然后在下一个命令中读取脚本来完成这一点。谢谢你的想法。我通过使用管道将python脚本的输出重定向到一个文件,然后在下一个命令中读取脚本来完成这一点。谢谢你的想法。我通过使用管道将python脚本的输出重定向到一个文件,然后在下一个命令中读取脚本来完成这一点。谢谢你的想法。我已经通过使用管道将python脚本的输出重定向到一个文件,然后在下一个命令中读取脚本来实现这一点。