[英]Execute (sub)commands in secondary shell/command on SSH server in Paramiko
[英]Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko
我在使用 ShoreTel 語音開關時遇到問題,我正在嘗試使用 Paramiko 跳進去並運行幾個命令。 我認為問題可能是 ShoreTel CLI 給出的提示與標准 Linux $
不同。 它看起來像這樣:
server1$:stcli
Mitel>gotoshell
CLI> (This is where I need to enter 'hapi_debug=1')
Python 仍然期待$
,還是我錯過了其他東西?
我認為這可能是一個時間問題,所以我把那些time.sleep(1)
放在命令之間。 好像還是不接。
import paramiko
import time
keyfile = "****"
User = "***"
ip = "****"
command1 = "stcli"
command2 = "gotoshell"
command4 = "hapi_debug=1"
ssh = paramiko.SSHClient()
print('paramikoing...')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = ip, username = User, key_filename = keyfile)
print('giving er a go...')
ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command(command1)
time.sleep(1)
stdin, stdout, stderr = ssh.exec_command(command2)
time.sleep(1)
stdin, stdout, stderr = ssh.exec_command(command4)
time.sleep(1)
print(stdout.read())
ssh.close()
print("complete")
我對成功執行此代碼的期望是, hapi_debug
級別為 1。這意味着當我 SSH 進入該事物時,我會看到那些 HAPI 調試填充。 當我這樣做時,我看不到那些調試。
我假設gotoshell
和hapi_debug=1
不是頂級命令,而是stcli
的子命令。 換句話說, stcli
有點像 shell。
在這種情況下,您需要將要在 subshell 中執行的命令寫入其stdin
:
stdin, stdout, stderr = ssh.exec_command('stcli')
stdin.write('gotoshell\n')
stdin.write('hapi_debug=1\n')
stdin.flush()
如果你之后調用stdout.read
,它會等到命令stcli
完成。 它從不做什么。 如果您想繼續閱讀 output,您需要發送終止子 shell 的命令(通常為exit\n
)。
stdin.write('exit\n')
stdin.flush()
print(stdout.read())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.