在Python Paramiko中的SSH服务器上的secondary shell/command中执行(sub)命令
我有一个ShoreTel语音开关的问题,我正试图使用Paramiko跳入它并运行几个命令。我认为问题可能在于,ShoreTel CLI提供的提示与标准Linux在Python Paramiko中的SSH服务器上的secondary shell/command中执行(sub)命令,python,ssh,paramiko,shoretel,Python,Ssh,Paramiko,Shoretel,我有一个ShoreTel语音开关的问题,我正试图使用Paramiko跳入它并运行几个命令。我认为问题可能在于,ShoreTel CLI提供的提示与标准Linux$不同。它看起来是这样的: server1$:stcli Mitel>gotoshell CLI> (This is where I need to enter 'hapi_debug=1') Python是否仍然期望$,或者我遗漏了什么 我想这可能是时间问题,所以我把那些time.sleep(1)放在命令之间。似乎还是没
$
不同。它看起来是这样的:
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\u调试
级别为1。这意味着当我用SSH连接到这个东西时,我会看到那些HAPI调试被填充。当我这样做时,我看不到那些调试。我假设gotoshell
和hapi_debug=1
不是顶级命令,而是stcli
的子命令。换句话说,stcli
是一种shell
在这种情况下,您需要将要在子shell中执行的命令写入其stdin
:
stdin, stdout, stderr = ssh.exec_command('stcli')
stdin.write('gotoshell\n')
stdin.write('hapi_debug=1\n')
stdin.flush()
如果随后调用
stdout.read
,它将等待命令stcli
完成。它从来没有做过的事。如果要继续读取输出,则需要发送终止子shell的命令(通常exit\n
)
stdin.write('exit\n')
stdin.flush()
print(stdout.read())