diff options
-rwxr-xr-x | demo.py | 7 | ||||
-rwxr-xr-x | demo_simple.py | 39 | ||||
-rwxr-xr-x | forward.py | 37 |
3 files changed, 29 insertions, 54 deletions
@@ -114,8 +114,11 @@ try: try: keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) except IOError: - print '*** Unable to open host keys file' - keys = {} + try: + keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) + except IOError: + print '*** Unable to open host keys file' + keys = {} key = t.get_remote_server_key() if not keys.has_key(hostname): diff --git a/demo_simple.py b/demo_simple.py index d664b994..5c3b1b02 100755 --- a/demo_simple.py +++ b/demo_simple.py @@ -22,35 +22,6 @@ import sys, os, base64, getpass, socket, traceback, termios, tty, select import paramiko -##### utility functions - -def load_host_keys(): - # this file won't exist on windows, but windows doesn't have a standard - # location for this file anyway. - filename = os.path.expanduser('~/.ssh/known_hosts') - keys = {} - try: - f = open(filename, 'r') - except Exception, e: - print '*** Unable to open host keys file (%s)' % filename - return - for line in f: - keylist = line.split(' ') - if len(keylist) != 3: - continue - hostlist, keytype, key = keylist - hosts = hostlist.split(',') - for host in hosts: - if not keys.has_key(host): - keys[host] = {} - if keytype == 'ssh-rsa': - keys[host][keytype] = paramiko.RSAKey(data=base64.decodestring(key)) - elif keytype == 'ssh-dss': - keys[host][keytype] = paramiko.DSSKey(data=base64.decodestring(key)) - f.close() - return keys - - # setup logging paramiko.util.log_to_file('demo_simple.log') @@ -83,7 +54,15 @@ password = getpass.getpass('Password for %s@%s: ' % (username, hostname)) # get host key, if we know one hostkeytype = None hostkey = None -hkeys = load_host_keys() +try: + hkeys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) +except IOError: + try: + hkeys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) + except IOError: + print '*** Unable to open host keys file' + hkeys = {} + if hkeys.has_key(hostname): hostkeytype = hkeys[hostname].keys()[0] hostkey = hkeys[hostname][hostkeytype] @@ -89,34 +89,19 @@ def forward_tunnel(local_port, remote_host, remote_port, transport): ssh_transport = transport ForwardServer(('', local_port), SubHander).serve_forever() -def load_host_keys(): - filename = os.path.expanduser('~/.ssh/known_hosts') - keys = {} - try: - f = open(filename, 'r') - except Exception, e: - print '*** Unable to open host keys file (%s)' % filename - return - for line in f: - keylist = line.split(' ') - if len(keylist) != 3: - continue - hostlist, keytype, key = keylist - hosts = hostlist.split(',') - for host in hosts: - if not keys.has_key(host): - keys[host] = {} - keys[host][keytype] = base64.decodestring(key) - f.close() - return keys - def find_default_key_file(): filename = os.path.expanduser('~/.ssh/id_rsa') if os.access(filename, os.R_OK): return filename + filename = os.path.expanduser('~/ssh/id_rsa') + if os.access(filename, os.R_OK): + return filename filename = os.path.expanduser('~/.ssh/id_dsa') if os.access(filename, os.R_OK): return filename + filename = os.path.expanduser('~/ssh/id_dsa') + if os.access(filename, os.R_OK): + return filename return '' def verbose(s): @@ -174,7 +159,15 @@ if ':' in options.ssh_host: except: parser.error('SSH port must be a number.') -host_keys = load_host_keys() +try: + host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) +except IOError: + try: + host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) + except IOError: + print '*** Unable to open host keys file' + host_keys = {} + if not host_keys.has_key(options.ssh_host): print '*** Warning: no host key for %s' % options.ssh_host expected_host_key_type = None |