[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-55]
fix the loading of known_hosts in the demos to work on winodws/cygwin
This commit is contained in:
parent
0f3bf86617
commit
01ca23cace
7
demo.py
7
demo.py
|
@ -114,8 +114,11 @@ try:
|
||||||
try:
|
try:
|
||||||
keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
|
keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
|
||||||
except IOError:
|
except IOError:
|
||||||
print '*** Unable to open host keys file'
|
try:
|
||||||
keys = {}
|
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()
|
key = t.get_remote_server_key()
|
||||||
if not keys.has_key(hostname):
|
if not keys.has_key(hostname):
|
||||||
|
|
|
@ -22,35 +22,6 @@ import sys, os, base64, getpass, socket, traceback, termios, tty, select
|
||||||
import paramiko
|
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
|
# setup logging
|
||||||
paramiko.util.log_to_file('demo_simple.log')
|
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
|
# get host key, if we know one
|
||||||
hostkeytype = None
|
hostkeytype = None
|
||||||
hostkey = 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):
|
if hkeys.has_key(hostname):
|
||||||
hostkeytype = hkeys[hostname].keys()[0]
|
hostkeytype = hkeys[hostname].keys()[0]
|
||||||
hostkey = hkeys[hostname][hostkeytype]
|
hostkey = hkeys[hostname][hostkeytype]
|
||||||
|
|
37
forward.py
37
forward.py
|
@ -89,32 +89,17 @@ def forward_tunnel(local_port, remote_host, remote_port, transport):
|
||||||
ssh_transport = transport
|
ssh_transport = transport
|
||||||
ForwardServer(('', local_port), SubHander).serve_forever()
|
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():
|
def find_default_key_file():
|
||||||
filename = os.path.expanduser('~/.ssh/id_rsa')
|
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):
|
if os.access(filename, os.R_OK):
|
||||||
return filename
|
return filename
|
||||||
filename = os.path.expanduser('~/.ssh/id_dsa')
|
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):
|
if os.access(filename, os.R_OK):
|
||||||
return filename
|
return filename
|
||||||
return ''
|
return ''
|
||||||
|
@ -174,7 +159,15 @@ if ':' in options.ssh_host:
|
||||||
except:
|
except:
|
||||||
parser.error('SSH port must be a number.')
|
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):
|
if not host_keys.has_key(options.ssh_host):
|
||||||
print '*** Warning: no host key for %s' % options.ssh_host
|
print '*** Warning: no host key for %s' % options.ssh_host
|
||||||
expected_host_key_type = None
|
expected_host_key_type = None
|
||||||
|
|
Loading…
Reference in New Issue