Ejemplo n.º 1
0
Archivo: main.py Proyecto: dobe/pypsh
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
	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 + "'")
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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