Gar*_*son 3 python ssh paramiko shoretel
我的 ShoreTel 语音开关出现问题,我正在尝试使用 Paramiko 跳入其中并运行几个命令。我认为问题可能在于 ShoreTel CLI 给出的提示与标准 Linux 不同$。它看起来像这样:
server1$:stcli
Mitel>gotoshell
CLI> (This is where I need to enter 'hapi_debug=1')
Run Code Online (Sandbox Code Playgroud)
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")
Run Code Online (Sandbox Code Playgroud)
我期望成功执行此代码时级别为hapi_debug1。这意味着当我通过 SSH 连接到该事物时,我会看到那些 HAPI 调试正在填充。当我这样做时,我看不到那些调试。
我假设 和gotoshell不是hapi_debug=1顶级命令,而是stcli. 换句话说,它stcli是一种外壳。
在这种情况下,您需要将要在子 shell 中执行的命令写入其stdin:
stdin, stdout, stderr = ssh.exec_command('stcli')
stdin.write('gotoshell\n')
stdin.write('hapi_debug=1\n')
stdin.flush()
Run Code Online (Sandbox Code Playgroud)
如果您stdout.read稍后调用,它将等待命令stcli完成。它从来不做的事情。如果您想继续读取输出,您需要发送一个终止子 shell 的命令(通常为exit\n)。
stdin.write('exit\n')
stdin.flush()
print(stdout.read())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1998 次 |
| 最近记录: |