在Paramiko的SSH服务器上的辅助shell/命令中执行(子)命令 [英] Execute (sub)commands in secondary shell/command on SSH server in Paramiko
问题描述
我的目标是能够通过 SSH 连接到设备,执行 CLI 命令,这会将我带到另一个 Shell,我可以在其中输入我的命令.目前,我能够成功地通过 SSH 连接到设备,但无法弄清楚如何使用 CLI 访问该辅助 shell.我的代码如下
My goal is to be able to SSH into a device, execute CLI command which will take me to another Shell where I can enter in my commands. Currently, I am able to successfully SSH into a device, but cannot figure out how to get to that secondary shell with the CLI. My code below
import datetime, logging, os, paramiko, re, scp, sys, time, socket, logging
SSH = paramiko.SSHClient()
SSH.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SSH.connect(server, username=usr, password=password, port=22, timeout=2)
print('successful ssh')
stdin, stdout, stderr = SSH.exec_command('cli console',bufsize=2)
# inBuf = stdout.readlines()
# for line in inBuf:
# print(line.strip('\n'))
SSH.close()
我最初的假设是,在执行 cli 进入 shell 控制台后,我将能够简单地执行我想要的任何命令,但事实并非如此.任何帮助将不胜感激
My initial assumption is that after executing the cli to get into the shell console, I would be able to just simply execute whatever command I want but that is not the case. Any help would be appreciated
推荐答案
将要在子shell中执行的命令写入stdin
:
Write the commands that you want to execute in the subshell to the stdin
:
stdin.write('command\n')
stdin.flush()
这篇关于在Paramiko的SSH服务器上的辅助shell/命令中执行(子)命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!