home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1424 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.1 KB  |  97 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  6. __docformat__ = 'restructuredtext en'
  7. import sys
  8. import socket
  9. import getpass
  10. from urlparse import urlparse
  11. from binascii import hexlify
  12. import paramiko
  13.  
  14. def agent_auth(transport, username):
  15.     agent = paramiko.Agent()
  16.     agent_keys = agent.get_keys()
  17.     if len(agent_keys) == 0:
  18.         return None
  19.     for key in agent_keys:
  20.         print 'Trying ssh-agent key %s' % hexlify(key.get_fingerprint()),
  21.         
  22.         try:
  23.             transport.auth_publickey(username, key)
  24.             print '... success!'
  25.             return True
  26.         continue
  27.         except paramiko.SSHException:
  28.             len(agent_keys) == 0
  29.             len(agent_keys) == 0
  30.             print '... failed.'
  31.             continue
  32.         
  33.  
  34.     
  35.     return False
  36.  
  37.  
  38. def portable_getpass(username, hostname, retry):
  39.     return None(getpass.getpass % ('%sPlease enter the password for %s on %s: ' if retry else '', username, hostname))
  40.  
  41.  
  42. def password_auth(transport, username, hostname, getpw = portable_getpass):
  43.     for i in range(3):
  44.         pw = getpw(username, hostname, i > 0)
  45.         transport.auth_password(username, pw)
  46.         if transport.is_authenticated():
  47.             return True
  48.     
  49.     return False
  50.  
  51.  
  52. def connect_to_url(url, getpw = portable_getpass, mode = 'r+', bufsize = -1):
  53.     (protocol, host, path) = urlparse(url)[:3]
  54.     if protocol != 'sftp':
  55.         raise ValueError(_('URL must have the scheme sftp'))
  56.     protocol != 'sftp'
  57.     
  58.     try:
  59.         (username, host) = host.split('@')
  60.     except:
  61.         raise ValueError(_('host must be of the form user@hostname'))
  62.  
  63.     port = 22
  64.     if ':' in host:
  65.         (host, port) = host.split(':')
  66.         port = int(port)
  67.     
  68.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  69.     sock.connect((host, port))
  70.     t = paramiko.Transport(sock)
  71.     
  72.     try:
  73.         t.start_client()
  74.     except:
  75.         raise Exception(_('Failed to negotiate SSH session: ') + str(t.get_exception()))
  76.  
  77.     if not agent_auth(t, username):
  78.         if not password_auth(t, username, host, getpw):
  79.             raise ValueError(_('Failed to authenticate with server: %s') % url)
  80.         password_auth(t, username, host, getpw)
  81.     
  82.     sftp = paramiko.SFTPClient.from_transport(t)
  83.     return (sftp, sftp.open(path, mode = mode, bufsize = bufsize))
  84.  
  85.  
  86. def main(args = sys.argv):
  87.     f = connect_to_url(args[1])[-1]
  88.     print f.read()
  89.     f.seek(0, 2)
  90.     print f.tell()
  91.     f.close()
  92.     return 0
  93.  
  94. if __name__ == '__main__':
  95.     sys.exit(main())
  96.  
  97.