home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL v3'
- __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
- __docformat__ = 'restructuredtext en'
- import sys
- import socket
- import getpass
- from urlparse import urlparse
- from binascii import hexlify
- import paramiko
-
- def agent_auth(transport, username):
- agent = paramiko.Agent()
- agent_keys = agent.get_keys()
- if len(agent_keys) == 0:
- return None
- for key in agent_keys:
- print 'Trying ssh-agent key %s' % hexlify(key.get_fingerprint()),
-
- try:
- transport.auth_publickey(username, key)
- print '... success!'
- return True
- continue
- except paramiko.SSHException:
- len(agent_keys) == 0
- len(agent_keys) == 0
- print '... failed.'
- continue
-
-
-
- return False
-
-
- def portable_getpass(username, hostname, retry):
- return None(getpass.getpass % ('%sPlease enter the password for %s on %s: ' if retry else '', username, hostname))
-
-
- def password_auth(transport, username, hostname, getpw = portable_getpass):
- for i in range(3):
- pw = getpw(username, hostname, i > 0)
- transport.auth_password(username, pw)
- if transport.is_authenticated():
- return True
-
- return False
-
-
- def connect_to_url(url, getpw = portable_getpass, mode = 'r+', bufsize = -1):
- (protocol, host, path) = urlparse(url)[:3]
- if protocol != 'sftp':
- raise ValueError(_('URL must have the scheme sftp'))
- protocol != 'sftp'
-
- try:
- (username, host) = host.split('@')
- except:
- raise ValueError(_('host must be of the form user@hostname'))
-
- port = 22
- if ':' in host:
- (host, port) = host.split(':')
- port = int(port)
-
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.connect((host, port))
- t = paramiko.Transport(sock)
-
- try:
- t.start_client()
- except:
- raise Exception(_('Failed to negotiate SSH session: ') + str(t.get_exception()))
-
- if not agent_auth(t, username):
- if not password_auth(t, username, host, getpw):
- raise ValueError(_('Failed to authenticate with server: %s') % url)
- password_auth(t, username, host, getpw)
-
- sftp = paramiko.SFTPClient.from_transport(t)
- return (sftp, sftp.open(path, mode = mode, bufsize = bufsize))
-
-
- def main(args = sys.argv):
- f = connect_to_url(args[1])[-1]
- print f.read()
- f.seek(0, 2)
- print f.tell()
- f.close()
- return 0
-
- if __name__ == '__main__':
- sys.exit(main())
-
-