在Python Paramiko中的SSH服务器上的辅助Shell/命令中执行(子)命令 [英] 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中执行的命令写入其stdin
:
stdin, stdout, stderr = ssh.exec_command('stcli')
stdin.write('gotoshell\n')
stdin.write('hapi_debug=1\n')
stdin.flush()
如果随后调用stdout.read
,它将等待直到命令stcli
完成.它永远不会做.如果要继续读取输出,则需要发送一个终止子外壳程序的命令(通常为exit\n
).
stdin.write('exit\n')
stdin.flush()
print(stdout.read())
I'm having a problem with a ShoreTel voice switch, and I'm trying to use Paramiko to jump into it and run a couple commands. What I believe the problem might be, is that the ShoreTel CLI gives different prompts than the standard Linux $
. It would look like this:
server1$:stcli
Mitel>gotoshell
CLI> (This is where I need to enter 'hapi_debug=1')
Is Python still expecting that $
, or am I missing something else?
I thought it might be a time thing, so I put those time.sleep(1)
between commands. Still doesn't seem to be taking.
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")
What I would expect from the successful execution of this code, would be for the hapi_debug
level to be 1. Which means that when I SSH into the thing, I would see those HAPI debugs populating. When I do, I do not see those debugs.
I assume that the gotoshell
and hapi_debug=1
are not top-level commands, but subcommands of the stcli
. In other words, the stcli
is kind of a shell.
In that case, you need to write the commands that you want to execute in the subshell to its stdin
:
stdin, stdout, stderr = ssh.exec_command('stcli')
stdin.write('gotoshell\n')
stdin.write('hapi_debug=1\n')
stdin.flush()
If you call stdout.read
afterwards, it will wait until the command stcli
finishes. What it never does. If you wanted to keep reading the output, you need to send a command that terminates the subshell (typically exit\n
).
stdin.write('exit\n')
stdin.flush()
print(stdout.read())
这篇关于在Python Paramiko中的SSH服务器上的辅助Shell/命令中执行(子)命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!