python-3.x - 等到命令在 paramiko invoke_shell() 中完成
问题描述
我想等待给定的命令执行已在远程机器上完成。这种情况下它只是执行并返回,而不是等到它完成。
import paramiko
import re
import time
def scp_switch(host, username, PasswdValue):
ssh = paramiko.SSHClient()
try:
# Logging into remote host as my credentials
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=PasswdValue ,timeout=30)
try:
# switcing to powerbroker/root mode
command = "pbrun xyz -u root\n"
channel = ssh.invoke_shell()
channel.send(command)
time.sleep(3)
while not re.search('Password',str(channel.recv(9999), 'utf-8')):
time.sleep(1)
print('Waiting...')
channel.send("%s\n" % PasswdValue)
time.sleep(3)
#Executing the command on remote host with root (post logged as root)
# I dont have any specific keyword to search in given output hence I am not using while loop here.
cmd = "/tmp/slp.sh cool >/tmp/slp_log.txt \n"
print('Executing %s' %cmd)
channel.send(cmd) # its not waiting here till the process completed,
time.sleep(3)
res = str(channel.recv(1024), 'utf-8')
print(res)
print('process completed')
except Exception as e:
print('Error while switching:', str(e))
except Exception as e:
print('Error while SSH : %s' % (str(e)))
ssh.close()
""" Provide the host and credentials here """
HOST = 'abcd.us.domain.com'
username = 'heyboy'
password = 'passcode'
scp_switch(HOST, username, password)
根据我的研究,它不会返回任何状态码,是否有任何逻辑来获取返回码并等待进程完成?
解决方案
推荐阅读
- python - IDLE 识别的错误 python 安装/或 IDLE 不识别 Anaconda
- sql - 转换为实体框架 Lambda
- php - 创建事务控制器导致意外错误
- python - 如何获取熊猫数据框中不唯一且按行的项目数?
- javascript - 在鼠标悬停时停止 setTimeout 引导模式
- databricks - 如何使用 Databricks dbutils 从文件夹中删除所有文件
- sql - 如何使用 SQL 索引元素?
- postgresql-9.5 - 在 postgreSQL 中创建数据库链接
- flask - 使用 mod_wsgi 在 apache 上提供 Flask 应用程序来考虑文件路径的正确方法是什么
- python - 有没有办法重写函数 data1.groupby(level=0)['total_tax'],apply(lambda x: x.shift()) 以避免设置复制警告