Skip to content

Commit

Permalink
Merge pull request #13 from choppsv1/allkeytypes
Browse files Browse the repository at this point in the history
Add generic key loading function to try all types.
  • Loading branch information
choppsv1 authored Sep 29, 2018
2 parents ce91e79 + 552f1c9 commit e3ab876
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions sshutil/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,27 @@
import threading
import traceback
import paramiko as ssh
import paramiko.dsskey
import paramiko.rsakey
import paramiko.ecdsakey
import paramiko.ed25519key

logger = logging.getLogger(__name__)


def from_private_key_file(keyfile, password=None):
"""Return a private key from a file, try all the types."""
keyclasses = [
paramiko.rsakey.RSAKey, paramiko.dsskey.DSSKey, paramiko.ecdsakey.ECDSAKey,
paramiko.ed25519key.Ed25519Key
]
for cl in keyclasses:
try:
return cl.from_private_key_file(keyfile, password)
except paramiko.SSHException:
continue


def is_sock_closed(sock):
"""Check to see if the socket is ready for reading but nothing is there, IOW it's closed"""
rds, _, _ = select.select([sock], [], [], 0)
Expand Down Expand Up @@ -244,8 +261,8 @@ def close(self):
self.ssh = None

if self.client_socket:
logger.debug("%s: close closing client socket %s", str(self), str(
self.client_socket))
logger.debug("%s: close closing client socket %s", str(self),
str(self.client_socket))
self.client_socket.close()
self.client_socket = None

Expand Down Expand Up @@ -356,12 +373,12 @@ def __init__(self,
# Load the host key for our ssh server.
if host_key:
assert os.path.exists(host_key)
self.host_key = ssh.RSAKey.from_private_key_file(host_key)
self.host_key = from_private_key_file(host_key)
else:
for keypath in ["/etc/ssh/ssh_host_rsa_key", "/etc/ssh/ssh_host_dsa_key"]:
# XXX check we have access
if os.path.exists(keypath):
self.host_key = ssh.RSAKey.from_private_key_file(keypath)
self.host_key = from_private_key_file(keypath)
break

# Bind first to IPv6, if the OS supports binding per AF then the IPv4
Expand Down

0 comments on commit e3ab876

Please sign in to comment.