解決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. ThinkPHP5 通過ajax插入圖片并實時顯示(完整代碼)2. Express 框架中使用 EJS 模板引擎并結合 silly-datetime 庫進行日期格式化的實現方法3. 一篇文章帶你了解JavaScript-對象4. Java構建JDBC應用程序的實例操作5. javascript設計模式 ? 建造者模式原理與應用實例分析6. IntelliJ IDEA設置條件斷點的方法步驟7. Python使用oslo.vmware管理ESXI虛擬機的示例參考8. Jsp中request的3個基礎實踐9. 淺談SpringMVC jsp前臺獲取參數的方式 EL表達式10. Spring應用拋出NoUniqueBeanDefinitionException異常的解決方案
