我需要模拟SSH客户端并记录输入的命令。
我遵循了不同的链接,但是大多数示例与自动化任务有关。为了进行测试,我需要模拟一个交互式 SSH session 和 log 命令。
注意:Paramiko不是必需的。非常感谢扭曲的资源
最佳答案
使用 Channel.get_pty
和 Channel.invoke_shell
模拟交互式SSH终端 session 。
sshClient = paramiko.SSHClient()
sshClient.connect(host, username=user, password=pass)
channel = sshClient.get_transport().open_session()
# Open interactive SSH session
channel.get_pty()
channel.invoke_shell()
print('Executing command 1')
channel.send('command 1\n')
print('Executing command 2')
channel.send('command 2\n')
关于python - 使用Paramiko/Twisted模拟交互式SSH客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52411224/