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