Skip to content

Commit ecb01d2

Browse files
author
Pan
committed
Set parallel client host to actual destination when using proxy for native client - resolves #120
1 parent a255b25 commit ecb01d2

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

Changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Change Log
22
============
33

4+
1.6.1
5+
+++++++
6+
7+
Fixes
8+
-------
9+
10+
* Host would always be `127.0.0.1` when using ``proxy_host`` on native client - #120.
11+
412
1.6.0
513
++++++
614

pssh/clients/native/parallel.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,13 @@ def _make_ssh_client(self, host):
316316
if self.proxy_host is not None:
317317
tunnel = self._start_tunnel(host)
318318
_user, _port, _password, _pkey = self._get_host_config_values(host)
319-
_host = host if self.proxy_host is None else '127.0.0.1'
319+
proxy_host = None if self.proxy_host is None else '127.0.0.1'
320320
_port = _port if self.proxy_host is None else tunnel.listen_port
321321
self.host_clients[host] = SSHClient(
322-
_host, user=_user, password=_password, port=_port, pkey=_pkey,
322+
host, user=_user, password=_password, port=_port, pkey=_pkey,
323323
num_retries=self.num_retries, timeout=self.timeout,
324-
allow_agent=self.allow_agent, retry_delay=self.retry_delay)
324+
allow_agent=self.allow_agent, retry_delay=self.retry_delay,
325+
proxy_host=proxy_host)
325326

326327
def copy_file(self, local_file, remote_file, recurse=False, copy_args=None):
327328
"""Copy local file to remote file in parallel

pssh/clients/native/single.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def __init__(self, host,
6464
num_retries=DEFAULT_RETRIES,
6565
retry_delay=RETRY_DELAY,
6666
allow_agent=True, timeout=None,
67-
forward_ssh_agent=True):
67+
forward_ssh_agent=True,
68+
proxy_host=None):
6869
""":param host: Host name or IP to connect to.
6970
:type host: str
7071
:param user: User to connect as. Defaults to logged in user.
@@ -93,6 +94,9 @@ def __init__(self, host,
9394
equivalent to `ssh -A` from the `ssh` command line utility.
9495
Defaults to True if not set.
9596
:type forward_ssh_agent: bool
97+
:param proxy_host: Connection to host is via provided proxy host
98+
and client should use self.proxy_host for connection attempts.
99+
:type proxy_host: str
96100
"""
97101
self.host = host
98102
self.user = user if user else None
@@ -111,7 +115,8 @@ def __init__(self, host,
111115
self.forward_ssh_agent = forward_ssh_agent
112116
self._forward_requested = False
113117
self.session = None
114-
self._connect(self.host, self.port)
118+
self._host = proxy_host if proxy_host else host
119+
self._connect(self._host, self.port)
115120
THREAD_POOL.apply(self._init)
116121

117122
def disconnect(self):
@@ -139,7 +144,7 @@ def _connect_init_retry(self, retries):
139144
if not self.sock.closed:
140145
self.sock.close()
141146
sleep(self.retry_delay)
142-
self._connect(self.host, self.port, retries=retries)
147+
self._connect(self._host, self.port, retries=retries)
143148
return self._init(retries=retries)
144149

145150
def _init(self, retries=1):

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
'optimize.use_switch': True,
3939
'wraparound': False,
4040
}
41-
_embedded_lib = bool(os.environ.get('EMBEDDED_LIB', 1))
41+
_embedded_lib = bool(int(os.environ.get('EMBEDDED_LIB', 1)))
4242

4343
cython_args = {'cython_directives': cython_directives,
4444
'cython_compile_time_env': {'EMBEDDED_LIB': _embedded_lib},
@@ -66,7 +66,6 @@
6666
**cython_args
6767
)]
6868

69-
7069
package_data = {}
7170

7271
if ON_WINDOWS:

tests/test_native_parallel_client.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,8 +1389,8 @@ def test_scp_send_dir(self):
13891389
gevent.joinall(cmds, raise_error=True)
13901390
self.assertTrue(os.path.isdir(remote_test_dir_abspath))
13911391
self.assertTrue(os.path.isfile(remote_file_abspath))
1392-
remote_file_data = open(remote_file_abspath, 'r').readlines()
1393-
self.assertEqual(remote_file_data[0].strip(), test_file_data)
1392+
remote_file_data = open(remote_file_abspath, 'r').read()
1393+
self.assertEqual(remote_file_data.strip(), test_file_data)
13941394
except Exception:
13951395
raise
13961396
finally:
@@ -1496,19 +1496,19 @@ def test_scp_recv(self):
14961496
shutil.rmtree(remote_test_path_abs)
14971497
shutil.rmtree(local_copied_dir)
14981498

1499-
## OpenSSHServer needs to run in its own thread for this test to work
1500-
## Race conditions otherwise.
1501-
#
1502-
# def test_tunnel(self):
1503-
# proxy_host = '127.0.0.9'
1504-
# server = OpenSSHServer(listen_ip=proxy_host, port=self.port)
1505-
# server.start_server()
1506-
# client = ParallelSSHClient(
1507-
# [self.host], port=self.port, pkey=self.user_key,
1508-
# proxy_host=proxy_host, proxy_port=self.port, num_retries=1,
1509-
# proxy_pkey=self.user_key,
1510-
# timeout=2)
1511-
# client.join(client.run_command('echo me'))
1499+
# This is a unit test, no output is checked, due to race conditions
1500+
# with running server in same thread.
1501+
def test_tunnel(self):
1502+
proxy_host = '127.0.0.9'
1503+
server = OpenSSHServer(listen_ip=proxy_host, port=self.port)
1504+
server.start_server()
1505+
client = ParallelSSHClient(
1506+
[self.host], port=self.port, pkey=self.user_key,
1507+
proxy_host=proxy_host, proxy_port=self.port, num_retries=1,
1508+
proxy_pkey=self.user_key,
1509+
timeout=2)
1510+
output = client.run_command('echo me', stop_on_errors=False)
1511+
self.assertEqual(self.host, list(output.keys())[0])
15121512

15131513
# def test_proxy_remote_host_failure_timeout(self):
15141514
# """Test that timeout setting is passed on to proxy to be used for the

0 commit comments

Comments
 (0)