def get_hosts(hostregex): """return all hosts that are in the known_hosts file and match the given regex """ try: keys = util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) except SSHException as e: print(colored('Error in ~/.ssh/known_hosts:', 'red', attrs=['bold'])) sys.exit(colored(e.message, 'red')) hosts = [] try: rex = re.compile(hostregex) except: sys.exit(colored('Invalid regular expression!', 'red', attrs=['bold'])) for key in keys: if rex.match(key): hosts.append(key) match_success = 'green' if len(hosts) > 0 else 'red' print('>>> {} Hosts matched:'.format(colored(len(hosts), match_success, attrs=['bold']))) print('') print('\n'.join(sorted(hosts))) print('') return hosts
def get_hosts(hostregex): """return all hosts that are in the known_hosts file and match the given regex. Will return an empty list if no hosts matched. """ try: keys = util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) except SSHException as e: print(colored('Error in ~/.ssh/known_hosts:', 'red', attrs=['bold'])) sys.exit(colored(e.message, 'red')) hosts = [] try: rex = re.compile(hostregex) except re.error: sys.exit(colored('Invalid regular expression!', 'red', attrs=['bold'])) keys = [key for key in keys] + list(keys_from_config()) for key in keys: custom_port_match = RE_HOST_PORT.match(key) if custom_port_match: key = custom_port_match.group(1) if rex.match(key): hosts.append(key) match_success = 'green' if len(hosts) > 0 else 'red' print('>>> {} Hosts matched:'.format( colored(len(hosts), match_success, attrs=['bold']))) print('') print('\n'.join(sorted(hosts))) print('') return hosts
def get_hosts(hostregex): """return all hosts that are in the known_hosts file and match the given regex. Will return an empty list if no hosts matched. """ try: keys = util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) except SSHException as e: print(colored('Error in ~/.ssh/known_hosts:', 'red', attrs=['bold'])) sys.exit(colored(e.message, 'red')) hosts = [] try: rex = re.compile(hostregex) except re.error: sys.exit(colored('Invalid regular expression!', 'red', attrs=['bold'])) keys = [key for key in keys] + list(keys_from_config()) for key in keys: custom_port_match = RE_HOST_PORT.match(key) if custom_port_match: key = custom_port_match.group(1) if rex.match(key): hosts.append(key) match_success = 'green' if len(hosts) > 0 else 'red' print('>>> {} Hosts matched:'.format(colored(len(hosts), match_success, attrs=['bold']))) print('') print('\n'.join(sorted(hosts))) print('') return hosts
def sftpOpenConnection(self, target): """ opens an sftp connection to the given target host; credentials are taken from /etc/ssh/sftp_passwd """ from paramiko import Transport, SFTPClient from paramiko.util import load_host_keys hostname, username, password = self.getCredentials(target) hostkeys = load_host_keys('/etc/ssh/ssh_known_hosts') hostkeytype, hostkey = hostkeys[hostname].items()[0] trans = Transport((hostname, 22)) trans.connect(username=username, password=password, hostkey=hostkey) return SFTPClient.from_transport(trans)
def __load_hk(self): try: keys = load_host_keys(self.knownfile) except: error("No valid public keys found in '" + self.knownfile + "'") self.knownkey = '' for h in [self.host, self.hhost]: if h in keys: if self.keytype in keys[h]: self.knownkey = keys[h][self.keytype] break if self.knownkey == '': error("No valid RSA or DSA host key for '" + self.host + "'")
def get_host_keys(hostname, sshdir): """get host key""" hostkey = None try: host_keys = load_host_keys(os.path.join(sshdir, 'known_hosts')) except IOError: host_keys = {} if hostname in host_keys: hostkeytype = host_keys[hostname].keys()[0] hostkey = host_keys[hostname][hostkeytype] return hostkey