【发布时间】:2017-10-04 15:54:50
【问题描述】:
我正在使用 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, 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程序。
【问题讨论】:
-
paramiko 无法告诉您命令是否失败。你必须自己解析输出。
-
谢谢我会这样做
标签: python linux shell ssh paramiko