How to use the paramiko.SSHConfig function in paramiko

To help you get started, we’ve selected a few paramiko examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebookarchive / augmented-traffic-control / tests / vagrant.py View on Github external
def sshConfig(self, name):
        p = Popen(['vagrant', 'ssh-config', name],
                  stdout=PIPE,
                  stderr=None,
                  stdin=None,
                  cwd='tests/',
                  )
        p.wait()
        if p.returncode != 0:
            raise RuntimeError('Could not get ssh-config for ' + repr(name))
        ssh_config = paramiko.SSHConfig()
        ssh_config.parse(p.stdout)
        p.stdout.close()
        return ssh_config.lookup(name)
github fabric / fabric / tests / _util.py View on Github external
def __init__(self, *args, **kwargs):
        wat = "You're giving ssh_config explicitly, please use Config_!"
        assert "ssh_config" not in kwargs, wat
        # Give ssh_config explicitly -> shorter way of turning off loading
        kwargs["ssh_config"] = SSHConfig()
        super(Config, self).__init__(*args, **kwargs)
github aiidateam / aiida-core / examples / submission / test_templatereplacer.py View on Github external
#but by 'name' only.
            computer = Computer.get(machine)
            print >> sys.stderr, "Using the existing computer {}...".format(computername)
        except NotExistent:
            print >> sys.stderr, "Creating a new computer..."
            computer = Computer(hostname=computername,transport_type='ssh',
                                scheduler_type=schedulertype)
            computer.set_workdir(workdir)
            #computer.set_mpirun_command(mpirun_command)
            computer.store()

        auth_params = {'load_system_host_keys': True,
                       'compress': True}


        config = paramiko.SSHConfig()
        try:
            config.parse(open(os.path.expanduser('~/.ssh/config')))
        except IOError:
            # No file found, so empty configuration
            pass
        # machine_config is a dict with only relevant properties set
        machine_config = config.lookup(computername)

        try:
            auth_params['username'] = machine_config['user']
        except KeyError:
            # No user set up in the config file: I explicitly set the local username
            auth_params['username'] = getpass.getuser()

        try:
            auth_params['key_filename'] = os.path.expanduser(
github bestephe / loom / exps / tc-test / tc_test_common.py View on Github external
def connect_rhost(rhost):
    rssh = paramiko.SSHClient()
    rssh.load_system_host_keys()
    rssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh_config = paramiko.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': rhost, 'username': options["username"]}
    cfg = {'hostname': rhost}

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

    if 'proxycommand' in user_config:
github robotframework / SSHLibrary / src / SSHLibrary / pythonclient.py View on Github external
def _read_ssh_config_host(host):
        ssh_config_file = os.path.expanduser("~/.ssh/config")
        if os.path.exists(ssh_config_file):
            conf = paramiko.SSHConfig()
            with open(ssh_config_file) as f:
                conf.parse(f)
            return conf.lookup(host)['hostname'] if not None else host
        return host
github saltycrane / remote-tools / multilog.py View on Github external
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()
github flaper87 / python-gerrit / gerrit / ssh.py View on Github external
def __init__(self, host, port=29418, user=None,
                 key=None, config="~/.ssh/config"):

        self.key = key
        self.host = host
        self.port = port
        self.user = user

        config = os.path.expanduser(config)
        if os.path.exists(config):
            ssh = paramiko.SSHConfig()
            ssh.parse(open(config))
            conf = ssh.lookup(host)

            self.host = conf['hostname']
            self.port = int(conf.get('port', self.port))
            self.user = conf.get('user', self.user)
            self.key = conf.get('identityfile', self.key)
github dpursehouse / pygerrit2 / pygerrit / ssh.py View on Github external
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'])
github dolph / next-review / next_review.py View on Github external
def merge_ssh_config(args):
    """Merge the local SSH config into next-review's config."""
    ssh_config = paramiko.SSHConfig()

    try:
        ssh_config.parse(open(os.path.expanduser('~/.ssh/config')))
    except IOError:
        # The user does not have an SSH config file (FileNotFoundError on py3),
        # so just bail.
        return

    host_config = ssh_config.lookup(args.host)

    if 'user' in host_config and not args.username:
        args.username = host_config['user']
    if 'identityfile' in host_config and not args.key:
        args.key = host_config['identityfile']