Fix demos

This commit is contained in:
Scott Maxwell 2013-10-31 16:05:30 -07:00
parent bc683ac365
commit 951e8cfd3a
5 changed files with 48 additions and 33 deletions

View File

@ -28,6 +28,7 @@ import socket
import sys import sys
import time import time
import traceback import traceback
from paramiko.py3compat import input
import paramiko import paramiko
try: try:

View File

@ -28,6 +28,7 @@ import sys
import traceback import traceback
import paramiko import paramiko
from paramiko.py3compat import input
# setup logging # setup logging
@ -40,9 +41,9 @@ if len(sys.argv) > 1:
if hostname.find('@') >= 0: if hostname.find('@') >= 0:
username, hostname = hostname.split('@') username, hostname = hostname.split('@')
else: else:
hostname = raw_input('Hostname: ') hostname = input('Hostname: ')
if len(hostname) == 0: if len(hostname) == 0:
print '*** Hostname required.' print('*** Hostname required.')
sys.exit(1) sys.exit(1)
port = 22 port = 22
if hostname.find(':') >= 0: if hostname.find(':') >= 0:
@ -53,7 +54,7 @@ if hostname.find(':') >= 0:
# get username # get username
if username == '': if username == '':
default_username = getpass.getuser() default_username = getpass.getuser()
username = raw_input('Username [%s]: ' % default_username) username = input('Username [%s]: ' % default_username)
if len(username) == 0: if len(username) == 0:
username = default_username username = default_username
password = getpass.getpass('Password for %s@%s: ' % (username, hostname)) password = getpass.getpass('Password for %s@%s: ' % (username, hostname))
@ -69,13 +70,13 @@ except IOError:
# try ~/ssh/ too, because windows can't have a folder named ~/.ssh/ # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/
host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts'))
except IOError: except IOError:
print '*** Unable to open host keys file' print('*** Unable to open host keys file')
host_keys = {} host_keys = {}
if host_keys.has_key(hostname): if hostname in host_keys:
hostkeytype = host_keys[hostname].keys()[0] hostkeytype = host_keys[hostname].keys()[0]
hostkey = host_keys[hostname][hostkeytype] hostkey = host_keys[hostname][hostkeytype]
print 'Using host key of type %s' % hostkeytype print('Using host key of type %s' % hostkeytype)
# now, connect and use paramiko Transport to negotiate SSH2 across the connection # now, connect and use paramiko Transport to negotiate SSH2 across the connection
@ -86,22 +87,22 @@ try:
# dirlist on remote host # dirlist on remote host
dirlist = sftp.listdir('.') dirlist = sftp.listdir('.')
print "Dirlist:", dirlist print("Dirlist: %s" % dirlist)
# copy this demo onto the server # copy this demo onto the server
try: try:
sftp.mkdir("demo_sftp_folder") sftp.mkdir("demo_sftp_folder")
except IOError: except IOError:
print '(assuming demo_sftp_folder/ already exists)' print('(assuming demo_sftp_folder/ already exists)')
sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n')
data = open('demo_sftp.py', 'r').read() data = open('demo_sftp.py', 'r').read()
sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data) sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data)
print 'created demo_sftp_folder/ on the server' print('created demo_sftp_folder/ on the server')
# copy the README back here # copy the README back here
data = sftp.open('demo_sftp_folder/README', 'r').read() data = sftp.open('demo_sftp_folder/README', 'r').read()
open('README_demo_sftp', 'w').write(data) open('README_demo_sftp', 'w').write(data)
print 'copied README back here' print('copied README back here')
# BETTER: use the get() and put() methods # BETTER: use the get() and put() methods
sftp.put('demo_sftp.py', 'demo_sftp_folder/demo_sftp.py') sftp.put('demo_sftp.py', 'demo_sftp_folder/demo_sftp.py')
@ -109,8 +110,9 @@ try:
t.close() t.close()
except Exception, e: except Exception:
print '*** Caught exception: %s: %s' % (e.__class__, e) e = sys.exc_info()[1]
print('*** Caught exception: %s: %s' % (e.__class__, e))
traceback.print_exc() traceback.print_exc()
try: try:
t.close() t.close()

View File

@ -25,9 +25,13 @@ import os
import socket import socket
import sys import sys
import traceback import traceback
from paramiko.py3compat import input
import paramiko import paramiko
import interactive try:
import interactive
except ImportError:
from . import interactive
# setup logging # setup logging
@ -40,9 +44,9 @@ if len(sys.argv) > 1:
if hostname.find('@') >= 0: if hostname.find('@') >= 0:
username, hostname = hostname.split('@') username, hostname = hostname.split('@')
else: else:
hostname = raw_input('Hostname: ') hostname = input('Hostname: ')
if len(hostname) == 0: if len(hostname) == 0:
print '*** Hostname required.' print('*** Hostname required.')
sys.exit(1) sys.exit(1)
port = 22 port = 22
if hostname.find(':') >= 0: if hostname.find(':') >= 0:
@ -53,7 +57,7 @@ if hostname.find(':') >= 0:
# get username # get username
if username == '': if username == '':
default_username = getpass.getuser() default_username = getpass.getuser()
username = raw_input('Username [%s]: ' % default_username) username = input('Username [%s]: ' % default_username)
if len(username) == 0: if len(username) == 0:
username = default_username username = default_username
password = getpass.getpass('Password for %s@%s: ' % (username, hostname)) password = getpass.getpass('Password for %s@%s: ' % (username, hostname))
@ -64,18 +68,18 @@ try:
client = paramiko.SSHClient() client = paramiko.SSHClient()
client.load_system_host_keys() client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy()) client.set_missing_host_key_policy(paramiko.WarningPolicy())
print '*** Connecting...' print('*** Connecting...')
client.connect(hostname, port, username, password) client.connect(hostname, port, username, password)
chan = client.invoke_shell() chan = client.invoke_shell()
print repr(client.get_transport()) print(repr(client.get_transport()))
print '*** Here we go!' print('*** Here we go!\n')
print
interactive.interactive_shell(chan) interactive.interactive_shell(chan)
chan.close() chan.close()
client.close() client.close()
except Exception, e: except Exception:
print '*** Caught exception: %s: %s' % (e.__class__, e) e = sys.exc_info()[1]
print('*** Caught exception: %s: %s' % (e.__class__, e))
traceback.print_exc() traceback.print_exc()
try: try:
client.close() client.close()

View File

@ -30,7 +30,11 @@ import getpass
import os import os
import socket import socket
import select import select
import SocketServer try:
import SocketServer
except ImportError:
import socketserver as SocketServer
import sys import sys
from optparse import OptionParser from optparse import OptionParser
@ -54,7 +58,8 @@ class Handler (SocketServer.BaseRequestHandler):
chan = self.ssh_transport.open_channel('direct-tcpip', chan = self.ssh_transport.open_channel('direct-tcpip',
(self.chain_host, self.chain_port), (self.chain_host, self.chain_port),
self.request.getpeername()) self.request.getpeername())
except Exception, e: except Exception:
e = sys.exc_info()[1]
verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, verbose('Incoming request to %s:%d failed: %s' % (self.chain_host,
self.chain_port, self.chain_port,
repr(e))) repr(e)))
@ -98,7 +103,7 @@ def forward_tunnel(local_port, remote_host, remote_port, transport):
def verbose(s): def verbose(s):
if g_verbose: if g_verbose:
print s print(s)
HELP = """\ HELP = """\
@ -165,8 +170,9 @@ def main():
try: try:
client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile,
look_for_keys=options.look_for_keys, password=password) look_for_keys=options.look_for_keys, password=password)
except Exception, e: except Exception:
print '*** Failed to connect to %s:%d: %r' % (server[0], server[1], e) e = sys.exc_info()[1]
print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e))
sys.exit(1) sys.exit(1)
verbose('Now forwarding port %d to %s:%d ...' % (options.port, remote[0], remote[1])) verbose('Now forwarding port %d to %s:%d ...' % (options.port, remote[0], remote[1]))
@ -174,7 +180,7 @@ def main():
try: try:
forward_tunnel(options.port, remote[0], remote[1], client.get_transport()) forward_tunnel(options.port, remote[0], remote[1], client.get_transport())
except KeyboardInterrupt: except KeyboardInterrupt:
print 'C-c: Port forwarding stopped.' print('C-c: Port forwarding stopped.')
sys.exit(0) sys.exit(0)

View File

@ -46,7 +46,8 @@ def handler(chan, host, port):
sock = socket.socket() sock = socket.socket()
try: try:
sock.connect((host, port)) sock.connect((host, port))
except Exception, e: except Exception:
e = sys.exc_info()[1]
verbose('Forwarding request to %s:%d failed: %r' % (host, port, e)) verbose('Forwarding request to %s:%d failed: %r' % (host, port, e))
return return
@ -82,7 +83,7 @@ def reverse_forward_tunnel(server_port, remote_host, remote_port, transport):
def verbose(s): def verbose(s):
if g_verbose: if g_verbose:
print s print(s)
HELP = """\ HELP = """\
@ -150,8 +151,9 @@ def main():
try: try:
client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile,
look_for_keys=options.look_for_keys, password=password) look_for_keys=options.look_for_keys, password=password)
except Exception, e: except Exception:
print '*** Failed to connect to %s:%d: %r' % (server[0], server[1], e) e = sys.exc_info()[1]
print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e))
sys.exit(1) sys.exit(1)
verbose('Now forwarding remote port %d to %s:%d ...' % (options.port, remote[0], remote[1])) verbose('Now forwarding remote port %d to %s:%d ...' % (options.port, remote[0], remote[1]))
@ -159,7 +161,7 @@ def main():
try: try:
reverse_forward_tunnel(options.port, remote[0], remote[1], client.get_transport()) reverse_forward_tunnel(options.port, remote[0], remote[1], client.get_transport())
except KeyboardInterrupt: except KeyboardInterrupt:
print 'C-c: Port forwarding stopped.' print('C-c: Port forwarding stopped.')
sys.exit(0) sys.exit(0)