解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题
Python-paramiko模块远程执行ssh命令nohup不生效的问题解决
1、使用paramiko模块ssh登陆到linux执行nohup命令不生效
#执行命令 defcommand(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() ifresult_print: lines=read_unicode(result) forlineinlines: print(line) ssh.close()
因为执行完毕后,shell会立即关闭通道
2、稍作修改,使用invoke_shell
#执行命令 defcommand(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) ifnohup: cmd+='&\n' invoke=ssh.invoke_shell() invoke.send(cmd) #等待命令执行完成 time.sleep(2) else: stdin,stdout,stderr=ssh.exec_command(cmd) result=stdout.read() ifresult_print: lines=read_unicode(result) forlineinlines: print(line) ssh.close()
到此这篇关于解决Pythonparamiko模块远程执行ssh命令nohup不生效的问题的文章就介绍到这了,更多相关Pythonparamiko模块远程执行ssh命令nohup不生效内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短