解決Python paramiko 模塊遠(yuǎn)程執(zhí)行ssh 命令 nohup 不生效的問題
Python - paramiko 模塊遠(yuǎn)程執(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()
因?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 不生效的問題的文章就介紹到這了,更多相關(guān)Python paramiko 模塊遠(yuǎn)程執(zhí)行ssh 命令 nohup 不生效內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python安裝并操作redis實(shí)現(xiàn)流程詳解2. 網(wǎng)頁中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)3. 部署vue+Springboot前后端分離項(xiàng)目的步驟實(shí)現(xiàn)4. Python常用擴(kuò)展插件使用教程解析5. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟6. AspNetCore&MassTransit Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過程7. vue組件庫的在線主題編輯器的實(shí)現(xiàn)思路8. ajax post下載flask文件流以及中文文件名問題9. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】10. PHP字符串前后字符或空格刪除方法介紹

網(wǎng)公網(wǎng)安備