def remote(self, node_name=None, host=None, address_pool=None, username=None): """Get SSHClient by a node name or hostname. One of the following arguments should be specified: - host (str): IP address or hostname. If specified, 'node_name' is ignored. - node_name (str): Name of the node stored to config.underlay.ssh - address_pool (str): optional for node_name. If None, use the first matched node_name. """ ssh_data = self.__ssh_data(node_name=node_name, host=host, address_pool=address_pool) ssh_auth = ssh_client.SSHAuth( username=username or ssh_data['login'], password=ssh_data['password'], keys=[ rsakey.RSAKey(file_obj=StringIO.StringIO(key)) for key in ssh_data['keys'] ]) client = ssh_client.SSHClient(host=ssh_data['host'], port=ssh_data['port'] or 22, auth=ssh_auth) client._ssh.get_transport().set_keepalive( settings.SSH_SERVER_ALIVE_INTERVAL) return client
def get_key_object(self): """ Identify key contents and return appropriate paramiko public key object """ key_type, data = self.key_data() data = base64.b64decode(data) if key_type == "ssh-rsa": key = rsakey.RSAKey(data=data) elif key_type == "ssh-dss": key = dsskey.DSSKey(data=data) else: raise Exception("Invalid key type") return key
def public_key_data_to_object(cls, key_type, key_data): """Get a PKey object for a given public key type from data This is unfortunately a hack around the Paramiko library not giving us a good way to represent a generic public key with binary encoding, the alternative way to do this would be to have a (fake) host file containing one line and hoping the Paramiko code will correctly parses out the key type from there""" # TODO https://github.com/paramiko/paramiko/issues/663 (everything but # rsa currently fails) if key_type == RSA_KEY_TYPE: return rsakey.RSAKey(data=key_data) elif key_type == DSS_KEY_TYPE: return dsskey.DSSKey(data=key_data) elif key_type == EC_KEY_TYPE: return ecdsakey.ECDSAKey(data=key_data)
def update_server(server, endpoint): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) LOG.debug('Opening SSH connection root@%s', server['public_ip']) print server['ssh_private_key'] pkey = rsakey.RSAKey( file_obj=cStringIO.StringIO(server['ssh_private_key'])) #pkey = rsakey.RSAKey(file_obj=open('marat.pem')) ssh.connect(server['public_ip'], port=70, username=USERNAME, pkey=pkey, timeout=30) #ssh.connect('local.webta.net', username='******', pkey=pkey, timeout=30) private_cnf = '/etc/scalr/private.d/config.ini' chan = ssh.get_transport().open_session() chan.exec_command( "sudo -S python -c '" "import os, ConfigParser; " "private_cnf = \"" + private_cnf + "\"; " "endpoint = \"" + endpoint + "\"; " "cnf = ConfigParser.ConfigParser(); " "cnf.readfp(open(private_cnf)); " "cnf.set(\"general\", \"queryenv_url\", endpoint + \"/query-env\"); " "cnf.set(\"messaging_p2p\", \"producer_url\", endpoint + \"/messaging\"); " "cnf.write(open(private_cnf, \"w\")); " "'") stdin = chan.makefile('wb', -1) stderr = chan.makefile_stderr('rb', -1) stdin.write(PASSWORD + '\n') status = chan.recv_exit_status() if status: LOG.warn('Failed to patch %s: %s', private_cnf, ''.join(stderr.readlines())) LOG.info('Restarting Scalarizr') chan = ssh.get_transport().open_session() chan.exec_command('sudo -S /etc/init.d/scalarizr restart') chan.send(PASSWORD + '\n') status = chan.recv_exit_status() if status: LOG.warn('Scalarizr restart on %s failed with code: %s', server['public_ip'], status)
def events(self): client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) connargs = { 'hostname': self.host, 'port': self.port, 'username': self.userid } if self.key: keyfile = six.moves.StringIO(self.key) pkey = rsakey.RSAKey(file_obj=keyfile) connargs['pkey'] = pkey client.connect(**connargs) LOG.info('Connected to gerrit') stdin, stdout, stderr = client.exec_command('gerrit stream-events') for event in self._read_events(stdout, use_poll=True): yield event
def remote(self, node_name=None, host=None, address_pool=None): """Get SSHClient by a node name or hostname. One of the following arguments should be specified: - host (str): IP address or hostname. If specified, 'node_name' is ignored. - node_name (str): Name of the node stored to config.underlay.ssh - address_pool (str): optional for node_name. If None, use the first matched node_name. """ ssh_data = self.__ssh_data(node_name=node_name, host=host, address_pool=address_pool) return ssh_client.SSHClient( host=ssh_data['host'], port=ssh_data['port'] or 22, username=ssh_data['login'], password=ssh_data['password'], private_keys=[ rsakey.RSAKey(file_obj=StringIO.StringIO(key)) for key in ssh_data['keys'] ])