def dummytest():
"""
Code posted on the github issue regarding the SSH Banner Error on the paramiko github.
https://github.com/paramiko/paramiko/issues/673
"""
import os
import paramiko
import logging
logging.basicConfig(level=logging.DEBUG)
# Loading ssh configuration to get the IP and user of the desired host (here 'bastion')
cfg = paramiko.SSHConfig()
with open(os.path.expanduser("~/.ssh/config")) as f:
cfg.parse(f)
host_cfg = cfg.lookup('bastion')
sock = paramiko.ProxyCommand("ssh {}@{} nc 10.8.9.160 22".format(host_cfg.get('user'), host_cfg.get('hostname')))
sock.settimeout(30)
# Sock stays open until a client tries to use it (or the program exits)
# Client Setup
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect and execute command
# The following line hangs for 15 seconds (increase with banner_timeout argument) and raises an SSHError
client.connect("10.8.9.160", username='root', sock=sock)
(stdin, stdout, stderr) = client.exec_command("echo 'Hello World !'")
for line in stdout.readlines():
print(line)
client.close()
评论列表
文章目录