解決Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題
Python - paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題解決
1、使用 paramiko 模塊ssh 登陸到 linux 執(zhí)行nohup命令不生效
# 執(zhí)行命令 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()
因為執(zhí)行完畢后,shell 會立即關閉通道
2、稍作修改,使用 invoke_shell
# 執(zhí)行命令 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) # 等待命令執(zhí)行完成 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 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題的文章就介紹到這了,更多相關Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
pymssql數(shù)據(jù)庫操作MSSQL2005實例分析
這篇文章主要介紹了pymssql數(shù)據(jù)庫操作MSSQL2005的方法,可實現(xiàn)基本的連接、查詢、插入、更新及調用存儲過程等功能,非常具有實用價值,需要的朋友可以參考下2015-05-05Python進階之使用selenium爬取淘寶商品信息功能示例
這篇文章主要介紹了Python進階之使用selenium爬取淘寶商品信息功能,結合實例形式詳細分析了Python使用selenium與requests模塊爬取淘寶商品信息的相關操作技巧,需要的朋友可以參考下2019-09-09jupyter notebook更換皮膚主題的實現(xiàn)
這篇文章主要介紹了jupyter notebook更換皮膚主題的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實例
這篇文章主要介紹了Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實例,通過在一個表格中進行操作來論述如何使用Python快速安全地操作sqlite3,需要的朋友可以參考下2014-04-04詳解pandas如何去掉、過濾數(shù)據(jù)集中的某些值或者某些行?
這篇文章主要介紹了pandas如何去掉、過濾數(shù)據(jù)集中的某些值或者某些行?,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05PyCharm實現(xiàn)遠程調試的全過程(附圖文講解)
這篇文章主要介紹了PyCharm實現(xiàn)遠程調試的全過程,文中通過圖文結合的方式給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-05-05