gpt4 book ai didi

python - 等待 paramiko exec_command 完成

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:47 49 4
gpt4 key购买 nike

我有一个 python 脚本,我正在尝试安装 rpm 包,但是当我发送安装命令时,它不会等待命令完成就重新启动服务。我读过很多关于使用 recv_exit_status() 的论坛,但我认为我没有正确使用它。

这就是我所拥有的:

#!/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()

我尝试过使用 Fabric,但我似乎在某个地方弄乱了我的语法。

最佳答案

add get_pty=True 这将等到命令执行完成。 stdin,stdout,stderr = self.ssh.exec_command(command,get_pty=True)

关于python - 等待 paramiko exec_command 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47230240/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com