Skip to content

Commit c158ac7

Browse files
Danpkittenis
authored andcommitted
Use ProxyCommand from ~/.ssh/config if configured. This resolves #6
1 parent 7c9d817 commit c158ac7

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pssh.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020
"""
2121

22+
import logging
2223
import gevent.pool
2324
from gevent import socket
24-
import paramiko
25-
import os
26-
import logging
2725
from gevent import monkey
2826
monkey.patch_all()
27+
import paramiko
28+
import os
2929

3030
host_logger = logging.getLogger('host_logging')
3131
handler = logging.StreamHandler()
@@ -37,15 +37,13 @@
3737

3838
logger = logging.getLogger(__name__)
3939

40-
4140
def _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

5048
class 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

Comments
 (0)