我使用paramiko建立ssh會話併發送命令到服務器。Python ssh paramiko檢測失敗的命令
很少的命令沒有成功執行。我如何檢測那些命令無法執行並終止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調用這裏。
請幫助如何檢測不成功的ran命令並終止python程序。要做到這一點
的paramiko不能告訴你,如果命令失敗與否。你必須自己解析輸出。 – pynexj
謝謝我會做同樣的方式 – asteroid4u