2017-11-10 31 views
0

私はrpmパッケージをインストールしようとしているpythonスクリプトを持っていますが、インストールするコマンドを送信するときに、コマンドを終了してからサービスを再起動するまで待つことはありません。私は "recv_exit_status()"の使用に関する多くのフォーラムを読んだが、私はそれを正しく使用しているとは思わない。paramiko exec_commandが終了するまで待つ

これは私が持っているものです。

#!/usr/bin/python 

import paramiko, os 
from getpass import getpass 

# Setting Variables 
Hosts = [ '192.168.1.1', '192.168.1.2'] #IPs changed for posting 
username = 'root' 
print 'Enter root password on remote computer:' 
password = getpass() 
port = 22 
File = 'Nessus-6.11.2-es7.x86_64.rpm' 

for host in Hosts: 
    print 'Finished copying files. Now executing on remote computer' 

    #Setting up SSH session to run commands 
    remote_client = paramiko.SSHClient() 
    remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    remote_client.connect(host, username=username, password=password) 

    InstallNessus = 'rpm -U --percent %s'%File 
    stdin, stdout, stderr = remote_client.exec_command(InstallNessus) 
    stdout.channel.recv_exit_status() 
    lines = stdout.readlines() 
    for line in lines: 
     print line 
    stdin, stdout, stderr = remote_client.exec_command('systemctl restart nessusd.service') 

    remote_client.close() 
+0

私はFabricを使用しようとしましたが、私はどこかで構文がうんざりしているようです。 –

答えて

0

それはchannel.recv_exit_status()、ないstdout.channel.recv_exit_status()です。

しかし、多くのサーバーで同じコマンドを実行しようとしているので、parallel-sshのようなものはparamikoよりもはるかに速く、より速く適合します。それを行うには

コードだけでも、はるかに簡単です:

from pssh.pssh2_client import ParallelSSHClient 

hosts = ['192.168.1.1', '192.168.1.2'] 
_file = 'Nessus-6.11.2-es7.x86_64.rpm' 
cmd = 'rpm -U --percent %s' % _file 

client = ParallelSSHClient(hosts, user='<user>', password='<password>') 

output = client.run_command(cmd) 
for host, host_output in output.items(): 
    for line in host_output.stdout: 
     print "Host %s: %s" % (host, line) 
    print "Host %s exit code %s" % (host, host_output.exit_code) 

restart_out = client.run_command('systemctl restart nessusd.service') 
# Just wait for completion 
client.join(restart_out) 

詳細についてdocumentationを参照してください。

+0

投稿したものを使用していますが、同じことをしているようです。スクリプトはrpmのインストールが完了するのを待っていないようです。私は "ホスト192.168.1.1終了コード0"の出力を取得するだけです。 私はドキュメントを見ていくつもりです。 –

+0

まずコマンドをテストしてください。出力がない場合、RPMが既にインストールされている場合など、何も実行しないでコマンドを実行したり終了したりすることはできません。 – danny

+0

私はそんな奴だ。私はちょうど私のファイル変数に完全なパスを入れていないことに気づいた。私はそれが私のスクリプトのすべての問題だと思う。 –

関連する問題