简体   繁体   English

在Paramiko的SSH服务器上的辅助Shell /命令中执行(子)命令

[英]Execute (sub)commands in secondary shell/command on SSH server in Paramiko

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. 我的目标是能够通过SSH进入设备,执行CLI命令,这会将我带到另一个可以在其中输入命令的Shell。 Currently, I am able to successfully SSH into a device, but cannot figure out how to get to that secondary shell with the CLI. 目前,我能够成功地通过SSH进入设备,但无法弄清楚如何使用CLI进入该辅助Shell。 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()

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. 我最初的假设是,执行cli进入shell控制台后,我将能够简单地执行我想要的任何命令,但实际情况并非如此。 Any help would be appreciated 任何帮助,将不胜感激

Write the commands that you want to execute in the subshell to the stdin : 将要在子外壳中执行的命令写到stdin

stdin.write('command\n')
stdin.flush()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM