Python ssh paramiko检测失败的命令
我正在使用paramiko建立ssh会话并向服务器发送命令 很少有命令没有成功执行。如何检测无法执行和终止python代码的命令。 下面是我正在尝试的代码:Python ssh paramiko检测失败的命令,python,linux,shell,ssh,paramiko,Python,Linux,Shell,Ssh,Paramiko,我正在使用paramiko建立ssh会话并向服务器发送命令 很少有命令没有成功执行。如何检测无法执行和终止python代码的命令。 下面是我正在尝试的代码: remote_conn_pre = paramiko.SSHClient() remote_conn_pre.set_missing_host_key_policy( paramiko.AutoAddPolicy()) remote_conn_pre.connect(host, username=username, passwor
remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
remote_conn_pre.connect(host, username=username, password=password, look_for_keys=False, allow_agent=False)
print "SSH connection established to %s" % host
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
remote_conn.send("\n")
remote_conn.send("scope org engg\n")
remote_conn.send("\n")
remote_conn.send("show service-profile")
if remote_conn.recv_ready():
details = remote_conn.recv(5000)
remote_conn.close()
详细输出:
servera# scope org engg
Error: Managed object does not exist # org engg is not exist that the reason we are getting this error
servera#
servera# show service-profile
% Incomplete Command at '^' marker # since the above command is failed but paramiko does not able to identify it is moving to second command execution . There is no org engg so that the reason i am getting incomplete command warning.
注意:这不是一个shell,所以我必须在这里使用shell调用
请帮助检测未成功运行的命令并终止python程序 一种方法是:
- 在本地创建一个可以处理错误的小bash脚本
- 使用
在远程服务器中复制脚本scp
- 运行脚本并捕获其输出
- 分析输出以查看是否发生错误
#!/bin/bash
function error() {
local parent_line_no="$1"
local message="$2"
local code="${3:-1}"
if [[ -n "$message" ]] ; then
echo "Error on or near line ${parent_line_no}: ${message}; exiting with status ${code}"
else
echo "Error on or near line ${parent_line_no}; exiting with status ${code}"
fi
exit "${code}"
}
trap 'error ${LINENO}' ERR
# your commands here...
远程服务器不是基于unix/linux的服务器这会起作用吗?@asteroid4u:是的,这是一个bash脚本。OP的远程登录shell看起来不像bash。是的,远程服务器不是bash shell它的开关类型paramiko无法告诉您命令是否失败。您必须自己解析输出。谢谢,我也会这样做