Skip to content

Commit 982a060

Browse files
author
Dan
committed
Moved openssh config parsing to utils. Added key file and port to config parsing.
1 parent 005749a commit 982a060

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

pssh/pssh_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __init__(self, hosts,
116116
Exit code(s) will not be available immediately.
117117
118118
>>> print output
119-
119+
120120
::
121121
122122
{'myhost1': {'exit_code': None,

pssh/ssh_client.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .exceptions import UnknownHostException, AuthenticationException, \
2828
ConnectionErrorException, SSHException
2929
from .constants import DEFAULT_RETRIES
30+
from .utils import read_openssh_config
3031
import logging
3132

3233
host_logger = logging.getLogger('pssh.host_logger')
@@ -92,29 +93,17 @@ def __init__(self, host,
9293
the SSH agent
9394
:type allow_agent: bool
9495
"""
95-
ssh_config = paramiko.SSHConfig()
96-
_ssh_config_file = os.path.sep.join([os.path.expanduser('~'),
97-
'.ssh',
98-
'config'])
99-
# Load ~/.ssh/config if it exists to pick up username
100-
# and host address if set
101-
if os.path.isfile(_ssh_config_file):
102-
ssh_config.parse(open(_ssh_config_file))
103-
host_config = ssh_config.lookup(host)
104-
resolved_address = (host_config['hostname'] if
105-
'hostname' in host_config
106-
else host)
107-
_user = host_config['user'] if 'user' in host_config else None
96+
host, _user, _port, _pkey = read_openssh_config(host)
10897
user = user if user else _user
10998
client = paramiko.SSHClient()
11099
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
111100
self.forward_ssh_agent = forward_ssh_agent
112101
self.client = client
113102
self.user = user
114103
self.password = password
115-
self.pkey = pkey
116-
self.port = port if port else 22
117-
self.host = resolved_address
104+
self.pkey = pkey if pkey else _pkey
105+
self.port = port if port else _port
106+
self.host = host
118107
self.allow_agent = allow_agent
119108
if agent:
120109
self.client._agent = agent

pssh/utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121

2222
import logging
2323
import gevent
24+
import os
2425
from paramiko.rsakey import RSAKey
2526
from paramiko.dsskey import DSSKey
2627
from paramiko.ecdsakey import ECDSAKey
27-
from paramiko import SSHException
28+
from paramiko import SSHException, SSHConfig
2829

2930
host_logger = logging.getLogger('pssh.host_logger')
3031
logger = logging.getLogger('pssh')
@@ -64,6 +65,34 @@ def load_private_key(_pkey):
6465
return pkey
6566
logger.error("Failed to load private key using all available key types - giving up..")
6667

68+
def read_openssh_config(_host, config_file=None):
69+
"""Parses user's OpenSSH config for per hostname configuration for
70+
hostname, user, port and private key values
71+
72+
:param _host: Hostname to lookup in config"""
73+
ssh_config = SSHConfig()
74+
_ssh_config_file = config_file if config_file else \
75+
os.path.sep.join([os.path.expanduser('~'),
76+
'.ssh', 'config'])
77+
# Load ~/.ssh/config if it exists to pick up username
78+
# and host address if set
79+
if os.path.isfile(_ssh_config_file):
80+
ssh_config.parse(open(_ssh_config_file))
81+
host_config = ssh_config.lookup(_host)
82+
host = (host_config['hostname'] if
83+
'hostname' in host_config
84+
else _host)
85+
user = host_config['user'] if 'user' in host_config else None
86+
port = int(host_config['port']) if 'port' in host_config else 22
87+
pkey = None
88+
# Try configured keys, pick first one that loads
89+
if 'identityfile' in host_config:
90+
for file_name in host_config['identityfile']:
91+
pkey = load_private_key(file_name)
92+
if pkey:
93+
break
94+
return host, user, port, pkey
95+
6796
# def enable_pssh_logger():
6897
# """Enable parallel-ssh's logger to stdout"""
6998
# enable_logger(logger)

tests/test_ssh_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,8 @@ def test_ssh_client_pty(self):
286286
exit_code,))
287287
del client
288288

289+
def test_openssh_config(self):
290+
pass
291+
289292
if __name__ == '__main__':
290293
unittest.main()

0 commit comments

Comments
 (0)