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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 14:22:32

问题


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

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


回答1:


Write the commands that you want to execute in the subshell to the stdin:

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


来源:https://stackoverflow.com/questions/50708225/execute-subcommands-in-secondary-shell-command-on-ssh-server-in-paramiko

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!