解决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 不生效内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. 解决VUE项目使用Element-ui 下拉组件的验证失效问题2. 如何基于python3和Vue实现AES数据加密3. Docker暴露2375端口导致服务器被攻击问题及解决方法4. CSS3实例分享之多重背景的实现(Multiple backgrounds)5. uni-app结合PHP实现单用户登陆demo及解析6. 解决VUE 在IE下出现ReferenceError: Promise未定义的问题7. Vue+express+Socket实现聊天功能8. 匹配模式 - XSL教程 - 49. AspNetCore&MassTransit Courier实现分布式事务的详细过程10. 数组在java中的扩容的实例方法

网公网安备