def connect(self, host=None, username=None, password=None, private_key=None, private_key_pass=None, port=22, timeout=30): host = host or self._host username = username or self._username password = password or self._password pkey = self._pkey if private_key: pkey = self.load_private_key(private_key, private_key_pass) log.debug("connecting to host %s on port %d as user %s" % (host, port, username)) try: sock = self._get_socket(host, port) transport = ssh.Transport(sock) transport.banner_timeout = timeout except socket.error: raise exception.SSHConnectionError(host, port) # Authenticate the transport. try: transport.connect(username=username, pkey=pkey, password=password) except ssh.AuthenticationException: raise exception.SSHAuthException(username, host) except ssh.SSHException, e: msg = e.args[0] raise exception.SSHError(msg)
def init(hostname, username, keyfile, passwd): global sftp, tc t = ssh.Transport(hostname) tc = t try: key = ssh.RSAKey.from_private_key_file(keyfile, passwd) except ssh.PasswordRequiredException: sys.stderr.write('\n\nssh.RSAKey.from_private_key_file REQUIRES PASSWORD.\n') sys.stderr.write('You have two options:\n') sys.stderr.write('* Use the "-K" option to point to a different (non-password-protected)\n') sys.stderr.write(' private key file.\n') sys.stderr.write('* Use the "-P" option to provide the password needed to unlock this private\n') sys.stderr.write(' key.\n') sys.stderr.write('\n') sys.exit(1) try: t.connect(username=username, pkey=key) except ssh.SSHException: t.close() sys.stderr.write('\n\nssh.Transport.connect FAILED.\n') sys.stderr.write('There are several possible reasons why it might fail so quickly:\n\n') sys.stderr.write('* The host to connect to (%s) is not a valid SSH server.\n' % hostname) sys.stderr.write(' (Use the "-H" option to change the host.)\n') sys.stderr.write('* The username to auth as (%s) is invalid.\n' % username) sys.stderr.write(' (Use the "-U" option to change the username.)\n') sys.stderr.write('* The private key given (%s) is not accepted by the server.\n' % keyfile) sys.stderr.write(' (Use the "-K" option to provide a different key file.)\n') sys.stderr.write('\n') sys.exit(1) sftp = ssh.SFTP.from_transport(t)
def _run(self): self.socks, addr = self.sockl.accept() self.ts = ssh.Transport(self.socks) host_key = ssh.RSAKey.from_private_key_file('tests/test_rsa.key') self.ts.add_server_key(host_key) server = NullServer() self.ts.start_server(self.event, server)
def connect(self): # get hostname username = '******' hostname = 'webhost.daily.co.uk' port = 22 password = open('../password.txt', 'r').read() # get host key, if we know one hostkeytype = None hostkey = None keypath = None try: keypath = os.path.expanduser( r'C:\MinGW\msys\1.0\home\h3r3tic\.ssh\known_hosts') print keypath host_keys = ssh.util.load_host_keys(keypath) except IOError: try: # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/ keypath = os.path.expanduser('/ssh/known_hosts') host_keys = ssh.util.load_host_keys(keypath) except IOError: print '*** Unable to open host keys file' host_keys = {} if host_keys.has_key(hostname): hostkeytype = host_keys[hostname].keys()[0] hostkey = host_keys[hostname][hostkeytype] print 'Using host key of type %s from %s' % (hostkeytype, keypath) self.t = ssh.Transport((hostname, port)) self.t.connect(username=username, password=password, hostkey=hostkey) self.sftp = ssh.SFTPClient.from_transport(self.t) self.sftp.chdir('public_html')
def init_transport(self): transport = ssh.Transport(self.request) transport.add_server_key(ssh.RSAKey(filename=SERVER_PRIVKEY)) transport.set_subsystem_handler('sftp', ssh.SFTPServer, sftp_si=FakeSFTPServer) server = TestServer(passwords, home, pubkeys, files) transport.start_server(server=server) self.ssh_server = server self.transport = transport
def init_loopback(): global sftp, tc socks = LoopSocket() sockc = LoopSocket() sockc.link(socks) tc = ssh.Transport(sockc) ts = ssh.Transport(socks) host_key = ssh.RSAKey.from_private_key_file('tests/test_rsa.key') ts.add_server_key(host_key) event = threading.Event() server = StubServer() ts.set_subsystem_handler('sftp', ssh.SFTPServer, StubSFTPServer) ts.start_server(event, server) tc.connect(username='******', password='******') event.wait(1.0) sftp = ssh.SFTP.from_transport(tc)
def connect(self, hostname, sock, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True): t = self._transport = ssh.Transport(sock) if self._log_channel is not None: t.set_log_channel(self._log_channel) t.start_client() ResourceManager.register(self, t) server_key = t.get_remote_server_key() keytype = server_key.get_name() our_server_key = self._system_host_keys.get(hostname, {}).get(keytype, None) if our_server_key is None: our_server_key = self._host_keys.get(hostname, {}).get(keytype, None) if our_server_key is None: # will raise exception if the key is rejected; let that fall out self._policy.missing_host_key(self, hostname, server_key) # if the callback returns, assume the key is ok our_server_key = server_key if server_key != our_server_key: raise ssh.BadHostKeyException(hostname, server_key, our_server_key) if username is None: username = getpass.getuser() if key_filename is None: key_filenames = [] elif isinstance(key_filename, (str, unicode)): key_filenames = [key_filename] else: key_filenames = key_filename self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
import ssh t = ssh.Transport(("localhost", 22)) t.connect(username="******", password="******") sftp = ssh.SFTPClient.from_transport(t) sftp.listdir() sftp.put("README", "README") sftp.put("/Users/chandrashekar/contact.txt", "README") sftp.listdir()
port = 22 if hostname.find(':') >= 0: hostname, portstr = hostname.split(':') port = int(portstr) # now connect try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, port)) except Exception, e: print '*** Connect failed: ' + str(e) traceback.print_exc() sys.exit(1) try: t = ssh.Transport(sock) try: t.start_client() except ssh.SSHException: print '*** SSH negotiation failed.' sys.exit(1) try: keys = ssh.util.load_host_keys( os.path.expanduser('~/.ssh/known_hosts')) except IOError: try: keys = ssh.util.load_host_keys( os.path.expanduser('~/ssh/known_hosts')) except IOError: print '*** Unable to open host keys file'
try: # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/ host_keys = ssh.util.load_host_keys( os.path.expanduser('~/ssh/known_hosts')) except IOError: print '*** Unable to open host keys file' host_keys = {} if host_keys.has_key(hostname): hostkeytype = host_keys[hostname].keys()[0] hostkey = host_keys[hostname][hostkeytype] print 'Using host key of type %s' % hostkeytype # now, connect and use ssh Transport to negotiate SSH2 across the connection try: t = ssh.Transport((hostname, port)) t.connect(username=username, password=password, hostkey=hostkey) sftp = ssh.SFTPClient.from_transport(t) # dirlist on remote host dirlist = sftp.listdir('.') print "Dirlist:", dirlist # copy this demo onto the server try: sftp.mkdir("demo_sftp_folder") except IOError: print '(assuming demo_sftp_folder/ already exists)' sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') data = open('demo_sftp.py', 'r').read()
traceback.print_exc() sys.exit(1) try: sock.listen(100) print 'Listening for connection ...' client, addr = sock.accept() except Exception, e: print '*** Listen/accept failed: ' + str(e) traceback.print_exc() sys.exit(1) print 'Got a connection!' try: t = ssh.Transport(client) try: t.load_server_moduli() except: print '(Failed to load moduli -- gex will be unsupported.)' raise t.add_server_key(host_key) server = Server() try: t.start_server(server=server) except ssh.SSHException, x: print '*** SSH negotiation failed.' sys.exit(1) # wait for auth chan = t.accept(20)