python paramiko invoke_shell,在Python Paramiko中的SSH服务器上的辅助Shell /命令中执行(子)命令...

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())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值