解决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 不生效内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Python Collections强大的数据结构工具使用实例探索
这篇文章主要介绍了Python Collections强大的数据结构工具的使用实例探索,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2024-01-01python xml.etree.ElementTree遍历xml所有节点实例详解
这篇文章主要介绍了python xml.etree.ElementTree遍历xml所有节点实例详解的相关资料,这里附有实例代码,需要的朋友可以参考下2016-12-12以一个投票程序的实例来讲解Python的Django框架使用
这篇文章主要介绍了以一个投票程序的实例来讲解Python的Django框架使用,Django是Python世界中人气最高的MVC框架,需要的朋友可以参考下2016-02-02
最新评论