Skip to content

Commit f0a43a4

Browse files
committed
Merge remote-tracking branch 'ssh_agent/connection_open_allow_ssh_agent' into release
2 parents e44971d + 7701899 commit f0a43a4

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

ssh_utilities/connection.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ def open(ssh_username: str, ssh_server: None = None,
299299
ssh_key_file: Optional[Union[str, "Path"]] = None,
300300
ssh_password: Optional[str] = None,
301301
server_name: Optional[str] = None, quiet: bool = False,
302-
thread_safe: bool = False) -> LocalConnection:
302+
thread_safe: bool = False,
303+
ssh_allow_agent: bool = False) -> LocalConnection:
303304
...
304305

305306
@overload
@@ -308,15 +309,17 @@ def open(ssh_username: str, ssh_server: str,
308309
ssh_key_file: Optional[Union[str, "Path"]] = None,
309310
ssh_password: Optional[str] = None,
310311
server_name: Optional[str] = None, quiet: bool = False,
311-
thread_safe: bool = False) -> SSHConnection:
312+
thread_safe: bool = False,
313+
ssh_allow_agent: bool = False) -> SSHConnection:
312314
...
313315

314316
@staticmethod
315317
def open(ssh_username: str, ssh_server: Optional[str] = "",
316318
ssh_key_file: Optional[Union[str, "Path"]] = None,
317319
ssh_password: Optional[str] = None,
318320
server_name: Optional[str] = None, quiet: bool = False,
319-
thread_safe: bool = False):
321+
thread_safe: bool = False,
322+
ssh_allow_agent: bool = False):
320323
"""Initialize SSH or local connection.
321324
322325
Local connection is only a wrapper around os and shutil module methods
@@ -343,6 +346,8 @@ def open(ssh_username: str, ssh_server: Optional[str] = "",
343346
make connection object thread safe so it can be safely accessed
344347
from any number of threads, it is disabled by default to avoid
345348
performance penalty of threading locks
349+
ssh_allow_agent: bool
350+
allow the use of the ssh-agent to connect. Will disable ssh_key_file.
346351
347352
Warnings
348353
--------
@@ -354,7 +359,12 @@ def open(ssh_username: str, ssh_server: Optional[str] = "",
354359
pkey_file=ssh_key_file,
355360
server_name=server_name, quiet=quiet)
356361
else:
357-
if ssh_key_file:
362+
if ssh_allow_agent:
363+
c = SSHConnection(ssh_server, ssh_username,
364+
allow_agent=ssh_allow_agent, line_rewrite=True,
365+
server_name=server_name, quiet=quiet,
366+
thread_safe=thread_safe)
367+
elif ssh_key_file:
358368
c = SSHConnection(ssh_server, ssh_username,
359369
pkey_file=ssh_key_file, line_rewrite=True,
360370
server_name=server_name, quiet=quiet,

ssh_utilities/remote/remote.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class SSHConnection(ConnectionABC):
6666
6767
Warnings
6868
--------
69-
At least one of (password, pkey_file) must be specified, if both are,
70-
RSA key will be used
69+
At least one of (password, pkey_file, allow_agent) must be specified, priority used is
70+
SSH-Agent -> RSA Key -> password
7171
7272
thread_safe parameter is not implemented yet!!!
7373
@@ -84,7 +84,8 @@ def __init__(self, address: str, username: str,
8484
password: Optional[str] = None,
8585
pkey_file: Optional[Union[str, Path]] = None,
8686
line_rewrite: bool = True, server_name: Optional[str] = None,
87-
quiet: bool = False, thread_safe: bool = False) -> None:
87+
quiet: bool = False, thread_safe: bool = False,
88+
allow_agent: Optional[bool] = False) -> None:
8889

8990
log.info(f"Connection object will {'' if thread_safe else 'not'} be "
9091
f"thread safe")
@@ -99,6 +100,10 @@ def __init__(self, address: str, username: str,
99100
lprint.line_rewrite = line_rewrite
100101
lprnt = lprint(quiet)
101102

103+
if allow_agent:
104+
msg = f"Will login with ssh-agent"
105+
lprnt(msg)
106+
log.info(msg)
102107
if pkey_file:
103108
msg = f"Will login with private RSA key located in {pkey_file}"
104109
lprnt(msg)
@@ -130,9 +135,13 @@ def __init__(self, address: str, username: str,
130135
self.address = address
131136
self.username = username
132137
self.pkey_file = pkey_file
138+
self.allow_agent = allow_agent
133139

134140
# paramiko connection
135-
if pkey_file:
141+
if allow_agent:
142+
self.pkey = None
143+
self.password = None
144+
elif pkey_file:
136145
for key in _KEYS:
137146
try:
138147
self.pkey = key.from_private_key_file(
@@ -246,6 +255,9 @@ def _get_ssh(self, authentication_attempts: int = 0):
246255

247256
with self.__lock:
248257
try:
258+
if self.allow_agent:
259+
# connect using ssh-agent
260+
self.c.connect(self.address, username=self.username, allow_agent=True)
249261
if self.pkey:
250262
# connect with public key
251263
self.c.connect(self.address, username=self.username,

0 commit comments

Comments
 (0)