def through(self, host, user='root'):
"""
Defines a proxy command to rebound on the host.
This method is actually unsafe to use and will cause an SSH Banner Error. This library can't work through a
proxy
:param string host: Either a valid name stored in ~/.ssh/config or a valid IP address.
:param string user: A valid user for the host. Default : 'root'.
:return: This instance. Allows to chain methods.
:rtype: CloudApp
"""
ssh_conf_file = os.path.expanduser("~/.ssh/config")
try:
ssh_conf = paramiko.SSHConfig()
with open(ssh_conf_file) as f:
ssh_conf.parse(f)
if host in ssh_conf.get_hostnames():
host_conf = ssh_conf.lookup(host)
user = host_conf.get('user', 'root')
host = host_conf.get('hostname')
else:
print("Could not find host {} in {}. Using raw hostname.".format(host, ssh_conf_file))
except FileNotFoundError as e:
print("Could not load {} : {}. Using raw hostname.".format(ssh_conf_file, e))
# "ssh -W %h:%p {}@{}" doesn't create an entry in the logs. "ssh {}@{} nc %h %p"
# Actually connects to name (causing an entry in the logs)
print("ssh {}@{} nc {} 22".format(user, host, self.instance.private_ip_address))
self.ssh_sock = paramiko.ProxyCommand("ssh {}@{} nc {} 22".format(user, host, self.instance.private_ip_address))
return self
评论列表
文章目录