解決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 不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)PDF轉(zhuǎn)MP3的示例代碼
我們平??吹胶芏辔募际荘DF格式,網(wǎng)上的各類書籍多為此格式。有時候不方便閱讀,或者怕費眼睛傷頸椎,那么有沒有一種方法可以把它變?yōu)橐纛l,本文就來和大家詳細講講2023-05-05
Dephi逆向工具Dede導出函數(shù)名MAP導入到IDA中的實現(xiàn)方法
這篇文章主要介紹了Dephi逆向工具Dede導出函數(shù)名MAP導入到IDA中,通過這個腳本,我們就可以把專業(yè)dephi程序分析的結(jié)果,轉(zhuǎn)移到IDA專業(yè)逆向代碼分析的平臺,實現(xiàn)聯(lián)動,需要的朋友可以參考下2022-08-08
python爬蟲爬取股票的北上資金持倉數(shù)據(jù)
這篇文章主要介紹了python爬蟲爬取股票的北上資金持倉數(shù)據(jù),文章基于python的相關資料展開爬取數(shù)據(jù)的詳細內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05
詳談python read readline readlines的區(qū)別
下面小編就為大家?guī)硪黄斦刾ython read readline readlines的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

