0

subprocess.popen を介して、ホストから一部のクライアントに Python スクリプトを実行しようとしています。このコマンドは一種のファイアアンドフォーゲットであり、クライアントのプロセスは、強制終了するまで無制限に実行する必要があります。問題は、この行を Python で実行すると、プロセスがクライアントで 1 時間実行され、1 時間 2 分後に突然停止することです。

subprocess.Popen(["rsh {} {} {}".format(ipClient,command,args)], shell=True)

ここで、「コマンド」はクライアントのパスとコマンドです。シェルで実行するだけ rsh 'ip' 'command' 'args' で、期待どおりに動作し、突然停止しません。

何か案が?

4

1 に答える 1

0

アクセスをsubprocess.Popenラップする場合はうまくいくかもしれませんがssh、これは推奨される方法ではありません。

paramikoの使用をお勧めします。

import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(server, username=user,password=password)
...
ssh_client.close()

また、ユーザーが入力しているかのように端末をシミュレートする場合は、次のようにします。

chan=self.ssh_client.invoke_shell()

def exec_cmd(cmd):
    """Gets ssh command(s), execute them, and returns the output"""
    prompt='bash $' # the command line prompt in the ssh terminal
    buff=''
    chan.send(str(cmd)+'\n')
    while not chan.recv_ready():
        time.sleep(1)
    while not buff.endswith(prompt):
        buff+=self.chan.recv(1024)
    return buff[:len(prompt)]

使用例:exec_cmd('pwd')

事前にプロンプ​​トがわからない場合は、次のように設定できます。

chan.send('PS1="python-ssh:"\n')
于 2015-08-25T07:11:33.743 に答える