Ejemplo n.º 1
0
 def read_ssh_config(self, hostname):
     config_file = os.path.join(os.getenv('HOME'), '.ssh/config')
     config = SSHConfig()
     config.parse(open(config_file, 'r'))
     self.ssh_config = config.lookup(hostname)
     if len(self.ssh_config.keys()) < 2:
         print("Hostname no found in .ssh/config")
Ejemplo n.º 2
0
    def connect(self):
        ssh = SSHClient()
        config = SSHConfig()
        with open(os.path.expanduser("~/.ssh/config")) as _file:
            config.parse(_file)
        host_config = config.lookup(self.host)
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        ssh.load_system_host_keys()

        try:
            ssh.connect(
                self.host,
                username=self.user,
                password=self.password,
                key_filename=host_config["identityfile"][0],
                allow_agent=False,
                timeout=30,
            )
        except SSHException:
            raise SSHHelperException

        transport = ssh.get_transport()
        channel = transport.open_session()
        channel.setblocking(1)
        return ssh
Ejemplo n.º 3
0
    def _configure(self):
        """ Configure the ssh parameters from the config file. """
        configfile = expanduser("~/.ssh/config")
        if not isfile(configfile):
            raise GerritError("ssh config file '%s' does not exist" %
                              configfile)

        config = SSHConfig()
        config.parse(open(configfile))
        data = config.lookup(self.hostname)
        if not data:
            raise GerritError("No ssh config for host %s" % self.hostname)
        if 'hostname' not in data or 'port' not in data or 'user' not in data:
            raise GerritError("Missing configuration data in %s" % configfile)
        self.hostname = data['hostname']
        self.username = data['user']
        if 'identityfile' in data:
            key_filename = abspath(expanduser(data['identityfile'][0]))
            if not isfile(key_filename):
                raise GerritError("Identity file '%s' does not exist" %
                                  key_filename)
            self.key_filename = key_filename
        try:
            self.port = int(data['port'])
        except ValueError:
            raise GerritError("Invalid port: %s" % data['port'])
        if 'proxycommand' in data:
            self.proxy = ProxyCommand(data['proxycommand'])
    def __init__(self, output_dir: str):
        assert exists(output_dir), "Output dir {} does not exist.".format(
            output_dir)
        self._output_dir = output_dir

        ssh_config = SSHConfig()
        user_config_file = os.path.expanduser("~/.ssh/config")
        if os.path.exists(user_config_file):
            with open(user_config_file) as f:
                ssh_config.parse(f)

        self._ssh_client = SSHClient()

        self._ssh_client.set_missing_host_key_policy(client.AutoAddPolicy())
        # self._ssh_client.

        self._ssh_config = ssh_config.lookup('minicup')

        self._ssh_client.load_system_host_keys()
        self._ssh_config = {
            "hostname": self._ssh_config["hostname"],
            "username": self._ssh_config["user"],
            # "port": int(self._ssh_config["port"]),
            'key_filename': self._ssh_config['identityfile']
        }
        self._ssh_client.connect(**self._ssh_config)
Ejemplo n.º 5
0
 def __init__(self, hostname='localhost', username=None, password=None, 
             config_path='~/.ssh/config', port=None, verbose=False):
     
     if not hostname:
         raise ValueError('Missing hostname')
     self.sftp = None
     self.ssh = None
     ssh_config_path = path(config_path).expand()
     if not username:
         config = SSHConfig()
         if ssh_config_path.exists():
             config.parse(ssh_config_path.open())
             if config.lookup(hostname):
                host_config = config.lookup(hostname)
                username = host_config['user']
             else:
                 print 'Unknown host ', hostname
         else: print 'config file path wrong'
     
     self.verbose = verbose        
     if not username:
         username = getpass.getuser()
     if self.verbose:
         print 'Connection info: ', username, hostname, ssh_config_path
     
    #self.ssh.set_missing_host_key_policy(
     #    paramiko.AutoAddPolicy())
     self.hostname = hostname
     self.username = username
     self.password = password
     self.connect() 
Ejemplo n.º 6
0
def main():
    USAGE = "usage: %prog [options] host1:path1 host2:path2"
    parser = OptionParser(usage=USAGE)
    parser.add_option("-F", "--config-file",
                      action="store",
                      dest="config_file",
                      default="%s/.ssh/config" % os.environ['HOME'],
                      help="SSH config file (default: ~/.ssh/config)",)
    parser.add_option("--scp-options",
                      action="store",
                      dest="scp_options",
                      default="",
                      help="string of options (in quotes) passed directy to the scp command",)
    (options, args) = parser.parse_args()
    host1, path1 = args[0].split(':', 1)
    host2, path2 = args[1].split(':', 1)

    # ssh config file
    config = SSHConfig()
    config.parse(open(options.config_file))
    o = config.lookup(host2)

    # copy keyfile
    keyfile_remote = '/tmp/%s' % os.path.basename(o['identityfile'])
    run('scp %s %s:%s' % (o['identityfile'], host1, keyfile_remote))

    # copy actual file
    ssh_options = ' -o'.join(['='.join([k, v]) for k, v in o.iteritems()
                              if k != 'hostname' and k != 'identityfile'])
    if ssh_options:
        ssh_options = '-o' + ssh_options
    run('ssh %s scp %s -i %s -oStrictHostKeyChecking=no %s %s %s:%s' % (
            host1, options.scp_options, keyfile_remote, ssh_options, path1,
            o['hostname'], path2))
Ejemplo n.º 7
0
    def readsshconfig(self):
        config = os.path.expanduser('~/.ssh/config')
        if not os.path.exists(config):
            return
        f = open(config,'r')
        sshconfig = SSHConfig()
        sshconfig.parse(f)
        f.close()
        host = self.host
        try:
            host,port = host.split(':')
        except:
            port = None
        opt = sshconfig.lookup(host)

        if port is None:
            port = opt.get('port')

        host = opt.get('hostname', host)
        if port:
            host = "%s:%s" % (host,port)
        self.host=host
        if not self.identityfile:
            self.identityfile = opt.get('identityfile', None)
            if self.identityfile:
                self.identityfile = os.path.expanduser(self.identityfile).strip()
        if not self.user:
            self.user=opt.get('user','root')
Ejemplo n.º 8
0
    def connect(self, timeout=None):
        '''
        Setup the connection to remote SSH servers
        '''
        if self._connections is not None:
            raise RuntimeError('already connected')

        ssh_config = SSHConfig()
        ssh_config_file = os.path.sep.join(
            [os.path.expanduser('~'),
             '.ssh',
             'config'])

        if os.path.isfile(ssh_config_file):
            ssh_config.parse(open(ssh_config_file))

        self._connections = [SSHClient() for _ in self._host]

        for conn, host, port in zip(self._connections, self._host, self._port):
            self._pool.spawn(self._connect, conn, host, port, ssh_config)

        try:
            self._pool.join(timeout, raise_error=True)
        except:
            self._pool.kill(None)
            for conn in self._connections:
                try:
                    conn.close()
                except:
                    pass
            self._connections = None
Ejemplo n.º 9
0
def copy_file(infile, outfile, target_host='gateway'):
    logger.debug('Copying %s to %s:%s' % (infile, target_host, outfile))
    ssh = SSHClient()

    ssh_config_file = os.path.expanduser("~/.ssh/config")

    hostname = None
    if os.path.exists(ssh_config_file):
        conf = SSHConfig()

        with open(ssh_config_file) as f:
            conf.parse(f)

        host_config = conf.lookup(target_host)
        hostname = host_config['hostname']

    ssh.load_system_host_keys(os.path.expanduser("~/.ssh/known_hosts"))
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname)

    #ssh.connect(target_host)

    with SCPClient(ssh.get_transport()) as scp:
        scp.put(infile, outfile)
        scp.close()

    return
Ejemplo n.º 10
0
def pyocd_cli(args):
    # Don't use argparse - we want to pass args transparently to pyocd
    if len(args) < 2:
        print(
            f'usage: {args[0]} user@host:port [--cmd pyocd_executable] pyocd_args',
            file=sys.stderr)
        sys.exit(-1)

    ssh_args, pyocd_args = args[1], args[2:]

    # Split user@host:port settings
    ssh_user, ssh_args = ssh_args.split('@') if '@' in ssh_args else (None,
                                                                      ssh_args)
    ssh_host, ssh_port = ssh_args.split(':') if ':' in ssh_args else (ssh_args,
                                                                      None)

    # Get default host/port/user settings from ssh config file
    ssh_config = SSHConfig()
    user_config_file = os.path.expanduser("~/.ssh/config")
    if os.path.exists(user_config_file):
        with open(user_config_file) as f:
            ssh_config.parse(f)

    cfg = {'hostname': ssh_host, 'user': ssh_user, 'port': ssh_port}
    cfg_default = {'user': os.getlogin(), 'port': 22}

    user_config = ssh_config.lookup(cfg['hostname'])
    for k in ('hostname', 'user', 'port'):
        if k in user_config:
            cfg[k] = user_config[k]
        if cfg[k] is None:
            cfg[k] = cfg_default[k]

    pyocd_remote(cfg['user'], cfg['hostname'], int(cfg['port']), pyocd_args)
Ejemplo n.º 11
0
def read_openssh_config(host, config_file=None):
    """Parses user's OpenSSH config for per hostname configuration for
    hostname, user, port and private key values

    :param host: Hostname to lookup in config
    """
    _ssh_config_file = config_file if config_file else \
        os.path.sep.join([os.path.expanduser('~'), '.ssh', 'config'])
    # Load ~/.ssh/config if it exists to pick up username
    # and host address if set
    if not os.path.isfile(_ssh_config_file):
        return
    ssh_config = SSHConfig()
    ssh_config.parse(open(_ssh_config_file))
    host_config = ssh_config.lookup(host)
    host = (host_config['hostname'] if
            'hostname' in host_config
            else host)
    user = host_config['user'] if 'user' in host_config else None
    port = int(host_config['port']) if 'port' in host_config else 22
    pkey = None
    # Try configured keys, pick first one that loads
    if 'identityfile' in host_config:
        for file_name in host_config['identityfile']:
            pkey = load_private_key(file_name)
            if pkey:
                break
    return host, user, port, pkey
Ejemplo n.º 12
0
def read_openssh_config(host, config_file=None):
    """Parses user's OpenSSH config for per hostname configuration for
    hostname, user, port and private key values

    :param host: Hostname to lookup in config
    """
    _ssh_config_file = config_file if config_file else \
        os.path.sep.join([os.path.expanduser('~'), '.ssh', 'config'])
    # Load ~/.ssh/config if it exists to pick up username
    # and host address if set
    if not os.path.isfile(_ssh_config_file):
        return
    ssh_config = SSHConfig()
    ssh_config.parse(open(_ssh_config_file))
    host_config = ssh_config.lookup(host)
    host = (host_config['hostname'] if 'hostname' in host_config else host)
    user = host_config['user'] if 'user' in host_config else None
    port = int(host_config['port']) if 'port' in host_config else 22
    pkey = None
    # Try configured keys, pick first one that loads
    if 'identityfile' in host_config:
        for file_name in host_config['identityfile']:
            pkey = load_private_key(file_name)
            if pkey:
                break
    return host, user, port, pkey
Ejemplo n.º 13
0
    def _configure(self):
        """ Configure the ssh parameters from the config file. """
        configfile = expanduser("~/.ssh/config")
        if not isfile(configfile):
            raise GerritError("ssh config file '%s' does not exist" %
                              configfile)

        config = SSHConfig()
        config.parse(open(configfile))
        data = config.lookup(self.hostname)
        if not data:
            raise GerritError("No ssh config for host %s" % self.hostname)
        if 'hostname' not in data or 'port' not in data or 'user' not in data:
            raise GerritError("Missing configuration data in %s" % configfile)
        self.hostname = data['hostname']
        self.username = data['user']
        if 'identityfile' in data:
            key_filename = abspath(expanduser(data['identityfile'][0]))
            if not isfile(key_filename):
                raise GerritError("Identity file '%s' does not exist" %
                                  key_filename)
            self.key_filename = key_filename
        try:
            self.port = int(data['port'])
        except ValueError:
            raise GerritError("Invalid port: %s" % data['port'])
        if 'proxycommand' in data:
            self.proxy = ProxyCommand(data['proxycommand'])
Ejemplo n.º 14
0
    def __init__(self, hostname):
        """ Initialise and connect to SSH. """
        super(GerritSSHClient, self).__init__()
        self.load_system_host_keys()
        self.remote_version = None

        configfile = expanduser("~/.ssh/config")
        if not isfile(configfile):
            raise GerritError("ssh config file '%s' does not exist" %
                              configfile)

        config = SSHConfig()
        config.parse(open(configfile))
        data = config.lookup(hostname)
        if not data:
            raise GerritError("No ssh config for host %s" % hostname)
        if not 'hostname' in data or not 'port' in data or not 'user' in data:
            raise GerritError("Missing configuration data in %s" % configfile)
        key_filename = None
        if 'identityfile' in data:
            key_filename = abspath(expanduser(data['identityfile']))
            if not isfile(key_filename):
                raise GerritError("Identity file '%s' does not exist" %
                                  key_filename)
        try:
            port = int(data['port'])
        except ValueError:
            raise GerritError("Invalid port: %s" % data['port'])
        try:
            self.connect(hostname=data['hostname'],
                         port=port,
                         username=data['user'],
                         key_filename=key_filename)
        except socket.error as e:
            raise GerritError("Failed to connect to server: %s" % e)
Ejemplo n.º 15
0
Archivo: main.py Proyecto: dobe/pypsh
def start_procs(serial, hosts, starter_func, wait=0.0):
    config = SSHConfig()
    config.parse(open(os.path.expanduser('~/.ssh/config')))

    try:
        wait = float(wait)
    except ValueError:
        pass

    processes = []
    for host in hosts:
        process = starter_func(host, config.lookup(host))
        process.start()
        if serial or wait > 0.0:
            process.join()
            if isinstance(wait, Number):
                sleep(wait)
        processes.append(process)

    while multiprocessing.active_children():
        try:
            sleep(0.3)
        except KeyboardInterrupt:
            for p in processes:
                p.stop()
            break
    return processes
Ejemplo n.º 16
0
Archivo: .py Proyecto: torenord/at
def lookup(configfile):
    config = SSHConfig()
    config.parse(open(configfile))
    res = config.lookup(login)
    if res.get('user'):
        res['username'] = res['user']
        del res['user']
    return res
Ejemplo n.º 17
0
    def _configure(self):
        """
        Configure the ssh parameters from the config file.

        The Port and username are extracted from .ssh/config, unless
        overridden by arguments to the constructor.

        If username and or port are provided to `__init__`, they will
        override the values found in the configuration file.


        :raise:
            SSHException under the following conditions:

            * No configuration file is found
            * It does not contain an entry for the Host.
            * It references a keyfile which does not exist
            * The port number is non-numeric or negative
            * Values for port and username can not be determined

        """
        configfile = expanduser("~/.ssh/config")

        if not isfile(configfile):
            raise SSHException("ssh config file '%s' does not exist" %
                               configfile)

        config = SSHConfig()
        config.parse(open(configfile))
        data = config.lookup(self.hostname)

        if not data:
            raise SSHException("No ssh config for host %s" % self.hostname)

        self.hostname = data.get('hostname', None)
        self.proxy_cmd = data.get('proxycommand', None)

        if not self.username:
            self.username = data.get('user', None)

        if self.key_filename:
            self.key_filename = abspath(expanduser(self.key_filename))
        elif 'identityfile' in data:
            self.key_filename = abspath(expanduser(data['identityfile'][0]))

        if self.key_filename and not isfile(self.key_filename):
            raise SSHException("Identity file '%s' does not exist" %
                               self.key_filename)

        if self.port is None:
            try:
                self.port = int(data.get('port', '29418'))
            except ValueError:
                raise SSHException("Invalid port: %s" % data['port'])

        config_data = (self.hostname, self.port, self.username)
        if not all(config_data):
            raise SSHException("Missing configuration data in %s" % configfile)
Ejemplo n.º 18
0
    def _configure(self):
        """
        Configure the ssh parameters from the config file.

        The Port and username are extracted from .ssh/config, unless
        overridden by arguments to the constructor.

        If username and or port are provided to `__init__`, they will
        override the values found in the configuration file.


        :raise:
            SSHException under the following conditions:

            * No configuration file is found
            * It does not contain an entry for the Host.
            * It references a keyfile which does not exist
            * The port number is non-numeric or negative
            * Values for port and username can not be determined

        """
        configfile = expanduser("~/.ssh/config")

        if not isfile(configfile):
            raise SSHException("ssh config file '%s' does not exist" %
                               configfile)

        config = SSHConfig()
        config.parse(open(configfile))
        data = config.lookup(self.hostname)

        if not data:
            raise SSHException("No ssh config for host %s" % self.hostname)

        self.hostname = data.get('hostname', None)
        self.proxy_cmd = data.get('proxycommand', None)

        if not self.username:
            self.username = data.get('user', None)

        if self.key_filename:
            self.key_filename = abspath(expanduser(self.key_filename))
        elif 'identityfile' in data:
            self.key_filename = abspath(expanduser(data['identityfile'][0]))

        if self.key_filename and not isfile(self.key_filename):
            raise SSHException("Identity file '%s' does not exist" %
                               self.key_filename)

        if self.port is None:
            try:
                self.port = int(data.get('port', '29418'))
            except ValueError:
                raise SSHException("Invalid port: %s" % data['port'])

        config_data = (self.hostname, self.port, self.username)
        if not all(config_data):
            raise SSHException("Missing configuration data in %s" % configfile)
Ejemplo n.º 19
0
 def scp(self, filename, remote_path):
     config = SSHConfig()
     config.parse(open(os.path.expanduser('~/.ssh/config')))
     o = config.lookup('geodata')
     ssh_client = SSHClient()
     ssh_client.load_system_host_keys()
     ssh_client.connect(o['hostname'], username=o['user'])
     scp = SCPClient(ssh_client.get_transport())
     scp.put(filename, remote_path=remote_path)
Ejemplo n.º 20
0
 def __init__(self, host):
     
     # store the host
     self.host = host
     
     # read the ssh config file
     config = SSHConfig()
     fname = os.path.join(os.environ['HOME'], '.ssh', 'config')
     config.parse(open(fname))
     self.config = config.lookup(host)
Ejemplo n.º 21
0
def get_host_list(config_location):
    config = SSHConfig()
    config.parse(open(config_location))
    host_list = [new['host'][0] for new in config.__dict__['_config'][1:]]
    i = 0
    host_menu = {}
    for host in host_list:
        host_menu[i] = host
        i += 1
    return host_menu
Ejemplo n.º 22
0
def get_ssh_config(path=None):
    """
    Return a ssh configuration parsed from path.

    :param path: The path to the config to parse.
    """
    path = path or '%s/%s' % (os.environ.get('HOME'), '.ssh/config')
    fh = open(path)
    ssh = SSHConfig()
    ssh.parse(fh)
    return ssh
Ejemplo n.º 23
0
 def config(self):
     sshconfig = SSHConfig()
     try:
         sshconfig.parse(open(SSH_CONFIG))
     except IOError:
         sys.stderr.write("Warning: SSH config file location invalid.\n")
     conf = sshconfig.lookup(self.server)
     if 'port' in conf:
         conf['port'] = int(conf['port'])
     
     return conf
Ejemplo n.º 24
0
    def connect(self, host):
        # ssh config file
        config = SSHConfig()
        config.parse(open('%s/.ssh/config' % os.environ['HOME']))
        o = config.lookup(host)

        # ssh client
        self.ssh_client = ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.connect(o['hostname'], username=o['user'], key_filename=o['identityfile'])
        self.sftp_client = ssh.open_sftp()
Ejemplo n.º 25
0
    def __init__(self, host_name: str, vagrant_dir: str = "./") -> None:
        self.host_name = host_name
        self.vagrant_dir = vagrant_dir

        # Initialize paramiko connection and make main connection
        config = SSHConfig()
        config.parse(
            io.StringIO(
                subprocess.check_output(["vagrant", "ssh-config", host_name],
                                        cwd=vagrant_dir).decode()))
        self.ssh_info = config.lookup(host_name)
        self.ssh_client = self.build_ssh_client()
Ejemplo n.º 26
0
 def test_write(self):
     import mock
     sshconf = section_template
     sshconf['host'] = ['test']
     sshconf['config']['hostname'] = 'test.domain.test'
     sshconf['config']['identityfile'] = ['/to/the/key']
     target_sshconf = tempfile.mkstemp()[1]
     with mock.patch.dict(globals(), {'SSH_CONFIG': target_sshconf}):
         write([sshconf])
         config = SSHConfig()
         config.parse(open(target_sshconf))
     self.assertTrue('test' in config._config[1]['host'])
 def test_write(self):
     import mock
     sshconf = section_template
     sshconf['host'] = ['test']
     sshconf['config']['hostname'] = 'test.domain.test'
     sshconf['config']['identityfile'] = ['/to/the/key']
     target_sshconf = tempfile.mkstemp()[1]
     with mock.patch.dict(globals(), {'SSH_CONFIG': target_sshconf}):
         write([sshconf])
         config = SSHConfig()
         config.parse(open(target_sshconf))
     self.assertTrue('test' in config._config[1]['host'])
Ejemplo n.º 28
0
 def config(self):
     sshconfig = SSHConfig()
     try:
         sshconfig.parse(open(SSH_CONFIG))
     except IOError:
         print "your app needs to have a valid " \
               "ssh config file location in settings.py"
         sys.exit(1)
     conf = sshconfig.lookup(self.server)
     if 'port' in conf:
         conf['port'] = int(conf['port'])
     
     return conf
Ejemplo n.º 29
0
Archivo: lit.py Proyecto: gicmo/lit
    def __init__(self):
        path = os.path.expanduser("~/.ssh/config")
        cfg = SSHConfig()
        cfg.parse(open(path))

        ssh = SSHClient()
        ssh.load_system_host_keys()

        (host, params) = self._ssh_cfg2params(cfg)
        ssh.connect(host, **params)

        self._ssh = ssh
        self._sftp = ssh.open_sftp()
Ejemplo n.º 30
0
def read_ssh_config():
    ssh_config_file = os.path.join(os.environ['HOME'], '.ssh', 'config')

    ssh_cfg = None
    try:
        with open(ssh_config_file) as ssh_cfg_fh:
            ssh_cfg = SSHConfig()
            ssh_cfg.parse(ssh_cfg_fh)
    except Exception as err:  # pylint: disable=broad-except
        LOG.warning(
            "Default SSH configuration could not be read: {}".format(err))

    return ssh_cfg
Ejemplo n.º 31
0
 def __init__(self, hostname):
     self.hostname = hostname
     ssh_config = SSHConfig()
     ssh_config.parse(open(expanduser('~/.ssh/config')))
     self.info = ssh_config.lookup(self.hostname)
     try:
         self.user = self.info['user']
     except KeyError:
         raise KeyError(
             'Your SSH Config is missing! Ensure an entry is present for {} in ~/.ssh.config'
             .format(self.hostname))
     self._client = None
     self.password = getpass('Server user password: ')
Ejemplo n.º 32
0
def fetch_ssh_configuration(hostname, directory):
    """
    Fetch the SSH configuration options that Vagrant
    holds for the VM with the specified hostname in
    the local directory.

    :param hostname: name of the Vagrant VM
    :param directory: "location" of the Vagrant VM
    :return: SSH configuration
    """
    config = SSHConfig()
    config.parse(StringIO(unicode(check_output(['vagrant', 'ssh-config', '--host', hostname], cwd=directory))))
    return config.lookup(hostname)
Ejemplo n.º 33
0
    def __init__(self, host=None):
        if host is None:
            raise (ValueError)

        self.ssh_config = SSHConfig()
        self.ssh_config.parse(open(SSH_CONFIG_PATH))
        self.ssh_host = self.ssh_config.lookup(host)

        self.ssh_client = SSHClient()
        self.ssh_client.load_system_host_keys()

        self.ssh_client.connect(self.ssh_host['hostname'],
                                username=self.ssh_host['user'],
                                key_filename=self.ssh_host['identityfile'])
Ejemplo n.º 34
0
def scp_bela(host='bbb'):
    ssh_config = SSHConfig()
    ssh_config_file = os.path.expanduser('~/.ssh/config')
    if os.path.exists(ssh_config_file):
        with open(ssh_config_file) as f:
            ssh_config.parse(f)
    bbb = ssh_config.lookup(host)
    sf = Transport((bbb['hostname'], 22))
    sf.connect(username=bbb['user'])
    sf.auth_none(bbb['user'])
    # progress callback for scp transfer
    # def progress(filename, size, sent, peername):
    #     print("%s:%s %s: %.2f%% \r" % (peername[0], peername[1], filename, float(sent)/float(size)*100))
    # return SCPClient(sf, progress = progress)
    return SCPClient(sf)
Ejemplo n.º 35
0
    def __init__(self, config_location, server_name):

        self.system = {}
        self.system['ssh_hostname'] = server_name

        config = SSHConfig()
        config.parse(open(config_location))
        o = config.lookup(server_name)

        self.ssh_client = SSHClient()
        self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
        self.ssh_client.load_system_host_keys()
        self.ssh_client.connect(o['hostname'],
                                username=o['user'],
                                key_filename=o['identityfile'])
Ejemplo n.º 36
0
 def read_ssh_config( self, hostname, cnf_file = '~/.ssh/config' ):
     """
         Read an ssh config file and store it in this class
         @param cnf_file: The configuration file for ssh
         @type cnf_file: str
         @raise MissingConfigurationFile: If the file does not exist
     """
     cnf_file = os.path.expanduser( cnf_file )
     if os.path.exists( cnf_file ):
         with open( cnf_file ) as fh:
             ssh_config = SSHConfig()
             ssh_config.parse( fh )
             self._ssh_config = ssh_config.lookup( hostname )
     else:
         raise MissingConfigurationFile( cnf_file )
Ejemplo n.º 37
0
 def read_ssh_config(self, hostname, cnf_file='~/.ssh/config'):
     """
         Read an ssh config file and store it in this class
         @param cnf_file: The configuration file for ssh
         @type cnf_file: str
         @raise MissingConfigurationFile: If the file does not exist
     """
     cnf_file = os.path.expanduser(cnf_file)
     if os.path.exists(cnf_file):
         with open(cnf_file) as fh:
             ssh_config = SSHConfig()
             ssh_config.parse(fh)
             self._ssh_config = ssh_config.lookup(hostname)
     else:
         raise MissingConfigurationFile(cnf_file)
def scp_upload(src_blob='Preproc.tar.gz',
               dst_blob="~",
               options={
                   'hostname': 'lecun',
                   'username': '******'
               },
               progress=simple_callback):
    # from https://gist.github.com/acdha/6064215

    #--- Make the Paramiko SSH thing use my .ssh/config file (b/c I like ProxyCommand!)
    client = SSHClient()
    client.load_system_host_keys()
    client._policy = WarningPolicy()
    client.set_missing_host_key_policy(
        WarningPolicy())  # hmm. WarningPolicy? Most people use AutoAddPolicy.

    ssh_config = SSHConfig()
    user_config_file = os.path.expanduser("~/.ssh/config")
    if os.path.exists(user_config_file):
        with open(user_config_file) as f:
            ssh_config.parse(f)

    cfg = {'hostname': options['hostname'], 'username': options["username"]}

    user_config = ssh_config.lookup(cfg['hostname'])
    for k in ('hostname', 'username', 'port'):
        if k in user_config:
            cfg[k] = user_config[k]

    if 'proxycommand' in user_config:
        cfg['sock'] = ProxyCommand(user_config['proxycommand'])

    client.connect(**cfg)

    socket_timeout = None  # number of seconds for timeout. None = never timeout. TODO: None means program may hang. But timeouts are annoying!

    # SCPCLient takes a paramiko transport and progress callback as its arguments.
    scp = SCPClient(client.get_transport(),
                    progress=progress,
                    socket_timeout=socket_timeout)

    # NOW we can finally upload! (in a separate process)
    #scp.put(src_blob, dst_blob)   # now in scp_thread

    # we want this to be non-blocking so we stick it in a thread
    thread = threading.Thread(target=scp_thread,
                              args=(scp, src_blob, dst_blob))
    thread.start()
Ejemplo n.º 39
0
    def test_proxycommand(self):
        config = SSHConfig.from_text("""
Host proxy-with-equal-divisor-and-space
ProxyCommand = foo=bar

Host proxy-with-equal-divisor-and-no-space
ProxyCommand=foo=bar

Host proxy-without-equal-divisor
ProxyCommand foo=bar:%h-%p
""")
        for host, values in {
                "proxy-with-equal-divisor-and-space": {
                    "hostname": "proxy-with-equal-divisor-and-space",
                    "proxycommand": "foo=bar",
                },
                "proxy-with-equal-divisor-and-no-space": {
                    "hostname": "proxy-with-equal-divisor-and-no-space",
                    "proxycommand": "foo=bar",
                },
                "proxy-without-equal-divisor": {
                    "hostname": "proxy-without-equal-divisor",
                    "proxycommand": "foo=bar:proxy-without-equal-divisor-22",
                },
        }.items():

            assert config.lookup(host) == values
Ejemplo n.º 40
0
    def test_identityfile(self):
        config = SSHConfig.from_text("""

IdentityFile id_dsa0

Host *
IdentityFile id_dsa1

Host dsa2
IdentityFile id_dsa2

Host dsa2*
IdentityFile id_dsa22
""")
        for host, values in {
                "foo": {
                    "hostname": "foo",
                    "identityfile": ["id_dsa0", "id_dsa1"]
                },
                "dsa2": {
                    "hostname": "dsa2",
                    "identityfile":
                    ["id_dsa0", "id_dsa1", "id_dsa2", "id_dsa22"],
                },
                "dsa22": {
                    "hostname": "dsa22",
                    "identityfile": ["id_dsa0", "id_dsa1", "id_dsa22"],
                },
        }.items():

            assert config.lookup(host) == values
Ejemplo n.º 41
0
def get_key_for_host(host, index):
    '''returns the path that is the private key for a given host by looking at ~/.ssh/config
    important this only works if there is 1 private key in the config file for a given host'''
    ssh_config = SSHConfig()
    user_config_file = os.path.expanduser("~/.ssh/config")
    if os.path.exists(user_config_file):
        with open(user_config_file) as f:
            ssh_config.parse(f)
    user_config = ssh_config.lookup(host)
    if 'identityfile' in user_config:
        path = os.path.expanduser(user_config['identityfile'][index])
        if not os.path.exists(path):
            raise Exception(
                "Specified IdentityFile " + path + " for " + host + " in ~/.ssh/config not existing anymore.")
        else:
            return path
Ejemplo n.º 42
0
 def __init__(self, host, ssh_config, ssh_key_filepath):
     ssh_config = SSHConfig.from_path(ssh_config)
     ssh_tgt = ssh_config.lookup(host)
     self.host = ssh_tgt.get("hostname")
     self.user = ssh_tgt.get("user")
     self.ssh_key_filepath = ssh_key_filepath
     self.client = None
Ejemplo n.º 43
0
def get_a_ssh_config(box_name):
    """Gives back a map of all the machine's ssh configurations"""

    output = subprocess.check_output(["vagrant", "ssh-config", box_name])
    config = SSHConfig()
    config.parse(StringIO(output))
    host_config = config.lookup(box_name)

    # man 5 ssh_config:
    # > It is possible to have multiple identity files ...
    # > all these identities will be tried in sequence.
    for id in host_config['identityfile']:
        if os.path.isfile(id):
            host_config['identityfile'] = id

    return dict((v, host_config[k]) for k, v in _ssh_to_ansible)
Ejemplo n.º 44
0
def get_a_ssh_config(box_name):
    """Gives back a map of all the machine's ssh configurations"""

    output = subprocess.check_output(["vagrant", "ssh-config", box_name])
    config = SSHConfig()
    config.parse(StringIO(output))
    host_config = config.lookup(box_name)

    # man 5 ssh_config:
    # > It is possible to have multiple identity files ...
    # > all these identities will be tried in sequence.
    for id in host_config['identityfile']:
        if os.path.isfile(id):
            host_config['identityfile'] = id

    return dict((v, host_config[k]) for k, v in _ssh_to_ansible)
    def __create_ssh_client(self):
        ssh_client = SSHClient()
        ssh_config = SSHConfig()
        ssh_config.parse(open(expanduser("~") + "/.ssh/config"))
        ssh_config_properties = ssh_config.lookup(self.host)

        identity_file = ssh_config_properties['identityfile'][0]
        host_name = ssh_config_properties['hostname']
        user_name = ssh_config_properties['user']

        key = RSAKey.from_private_key_file(identity_file)

        ssh_client.set_missing_host_key_policy(AutoAddPolicy())
        ssh_client.connect(hostname=host_name, username=user_name, pkey=key)

        return ssh_client
Ejemplo n.º 46
0
def get_ssh_config(hostname):
    """Get SSH config for given hostname

    :param: hostname: hostname

    :return: dict
    """

    ssh_config_file = path.abspath(path.expanduser('~/.ssh/config'))
    if path.exists(ssh_config_file):
        ssh_config = SSHConfig()
        with open(ssh_config_file) as f:
            ssh_config.parse(f)
            return ssh_config.lookup(hostname)

    return dict()
Ejemplo n.º 47
0
    def makeSshSocket(self, host, port):
        ssh_config = SSHConfig()
        try:
            with open(os.path.expanduser("~/.ssh/config")) as f:
                ssh_config.parse(f)
        except FileNotFoundError:
            pass

        conf = ssh_config.lookup(host)
        proxycommand = conf.get('proxycommand')
        close_pforward = None
        try:
            if proxycommand:
                proxycommand = ' '.join(proxycommand.split())
                match = re.match("ssh (\S+) nc (\S+) (\d+)", proxycommand)
                if match:
                    phost, dhost, dport = match.groups()
                    port = 2222
                    host = "localhost"
                    subprocess.check_call([
                        'ssh', phost, '-N', '-f',
                        '-L', '{}:{}:{}'.format(port, dhost, dport),
                        '-M', '-S', 'gicosf_port_forward',
                    ])
                    print("Port-forward opened")
                    close_pforward = [
                        'ssh',
                        '-S',
                        'gicosf_port_forward',
                        '-O',
                        'exit',
                        phost,
                    ]
                else:
                    print("Warning: proxycommand ignored")
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                sock.connect((host, port))
                yield sock
            finally:
                sock.close()
        finally:
            if close_pforward:
                subprocess.check_call(
                    close_pforward,
                    stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
                )
Ejemplo n.º 48
0
    def connect(self):
        ''' activates the connection object '''

        if not HAVE_PARAMIKO:
            raise errors.AnsibleError("paramiko is not installed")

        user = self.runner.remote_user

        vvv("ESTABLISH CONNECTION FOR USER: %s" % user, host=self.host)



        #SSHConfig() checks
        config = SSHConfig()
        home_directory = os.getenv('HOME')
        config.parse(open(os.path.join(home_directory, '.ssh/config')))
        o = config.lookup(self.host)

        if o.has_key('port'):
            self.port = int(o['port'])
        if o.has_key('hostname'):
            self.host = o['hostname']
        if o.has_key('user'):
            user = o['user']


        #print user,self.host
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        allow_agent = True
        if self.runner.remote_pass is not None:
            allow_agent = False
        try:
            ssh.connect(self.host, username=user, allow_agent=allow_agent, look_for_keys=True,
                key_filename=self.runner.private_key_file, password=self.runner.remote_pass,
                timeout=self.runner.timeout, port=self.port)
        except Exception, e:
            msg = str(e)
            if "PID check failed" in msg:
                raise errors.AnsibleError("paramiko version issue, please upgrade paramiko on the machine running ansible")
            elif "Private key file is encrypted" in msg:
                msg = 'ssh %s@%s:%s : %s\nTo connect as a different user, use -u <username>.' % (
                    user, self.host, self.port, msg)
                raise errors.AnsibleConnectionFailed(msg)
            else:
                raise errors.AnsibleConnectionFailed(msg)
Ejemplo n.º 49
0
    def test_SSHConfig_host_dicts_are_SSHConfigDict_instances(self):
        config = SSHConfig.from_text("""
Host *.example.com
    Port 2222

Host *
    Port 3333
""")
        assert config.lookup("foo.example.com").as_int("port") == 2222
Ejemplo n.º 50
0
    def test_SSHConfig_wildcard_host_dicts_are_SSHConfigDict_instances(self):
        config = SSHConfig.from_text("""
Host *.example.com
    Port 2222

Host *
    Port 3333
""")
        assert config.lookup("anything-else").as_int("port") == 3333
Ejemplo n.º 51
0
    def test_config_addressfamily_and_lazy_fqdn(self):
        """
        Ensure the code path honoring non-'all' AddressFamily doesn't asplode
        """
        config = SSHConfig.from_text("""
AddressFamily inet
IdentityFile something_%l_using_fqdn
""")
        assert config.lookup(
            "meh")  # will die during lookup() if bug regresses
Ejemplo n.º 52
0
Archivo: main.py Proyecto: jukart/pypsh
def start_procs(serial, hosts, starter_func):
    config = SSHConfig()
    config.parse(open(os.path.expanduser('~/.ssh/config')))

    processes = []
    for host in hosts:
        process = starter_func(host, config.lookup(host))
        process.start()
        if serial:
            process.join()
        processes.append(process)

    while multiprocessing.active_children():
        try:
            sleep(0.3)
        except KeyboardInterrupt:
            for p in processes:
                p.stop()
            break
    return processes
Ejemplo n.º 53
0
def get_a_ssh_config(box):
    # call `vagrant ssh-config` with box id
    local.cwd.chdir(box[4])
    vagrant_cmd = local["vagrant"]
    ssh_config_cmd = vagrant_cmd["ssh-config"]
    output = ssh_config_cmd(box[1])
    output = output[output.find("Host "):]

    config = SSHConfig()
    config.parse(StringIO(output))
    host_config = config.lookup(box[1])
    host_config['python_interpreter'] = "PATH=/home/core/bin:$PATH python"

    # man 5 ssh_config:
    # > It is possible to have multiple identity files ...
    # > all these identities will be tried in sequence.
    for identity_file in host_config['identityfile']:
        if os.path.isfile(identity_file):
            host_config['identityfile'] = identity_file

    return {v: host_config[h] for h, v in _ssh_to_ansible}
Ejemplo n.º 54
0
def main(argc, argv):
    global config, is_end, maxthreads
    if (argc < 2):
        usage()
        exit(1)

    parse_options()

    debug("Arguments: maxthreads %d" % (maxthreads))

    config = SSHConfig()
    config.parse(open('/root/.ssh/config'))
    
    hosts_queue = Queue.Queue()
    result_queue = Queue.Queue()
    threads = [
      threading.Thread(target = thread_worker,
        args = (hosts_queue, result_queue, commands))
      for i in range(maxthreads)]

    print "[+] initialisation complete"
    for i in range(maxthreads):
        threads[i].daemon = True
        threads[i].start()
    print "[+] Threads created"
    #print "[+] cmd is %s" % commands
    for host in hosts.splitlines():
        if len(host) == 0: continue
        hosts_queue.put(host)
        print host
    print "[+] Host queue filled"
    #No data for workes avaiable, set finalize flag
    is_end = True
    for t in threads:
        print "wait another thread"
        t.join()
    while not result_queue.empty():
        print result_queue.get()
def main(args):
    config = SSHConfig()
    if os.path.isfile(SSH_CONFIG):
        config.parse(open(SSH_CONFIG))
    else:
        print "[+] %s does not exists. Creating an empty one." % SSH_CONFIG
        file(SSH_CONFIG, 'w').write("")

    if args.delete:
        print "[+] delete section Host %s" % args.alias
        config._config = [
            section for section in config._config if
            args.alias not in section['host']]
    else:
        if (args.hostname and args.alias and
           (args.key_path or args.key_from_stdin)):
            if args.key_from_stdin:
                print "[+] read the key from stdin ..."
                fd, key_path = tempfile.mkstemp(suffix='_%s.key' % args.alias)
                for line in sys.stdin:
                    os.write(fd, line)
                os.fsync(fd)
                key_path = copy_key(key_path)
            else:
                key_path = copy_key(args.key_path)
            print "[+] add section Host %s" % args.alias
            # Clean section with the name
            config._config = [
                section for section in config._config if
                args.alias not in section['host']]
            section = section_template
            section['host'] = [args.alias]
            section['config']['hostname'] = args.hostname
            section['config']['identityfile'] = [key_path]
            config._config.append(section)
    print "[+] write the ssh_config file"
    write(config._config)
Ejemplo n.º 56
0
def read_openssh_config(_host, config_file=None):
    """Parses user's OpenSSH config for per hostname configuration for
    hostname, user, port and private key values

    :param _host: Hostname to lookup in config"""
    _ssh_config_file = config_file if config_file else os.path.sep.join([os.path.expanduser("~"), ".ssh", "config"])
    # Load ~/.ssh/config if it exists to pick up username
    # and host address if set
    if not os.path.isfile(_ssh_config_file):
        return
    ssh_config = SSHConfig()
    ssh_config.parse(open(_ssh_config_file))
    host_config = ssh_config.lookup(_host)
    host = host_config["hostname"] if "hostname" in host_config else _host
    user = host_config["user"] if "user" in host_config else None
    port = int(host_config["port"]) if "port" in host_config else 22
    pkey = None
    # Try configured keys, pick first one that loads
    if "identityfile" in host_config:
        for file_name in host_config["identityfile"]:
            pkey = load_private_key(file_name)
            if pkey:
                break
    return host, user, port, pkey
    def test_main(self):
        import mock
        import copy
        sshconf1 = copy.deepcopy(section_template)
        sshconf1['host'] = ['test1']
        sshconf1['config']['hostname'] = 'test.domain.test'
        sshconf1['config']['identityfile'] = ['/to/the/key']
        sshconf2 = copy.deepcopy(section_template)
        sshconf2['host'] = ['test2']
        sshconf2['config']['hostname'] = 'test.domain.test'
        sshconf2['config']['identityfile'] = ['/to/the/key']
        target_sshconf = tempfile.mkstemp()[1]
        with mock.patch.dict(globals(), {'SSH_CONFIG': target_sshconf}):
            # Create a dummy ssh_config file
            write([sshconf1, sshconf2])

            # Call the app with delete args
            args = mock.MagicMock(delete=True, alias='test1')
            main(args)
            # Read the target file and check content
            config = SSHConfig()
            config.parse(open(target_sshconf))
            self.assertTrue("test2" in [e['host'][0] for
                            e in config._config])
            self.assertTrue("test1" not in [e['host'][0] for
                            e in config._config])

            # Call the app with a add/replace args
            args = mock.MagicMock(alias='backup', hostname='backupserver',
                                  key_path='/tmp/mykey', delete=False,
                                  key_from_stdin=False)
            with mock.patch.dict(globals(),
                                 {"copy_key":
                                  mock.Mock(return_value='/tmp/mykey')}):
                main(args)
            # Read the target file and check content
            config = SSHConfig()
            config.parse(open(target_sshconf))
            self.assertTrue("test2" in [e['host'][0] for
                            e in config._config])
            self.assertTrue("backup" in [e['host'][0] for
                            e in config._config])
Ejemplo n.º 58
0
def ssh_config(ssh_config_filename, name):
    config = SSHConfig()
    with open(ssh_config_filename) as stream:
        config.parse(stream)
    return config.lookup(name)
Ejemplo n.º 59
0
import os

from paramiko import SSHClient, SSHConfig

# ssh config file
config = SSHConfig()
ssh_config = os.path.join(os.path.expanduser('~'), '.ssh/config')
print ssh_config
config.parse(file(ssh_config))
o = config.lookup('zA')

# ssh client
pwd1 = 'uhbnmj^&*'
ssh_client = SSHClient()
ssh_client.load_system_host_keys()
ssh_client.connect(o['hostname'], username=o['user'], password=pwd1)

# run a command
print "\nRun a command"
cmd = 'ps aux'
stdin, stdout, stderr = ssh_client.exec_command(cmd)
for i, line in enumerate(stdout):
    line = line.strip()
    print "%d: %s" % (i, line)
    if i >= 9:
        break

# open a remote file
print "\nOpen a remote file"
sftp_client = ssh_client.open_sftp()
sftp_file = sftp_client.open('/var/log/mail.log')
Ejemplo n.º 60
0
def get_sshConfig():
	sshConfig = SSHConfig()
	sshConfig.parse(open(HOME_SSH_CONFIG))
	return sshConfig