解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题
更新时间:2020年07月14日 14:11:22 作者:简简单单OnlineZuozuo
这篇文章主要介绍了解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Python - paramiko 模块远程执行ssh 命令 nohup 不生效的问题解决
1、使用 paramiko 模块ssh 登陆到 linux 执行nohup命令不生效
# 执行命令
def command(ssh_config, cmd, result_print=None, nohup=False):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
password=ssh_config.password)
print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
if result_print:
lines = read_unicode(result)
for line in lines:
print(line)
ssh.close()
因为执行完毕后,shell 会立即关闭通道
2、稍作修改,使用 invoke_shell
# 执行命令
def command(ssh_config, cmd, result_print=None, nohup=False):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
password=ssh_config.password)
print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
if nohup:
cmd += ' & \n '
invoke = ssh.invoke_shell()
invoke.send(cmd)
# 等待命令执行完成
time.sleep(2)
else:
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
if result_print:
lines = read_unicode(result)
for line in lines:
print(line)
ssh.close()
到此这篇关于解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题的文章就介绍到这了,更多相关Python paramiko 模块远程执行ssh 命令 nohup 不生效内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Dephi逆向工具Dede导出函数名MAP导入到IDA中的实现方法
这篇文章主要介绍了Dephi逆向工具Dede导出函数名MAP导入到IDA中,通过这个脚本,我们就可以把专业dephi程序分析的结果,转移到IDA专业逆向代码分析的平台,实现联动,需要的朋友可以参考下2022-08-08
详谈python read readline readlines的区别
下面小编就为大家带来一篇详谈python read readline readlines的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-09-09


最新评论