1919Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020"""
2121
22+ import logging
2223import gevent .pool
2324from gevent import socket
24- import paramiko
25- import os
26- import logging
2725from gevent import monkey
2826monkey .patch_all ()
27+ import paramiko
28+ import os
2929
3030host_logger = logging .getLogger ('host_logging' )
3131handler = logging .StreamHandler ()
3737
3838logger = logging .getLogger (__name__ )
3939
40-
4140def _setup_logger (_logger ):
4241 """Setup default logger"""
4342 _handler = logging .StreamHandler ()
4443 log_format = logging .Formatter ('%(name)s - %(asctime)s - %(levelname)s - %(message)s' )
4544 _handler .setFormatter (log_format )
4645 _logger .addHandler (handler )
4746 _logger .setLevel (logging .DEBUG )
48-
4947
5048class UnknownHostException (Exception ):
5149 """Raised when a host is unknown (dns failure)"""
@@ -93,7 +91,7 @@ def __init__(self, host,
9391 _ssh_config_file = os .path .sep .join ([os .path .expanduser ('~' ),
9492 '.ssh' ,
9593 'config' ])
96- # Load ~/.ssh/config if it exists to pick up username
94+ # Load ~/.ssh/config if it exists to pick up username, ProxyCommand
9795 # and host address if set
9896 if os .path .isfile (_ssh_config_file ):
9997 ssh_config .parse (open (_ssh_config_file ))
@@ -115,14 +113,20 @@ def __init__(self, host,
115113 self .pkey = pkey
116114 self .port = port if port else 22
117115 self .host = resolved_address
116+ self .proxy_command = paramiko .ProxyCommand (host_config ['proxycommand' ]) if 'proxycommand' in \
117+ host_config else None
118+ if self .proxy_command :
119+ logger .debug ("Proxy configured for destination host %s - ProxyCommand: '%s'" % (
120+ self .host , " " .join (self .proxy_command .cmd ),))
118121 self ._connect ()
119122
120123 def _connect (self , retries = 1 ):
121124 """Connect to host, throw UnknownHost exception on DNS errors"""
122125 try :
123126 self .client .connect (self .host , username = self .user ,
124127 password = self .password , port = self .port ,
125- pkey = self .pkey )
128+ pkey = self .pkey ,
129+ sock = self .proxy_command )
126130 except socket .gaierror , e :
127131 logger .error ("Could not resolve host '%s'" , self .host ,)
128132 while retries < NUM_RETRIES :
0 commit comments