Example #1
0
    def __init__(
        self,
        remote_host,
        remote_port,
        username=None,
        password=None,
        key_file=None,
        timeout=10,
        keepalive_interval=30,
        compress=True,
        no_host_key_check=True,
        allow_host_key_change=False,
        logger=None,
    ):
        self.remote_host = check.str_param(remote_host, 'remote_host')
        self.remote_port = check.opt_int_param(remote_port, 'remote_port')
        self.username = check.opt_str_param(username, 'username')
        self.password = check.opt_str_param(password, 'password')
        self.key_file = check.opt_str_param(key_file, 'key_file')
        self.timeout = check.opt_int_param(timeout, 'timeout')
        self.keepalive_interval = check.opt_int_param(keepalive_interval,
                                                      'keepalive_interval')
        self.compress = check.opt_bool_param(compress, 'compress')
        self.no_host_key_check = check.opt_bool_param(no_host_key_check,
                                                      'no_host_key_check')
        self.allow_host_key_change = check.opt_bool_param(
            allow_host_key_change, 'allow_host_key_change')
        self.log = logger

        self.host_proxy = None

        # Auto detecting username values from system
        if not self.username:
            logger.debug(
                'username to ssh to host: %s is not specified. Using system\'s default provided by'
                ' getpass.getuser()' % self.remote_host)
            self.username = getpass.getuser()

        user_ssh_config_filename = os.path.expanduser('~/.ssh/config')
        if os.path.isfile(user_ssh_config_filename):
            ssh_conf = paramiko.SSHConfig()
            ssh_conf.parse(open(user_ssh_config_filename))
            host_info = ssh_conf.lookup(self.remote_host)
            if host_info and host_info.get('proxycommand'):
                self.host_proxy = paramiko.ProxyCommand(
                    host_info.get('proxycommand'))

            if not (self.password or self.key_file):
                if host_info and host_info.get('identityfile'):
                    self.key_file = host_info.get('identityfile')[0]
Example #2
0
    def _use_ssh_config(self, dict_arg):
        """
		Update SSH connection parameters based on contents of SSH 'config' file.

		:param dict_arg: Dictionary of SSH connection parameters
		:type dict_arg: dict
		"""
        connect_dict = dict_arg.copy()

        # Use SSHConfig to generate source content.
        full_path = path.abspath(path.expanduser(self.ssh_config_file))
        if path.exists(full_path):
            ssh_config_instance = paramiko.SSHConfig()
            with io.open(full_path, "rt", encoding='utf-8') as f:
                ssh_config_instance.parse(f)
                source = ssh_config_instance.lookup(self.host)
        else:
            source = {}

        if source.get('proxycommand'):
            proxy = paramiko.ProxyCommand(source['proxycommand'])
        elif source.get('ProxyCommand'):
            proxy = paramiko.ProxyCommand(source['proxycommand'])
        else:
            proxy = None

        # Only update 'hostname', 'sock', 'port', and 'username'
        # For 'port' and 'username' only update if using object defaults
        if connect_dict['port'] == 22:
            connect_dict['port'] = int(source.get('port', self.port))
        if connect_dict['username'] == '':
            connect_dict['username'] = source.get('username', self.username)
        if proxy:
            connect_dict['sock'] = proxy
        connect_dict['hostname'] = source.get('hostname', self.host)

        return connect_dict
Example #3
0
def execute_command_via_gateway(cmd,
                                host,
                                username,
                                private_key_filename,
                                gateway_host,
                                gateway_username=None,
                                proxy_command=None,
                                password=None,
                                private_key=None):
    LOG.debug('Creating SSH connection')

    private_key = _to_paramiko_private_key(private_key_filename, private_key,
                                           password)

    proxy = None

    if proxy_command:
        LOG.debug('Creating proxy using command: %s', proxy_command)

        proxy = paramiko.ProxyCommand(proxy_command)

    _proxy_ssh_client = paramiko.SSHClient()
    _proxy_ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    LOG.debug('Connecting to proxy gateway at: %s', gateway_host)

    if not gateway_username:
        gateway_username = username

    _proxy_ssh_client.connect(gateway_host,
                              username=gateway_username,
                              pkey=private_key,
                              sock=proxy)

    proxy = _proxy_ssh_client.get_transport().open_session()
    proxy.exec_command("nc {0} 22".format(host))

    ssh_client = _connect(host,
                          username=username,
                          pkey=private_key,
                          proxy=proxy)

    try:
        return _execute_command(ssh_client,
                                cmd,
                                get_stderr=False,
                                raise_when_error=True)
    finally:
        _cleanup(_proxy_ssh_client)
Example #4
0
def node_shell(hostname, siteUser, sitePasswd, connectTimeout, cmdTimeout):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        if not args.proxyPort:
            ssh.connect(hostname,
                        username=siteUser,
                        password=sitePasswd,
                        timeout=connectTimeout,
                        banner_timeout=connectTimeout,
                        look_for_keys=False)
            logger.debug('Connected: %s, %s, %s, proxy disabled' %
                         (hostname, siteUser, connectTimeout))
        else:
            proxyPort = int(args.proxyPort)
            proxyCommandStr = "nc -X 5 -x localhost:%s %s %s" % (
                proxyPort, hostname, '22')
            proxySock = paramiko.ProxyCommand(proxyCommandStr)
            ssh.connect(hostname,
                        username=siteUser,
                        password=sitePasswd,
                        timeout=connectTimeout,
                        banner_timeout=connectTimeout,
                        sock=proxySock,
                        look_for_keys=False)
            logger.debug('Connected: %s, %s, %s, proxy enabled' %
                         (hostname, siteUser, connectTimeout))
        transport = ssh.get_transport()
        transport.set_keepalive(0)

        if args.commandFile:
            try:
                cmds = open(args.commandFile)
                cmds = [cmd.strip() for cmd in cmds]
            except Exception as e:
                logger.error(e)
        else:
            cmd = args.commandString
            cmds = [cmd]

        run_cmds(ssh, cmds, hostname, sitePasswd, cmdTimeout)

        successful_logins.append(hostname)

    except Exception as e:
        ssh.close()
        logger.debug('ssh closed')
        logger.error('%s: failed to login : %s' % (hostname, e))
        failed_logins.append(hostname)
Example #5
0
 def _login_with_public_key(self,
                            username,
                            key_file,
                            password,
                            allow_agent,
                            look_for_keys,
                            proxy_cmd=None,
                            jumphost_connection=None,
                            read_config_host=False):
     if read_config_host:
         self.config.host = self._read_ssh_config_host(self.config.host)
     try:
         sock_tunnel = None
         if proxy_cmd and not jumphost_connection:
             sock_tunnel = paramiko.ProxyCommand(proxy_cmd)
         elif jumphost_connection and not proxy_cmd:
             dest_addr = (self.config.host, self.config.port)
             jump_addr = (jumphost_connection.config.host,
                          jumphost_connection.config.port)
             jumphost_transport = jumphost_connection.client.get_transport()
             if not jumphost_transport:
                 raise RuntimeError(
                     "Could not get transport for {}:{}. Have you logged in?"
                     .format(*jump_addr))
             sock_tunnel = jumphost_transport.open_channel(
                 "direct-tcpip", dest_addr, jump_addr)
         elif proxy_cmd and jumphost_connection:
             raise ValueError(
                 "`proxy_cmd` and `jumphost_connection` are mutually exclusive SSH features."
             )
         self.client.connect(self.config.host,
                             self.config.port,
                             username,
                             password,
                             key_filename=key_file,
                             allow_agent=allow_agent,
                             look_for_keys=look_for_keys,
                             timeout=float(self.config.timeout),
                             sock=sock_tunnel)
     except paramiko.AuthenticationException:
         try:
             transport = self.client.get_transport()
             try:
                 transport.auth_none(username)
             except:
                 pass
             transport.auth_publickey(username, None)
         except Exception as err:
             raise SSHClientException
    def open(
        self,
        hostname: Optional[str],
        username: Optional[str],
        password: Optional[str],
        port: Optional[int],
        platform: Optional[str],
        extras: Optional[Dict[str, Any]] = None,
        configuration: Optional[Config] = None,
    ) -> None:
        extras = extras or {}

        client = paramiko.SSHClient()
        client._policy = paramiko.WarningPolicy()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        ssh_config = paramiko.SSHConfig()
        ssh_config_file = configuration.ssh.config_file  # type: ignore
        if os.path.exists(ssh_config_file):
            with open(ssh_config_file) as f:
                ssh_config.parse(f)
        parameters = {
            "hostname": hostname,
            "username": username,
            "password": password,
            "port": port,
        }

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

        if "proxycommand" in user_config:
            parameters["sock"] = paramiko.ProxyCommand(
                user_config["proxycommand"])

        self.state["ssh_forward_agent"] = user_config.get(
            "forwardagent") == "yes"

        # TODO configurable
        #  if ssh_key_file:
        #      parameters['key_filename'] = ssh_key_file
        if "identityfile" in user_config:
            parameters["key_filename"] = user_config["identityfile"]

        extras.update(parameters)
        client.connect(**extras)
        self.connection = client
Example #7
0
def _connect(host, username, private_key, proxy_command=None):
    global _ssh

    LOG.debug('Creating SSH connection')
    if type(private_key) in [str, unicode]:
        private_key = crypto.to_paramiko_private_key(private_key)
    _ssh = paramiko.SSHClient()
    _ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    proxy = None
    if proxy_command:
        LOG.debug('creating proxy using command: {0}'.format(proxy_command))
        proxy = paramiko.ProxyCommand(proxy_command)

    _ssh.connect(host, username=username, pkey=private_key, sock=proxy)
Example #8
0
def secure_copy(user, host, src, dest, key_filename=None, allow_agent=True):
    keys = _load_keys(key_filename, allow_agent)
    pkey = keys[0]
    ssh = paramiko.SSHClient()
    conf = paramiko.SSHConfig()
    ssh_config_file = os.path.expanduser("~/.ssh/config")
    conf.parse(open(ssh_config_file))
    host_config = conf.lookup(host)
    proxy = paramiko.ProxyCommand(host_config['proxycommand'])
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=user, pkey=pkey, sock=proxy)
    scp = SCPClient(ssh.get_transport())
    scp.get(src, dest)
    scp.close()
Example #9
0
def test_client(host_name):
    conf = paramiko.SSHConfig()
    conf.parse(open(os.path.expanduser('~/.ssh/config')))
    host = conf.lookup(host_name)
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host['hostname'],
                   username=host['user'],
                   key_filename=host['identityfile'],
                   sock=paramiko.ProxyCommand(host.get('proxycommand')))
    stdin, stdout, stderr = client.exec_command('ls /home')
    print stdout.read()
    stdin, stdout, stderr = client.exec_command('ls /')
    print stdout.read()
Example #10
0
def ssh_config_run(host, cmd):
    ssh = paramiko.SSHClient()
    ssh.load_system_host_keys()
    ssh._policy = paramiko.WarningPolicy()
    #ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh_config = paramiko.SSHConfig()
    user_config_file = os.path.expanduser("~/.ssh/config")
    try:
        with open(user_config_file) as f:
            ssh_config.parse(f)
    except:
        print("{} file could not be found. Aborting.".format(user_config_file))
        sys.exit(1)
    user_config = ssh_config.lookup(host)
    cfg = {
        'hostname': user_config['hostname'],
        'username': user_config['user'],
        'look_for_keys': False
    }

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

    ssh.connect(**cfg)
    for m in cmd:
        print(host, m)
        if m[:4] == 'sudo':
            transport = ssh.get_transport()
            session = transport.open_session()
            session.set_combine_stderr(True)
            session.get_pty()
            session.exec_command(m)
            stdin = session.makefile('wb', -1)
            stdout = session.makefile('rb', -1)
            stdin.write('Ranchyang!1\n')
            stdin.flush()
            for line in stdout.read().splitlines():
                print(host, line)
            continue
        stdin, stdout, stderr = ssh.exec_command(m)
        outt = stdout.readlines()
        for o in outt:
            print(host, 'out', o)
        err = stderr.readlines()
        for e in err:
            print(host, 'err', e)
    ssh.close()
Example #11
0
    def LIST_selectST(self):
        import paramiko

        client = paramiko.SSHClient()
        ssh_config = paramiko.SSHConfig()

        client._policy = paramiko.WarningPolicy()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        user_config_file = os.path.expanduser(self.CONFIG_FILE)
        try:
            with open(user_config_file) as f:
                ssh_config.parse(f)
        except FileNotFoundError:
            print("{} file could not be found. Aborting.".format(
                user_config_file))
            sys.exit(1)

        user_config = ssh_config.lookup(self.IP)
        if 'proxycommand' in user_config:
            user_config['sock'] = paramiko.ProxyCommand(
                user_config['proxycommand'])
            del user_config['proxycommand']
        if 'user' in user_config:
            user_config['username'] = user_config['user']
            del user_config['user']

        client.connect(**user_config)
        stdin, stdout, stderr = client.exec_command('df -l \
					| awk \' !/maha/ {if(length($2)> 9 && NR!=1) \
					print substr($0,index($0,$2))}\'')

        TOTAL_LIST = []
        LEN = 5
        for line in [line.strip().split() for line in stdout.readlines()]:
            stdin2, stdout2, stderr2 = client.exec_command(
                'find %s -maxdepth 1 -type d ! -name \'lost+found\' \
				| sed 1d \
				|awk \'{split($0,a,"/"); print a[length(a)]}\'' % line[-1])

            all_list = [a.strip() for a in stdout2.readlines()]
            if len(all_list) >= LEN:
                all_list = all_list[:LEN - 1] + ['...']

            line.append(', '.join(all_list))
            TOTAL_LIST.append(line)

        return TOTAL_LIST
Example #12
0
 def open(self):
     self._client = SSHClient()
     self._client.load_system_host_keys()
     self._client.set_missing_host_key_policy(WarningPolicy())
     print(f'Connecting to {self.username}@{self.hostname}')
     if self.proxy_command:
         print('ProxyCommand:', self.proxy_command)
     sock = (paramiko.ProxyCommand(self.proxy_command)
             if self.proxy_command else None)
     try:
         self._client.connect(hostname=self.hostname,
                              username=self.username, sock=sock,
                              key_filename=self.key_filenames)
     except paramiko.ssh_exception.BadHostKeyException:
         sys.exit('Connection error: bad host key')
     self._transport = self._client.get_transport()
Example #13
0
    def get_ssh_client(self, host_name, ssh_config_path):
        conf = paramiko.SSHConfig()
        conf.parse(open(os.path.expanduser(ssh_config_path)))
        host = conf.lookup(host_name)
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(
            host['hostname'],
            username=host['user'],
            # key_filename=host['identityfile'],
            sock=paramiko.ProxyCommand(host.get('proxycommand')))

        print('CREATE SSH CLIENT')

        return client
Example #14
0
def sendDirsToIMR(dirs, vaspfiles):

    print("type your passwd for IMR supercomputer node")
    passwd = getpass()
    config_file = os.path.join(os.getenv('HOME'), '.ssh/config')
    ssh_config = paramiko.SSHConfig()
    ssh_config.parse(open(config_file, 'r'))
    lkup = ssh_config.lookup('super.imr')

    # ProxyCommand setup
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    print("what found in config file")
    print(lkup)

    ssh.connect(lkup['hostname'],
                username=lkup['user'],
                sock=paramiko.ProxyCommand(lkup['proxycommand']),
                password=passwd)

    sftp = ssh.open_sftp()
    workingdir = '/home/emi0716/work/'
    for i in dirs:
        mkdircommand = 'mkdir ' + workingdir + i
        stdin, stdout, stderr = ssh.exec_command(mkdircommand)
        outlines = stdout.readlines()
        result = ''.join(outlines)
        print("making dirs:" + result)

    for i in dirs:

        #TODO catch stderr
        for f in vaspfiles:
            origin = "./" + i + "/" + f
            print("origin:" + origin)
            #print(os.path.exists(origin))
            target = workingdir + i + "/" + f
            print("target:" + target)
            sftp.put(origin, target)

    with open("alldirs-" + str(datetime.date.today()) + ".txt", "w") as f:
        for i in dirs:
            f.write(str(workingdir + i) + '\n')

    sftp.close()
    ssh.close()
Example #15
0
def connect(hostname, port, ssh_key_file, passphrase, user):
    # If we are in a localhost case, don't play with ssh
    if is_local(hostname):
        return LocalExec()

    # Maybe paramiko is missing, but now we relly need ssh...
    if paramiko is None:
        print "ERROR : this plugin needs the python-paramiko module. Please install it"
        sys.exit(2)

    if not os.path.exists(os.path.expanduser(ssh_key_file)):
        err = "Error : missing ssh key file. please specify it with -i parameter"
        raise Exception(err)

    ssh_key_file = os.path.expanduser(ssh_key_file)
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.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': hostname,
        'port': port,
        'username': user,
        'key_filename': ssh_key_file,
        'password': passphrase
    }

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

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

    try:
        client.connect(**cfg)
    except Exception, exp:
        err = "Error : connexion failed '%s'" % exp
        print err
        sys.exit(2)
Example #16
0
    def _parse_proxy_command(self, port=22):
        proxy_command = None
        # Parse ansible_ssh_common_args, specifically looking for ProxyCommand
        ssh_args = [
            getattr(self._play_context, 'ssh_extra_args', '') or '',
            getattr(self._play_context, 'ssh_common_args', '') or '',
            getattr(self._play_context, 'ssh_args', '') or '',
        ]
        if ssh_args is not None:
            args = self._split_ssh_args(' '.join(ssh_args))
            for i, arg in enumerate(args):
                if arg.lower() == 'proxycommand':
                    # _split_ssh_args split ProxyCommand from the command itself
                    proxy_command = args[i + 1]
                else:
                    # ProxyCommand and the command itself are a single string
                    match = SETTINGS_REGEX.match(arg)
                    if match:
                        if match.group(1).lower() == 'proxycommand':
                            proxy_command = match.group(2)

                if proxy_command:
                    break

        proxy_command = proxy_command or self.get_option('proxy_command')

        sock_kwarg = {}
        if proxy_command:
            replacers = {
                '%h': self._play_context.remote_addr,
                '%p': port,
                '%r': self._play_context.remote_user
            }
            for find, replace in replacers.items():
                proxy_command = proxy_command.replace(find, str(replace))
            try:
                sock_kwarg = {'sock': paramiko.ProxyCommand(proxy_command)}
                display.vvv("CONFIGURE PROXY COMMAND FOR CONNECTION: %s" %
                            proxy_command,
                            host=self._play_context.remote_addr)
            except AttributeError:
                display.warning('Paramiko ProxyCommand support unavailable. '
                                'Please upgrade to Paramiko 1.9.0 or newer. '
                                'Not using configured ProxyCommand')

        return sock_kwarg
Example #17
0
    def __init__(self,
                 base_url,
                 timeout=60,
                 pool_connections=constants.DEFAULT_NUM_POOLS,
                 *args,
                 **kwargs):
        logging.getLogger("paramiko").setLevel(logging.WARNING)

        self.ssh_client = paramiko.SSHClient()
        base_url = six.moves.urllib_parse.urlparse(base_url)
        self.ssh_params = {
            "hostname": base_url.hostname,
            "port": base_url.port,
            "username": base_url.username
        }
        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)
            host_config = conf.lookup(base_url.hostname)
            self.ssh_conf = host_config
            if 'proxycommand' in host_config:
                self.ssh_params["sock"] = paramiko.ProxyCommand(
                    self.ssh_conf['proxycommand'])
            if 'hostname' in host_config:
                self.ssh_params['hostname'] = host_config['hostname']
            if base_url.port is None and 'port' in host_config:
                self.ssh_params['port'] = self.ssh_conf['port']
            if base_url.username is None and 'user' in host_config:
                self.ssh_params['username'] = self.ssh_conf['user']

        if "pkey_str" in kwargs:
            self.ssh_params["pkey"] = paramiko.RSAKey.from_private_key(
                io.StringIO(kwargs.get("pkey_str")))
        if "pkey" in kwargs:
            self.ssh_params["pkey"] = kwargs.get("pkey")
        self.ssh_client.load_system_host_keys()
        self.ssh_client.set_missing_host_key_policy(
            kwargs.get("hostKeyPolicy", paramiko.WarningPolicy()))

        self._connect()
        self.timeout = timeout
        self.pools = RecentlyUsedContainer(pool_connections,
                                           dispose_func=lambda p: p.close())
        super(SSHHTTPAdapter, self).__init__()
Example #18
0
    def __init__(self, **config):
        super().__init__(**config)
        url = config.get("url")
        if url:
            parsed = urlparse(url)
            user_ssh_config = self._load_user_ssh_config(parsed.hostname)

            host = user_ssh_config.get("hostname", parsed.hostname)
            user = (
                config.get("user")
                or parsed.username
                or user_ssh_config.get("user")
                or getpass.getuser()
            )
            port = (
                config.get("port")
                or parsed.port
                or self._try_get_ssh_config_port(user_ssh_config)
                or self.DEFAULT_PORT
            )
            self.path_info = self.PATH_CLS.from_parts(
                scheme=self.scheme,
                host=host,
                user=user,
                port=port,
                path=parsed.path,
            )
        else:
            self.path_info = None
            user_ssh_config = {}

        self.keyfile = config.get(
            "keyfile"
        ) or self._try_get_ssh_config_keyfile(user_ssh_config)
        self.timeout = config.get("timeout", self.TIMEOUT)
        self.password = config.get("password", None)
        self.ask_password = config.get("ask_password", False)
        self.gss_auth = config.get("gss_auth", False)
        proxy_command = user_ssh_config.get("proxycommand", False)
        if proxy_command:
            import paramiko

            self.sock = paramiko.ProxyCommand(proxy_command)
        else:
            self.sock = None
        self.allow_agent = config.get("allow_agent", True)
Example #19
0
 def fill_in_srvdict(self):
     ssh_config = paramiko.SSHConfig()
     if os.path.exists(self.__user_config_file):
         with open(self.__user_config_file) as f:
             ssh_config.parse(f)
             for srv in self.srvlist:
                 host=ssh_config.lookup(srv)
                 if 'hostname' in host:
                     hostname=host['hostname'].replace('\t',' ')
                 else:
                     hostname=srv
                 if 'proxycommand' in host:
                     proxycmd=host['proxycommand'].replace('\t',' ')
                     proxy=paramiko.ProxyCommand(proxycmd)
                 else:
                     proxy=None
                 self.srvdict[srv]=dict(dict(hostname=hostname,proxy=proxy))
Example #20
0
    def connect(self):
        """Connect to ssh server."""
        if not self.closed:
            raise RuntimeError('SSH is already opened')

        sock = paramiko.ProxyCommand(self._proxy_cmd) \
            if self._proxy_cmd else None

        self._ssh = paramiko.SSHClient()
        self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self._ssh.connect(self._host,
                          self._port,
                          pkey=self._pkey,
                          timeout=self._timeout,
                          banner_timeout=self._timeout,
                          username=self._username,
                          password=self._password,
                          sock=sock)
Example #21
0
def _get_ssh_client(self):
    config = paramiko.SSHConfig()
    config.parse(open(os.path.expanduser('~/.ssh/config')))
    host = config.lookup(self.inputs.hostname)
    if 'proxycommand' in host:
        proxy = paramiko.ProxyCommand(
            subprocess.check_output(
                [os.environ['SHELL'], '-c',
                 'echo %s' % host['proxycommand']]).strip())
    else:
        proxy = None
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host['172.16.37.254'],
                   username=host['grant.stokley'],
                   sock=proxy)
    return client
    def __init__(self, metapath, clean_remote=False, retrieve=True):
        self.metadata_path = Path(metapath)
        if not self.metadata_path.is_file():
            raise FileNotFoundError("Cluster run metadata file not found: {}".format(self.metadata_path.as_posix()))
        self.metadata = self.load_metadata()
        submission_time = datetime.strptime(self.metadata['timestamp'], '%Y-%m-%d %H:%M:%S')
        time_diff = datetime.now() - submission_time
        # let's wait for at least 30 seconds after job submissions and then look for results
        while time_diff.seconds < 5:
            time.sleep(1)
            time_diff = datetime.now() - submission_time
        self.client = paramiko.SSHClient()
        self.client.load_system_host_keys()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        if 'CLUSTERPASS' in os.environ.keys():
            self.password = os.environ['CLUSTERPASS']
        else:
            self.password = getpass.getpass('Enter password for user {}: '.format(self.metadata['cluster_username']))
        if self.metadata['cluster_login_node'] != '--':
            print(" -  Connecting to login node {}".format(self.metadata['cluster_login_node']))
            sock = paramiko.ProxyCommand("ssh {}@{} nc {} 22".format(self.metadata['cluster_username'],
                                                                     self.metadata['cluster_login_node'],
                                                                     self.metadata['cluster_address']))
            sock.settimeout(30)
            self.client.connect(self.metadata['cluster_address'],
                                port=22,
                                username=self.metadata['cluster_username'],
                                password=self.password,
                                sock=sock)
        else:
            self.client.connect(self.metadata['cluster_address'],
                                port=22,
                                username=self.metadata['cluster_username'],
                                password=self.password)

        self.scpclient = SCPClient(self.client.get_transport())
        time.sleep(1)
        if not Path(self.metadata['local_workspace']).is_dir():
            os.mkdir(self.metadata['local_workspace'])
        if retrieve:
            self.retrieve()
        if clean_remote:
            self.clean_remote()
        self.client.close()
Example #23
0
def ssh_client(host):
    """Start an ssh client.

    The ssh client will attempt to use configs from the user's local ssh config in ~/.ssh/config.

    :param host: the host
    :type host: str
    :returns: ssh client
    :rtype: Paramiko client
    """
    client = paramiko.SSHClient()
    client._policy = paramiko.WarningPolicy()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # parse local user ssh config
    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': host, 'username': getpass.getuser()}

    user_config = ssh_config.lookup(cfg['hostname'])

    logging.debug("Local user ssh config for host {host}: {user_config}".format(host=host, user_config=user_config))

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

    if 'user' in user_config:
        cfg['username'] = user_config['user']

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

    if 'identityfile' in user_config:
        cfg['key_filename'] = user_config['identityfile']

    logging.debug("Overriden ssh config for {host}: {cfg}".format(host=host, cfg=cfg))

    client.connect(**cfg)
    return client
Example #24
0
def ssh_proxy_sock(hostname=None,
                   port=None,
                   command=None,
                   client=None,
                   source_address=None,
                   timeout=None,
                   connection_attempts=None,
                   connection_interval=None):
    if not command:
        if client:
            # I need a command to execute with proxy client
            options = []
            if source_address:
                options += ['-s', str(source_address)]
            command = ['nc'] + options + ['{hostname!s}', '{port!s}']
        elif not command:
            # Proxy sock is not required
            return None

    # Apply connect parameters to proxy command
    if not isinstance(command, str):
        command = subprocess.list2cmdline(command)
    if hostname:
        command = command.format(hostname=hostname, port=(port or 22))
    if client:
        if isinstance(client, SSHClientFixture):
            # Connect to proxy server
            client = client.connect(connection_timeout=timeout,
                                    connection_attempts=connection_attempts,
                                    connection_interval=connection_interval)
        elif not isinstance(client, paramiko.SSHClient):
            message = "Object {!r} is not an SSHClient".format(client)
            raise TypeError(message)

        # Open proxy channel
        LOG.debug("Execute proxy command with proxy client %r: %r", client,
                  command)
        sock = client.get_transport().open_session(timeout=timeout)
        sock.exec_command(command)
    else:
        LOG.debug("Execute proxy command on local host: %r", command)
        sock = paramiko.ProxyCommand(command)

    return sock
Example #25
0
def getSSHConnection(ttip, ip, prot, name, pwd):

    # setup SSH client
    client = SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # #Check for proxy settings
    try:
        proxy = paramiko.ProxyCommand("ssh -o StrictHostKeyChecking=no " +
                                      ttuser + "@" + ttip + " nc " + ip + " " +
                                      ttport)
    except:
        print 1
    #Setup the SSH connection
    try:
        client.connect(ip, prot, username=name, password=pwd, sock=proxy)
    except SSHException, ex:
        print ex
Example #26
0
    def _login(self, username, password, allow_agent=False, look_for_keys=False, proxy_cmd=None,
               read_config_host=False, jumphost_connection=None):
        if read_config_host:
            self.config.host = self._read_ssh_config_host(self.config.host)
        sock_tunnel = None

        if proxy_cmd and jumphost_connection:
            raise ValueError("`proxy_cmd` and `jumphost_connection` are mutually exclusive SSH features.")
        elif proxy_cmd:
            sock_tunnel = paramiko.ProxyCommand(proxy_cmd)
        elif jumphost_connection:
            sock_tunnel = self._get_jumphost_tunnel(jumphost_connection)

        try:
            if not password and not allow_agent:
                # If no password is given, try login without authentication
                try:
                    self.client.connect(self.config.host, self.config.port, username,
                                        password, look_for_keys=look_for_keys,
                                        allow_agent=allow_agent,
                                        timeout=float(self.config.timeout), sock=sock_tunnel)
                except paramiko.SSHException:
                    pass
                transport = self.client.get_transport()
                transport.auth_none(username)
            else:
                try:
                    self.client.connect(self.config.host, self.config.port, username,
                                        password, look_for_keys=look_for_keys,
                                        allow_agent=allow_agent,
                                        timeout=float(self.config.timeout), sock=sock_tunnel)
                except paramiko.AuthenticationException:
                    try:
                        transport = self.client.get_transport()
                        try:
                            transport.auth_none(username)
                        except:
                            pass
                        transport.auth_password(username, password)
                    except:
                        raise SSHClientException
        except paramiko.AuthenticationException:
            raise SSHClientException
Example #27
0
    def open_ssh(self, id):
        try:
            os.system("rm ~/.ssh/known_hosts > /dev/null 2>&1")
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ip = str(self.store.get_attr("base", id, "base.net.ip")).strip()
            password = str(self.store.get_attr(
                "base", id, "base.ssh.password")).strip()
            username = str(self.store.get_attr(
                "base", id, "base.ssh.user")).strip()
            port = self.store.get_attr(
                "base", id, "base.ssh.port")
            tunnel = self.find_tunnel(id)
            if tunnel != None:
                tip = str(self.store.get_attr(
                    "base", tunnel, "base.tunnel.ip")).strip()
                tport = self.store.get_attr(
                    "base", tunnel, "base.tunnel.port")
                tuser = str(self.store.get_attr(
                    "base", tunnel, "base.tunnel.user")).strip()
                tpass = str(self.store.get_attr(
                    "base", tunnel, "base.tunnel.password")).strip()
                source = "sshpass -p"
                source += tpass.replace("!", "\\!")
                source += " ssh -p "
                source += ("22" if tport == None or tport ==
                           "" else str(tport))
                source += " -o StrictHostKeyChecking=no "
                source += tuser
                source += "@"
                source += tip
                source += " nc -w 15 "+ip+" " + \
                    ("22" if port == None or port == "" else str(port))
                proxy = paramiko.ProxyCommand(source)
                ssh.connect(ip, username=username,
                            password=password, sock=proxy, timeout=5)
            else:
                ssh.connect(ip, username=username,
                            password=password, timeout=5)

            return ssh
        except:
            return None
Example #28
0
def jobSubmitIMR(dirs):

    config_file = os.path.join(os.getenv('HOME'), '.ssh/config')
    ssh_config = paramiko.SSHConfig()
    ssh_config.parse(open(config_file, 'r'))
    lkup = ssh_config.lookup('super.imr')
    print("type your passwd for IMR supercomputer node")
    passwd = getpass()

    # ProxyCommand setup
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    print("what found in config file")
    print(lkup)

    ssh.connect(lkup['hostname'],
                username=lkup['user'],
                sock=paramiko.ProxyCommand(lkup['proxycommand']),
                password=passwd)
    workingdir = '/home/emi0716/work/'
    jobcommand = "qsub run_imr.sh"
    joblogfile = 'joblog-' + str(datetime.datetime.now()) + '.txt'

    f = open(joblogfile, 'w')

    for i in dirs:
        print(i)

        chdircommand = "cd " + i

        stdin, stdout, stderr = ssh.exec_command(chdircommand + ";" + "pwd;" +
                                                 jobcommand)
        outlines = stdout.readlines()
        result = ''.join(outlines)
        err = stderr.readlines()
        print(result)
        print(err)

        f.write(result)

    f.flush()
    f.close()
Example #29
0
def get_gateway(host, port, cache, replace=False):
    """
    Create and return a gateway socket, if one is needed.

    This function checks ``env`` for gateway or proxy-command settings and
    returns the necessary socket-like object for use by a final host
    connection.

    :param host:
        Hostname of target server.

    :param port:
        Port to connect to on target server.

    :param cache:
        A ``HostConnectionCache`` object, in which gateway ``SSHClient``
        objects are to be retrieved/cached.

    :param replace:
        Whether to forcibly replace a cached gateway client object.

    :returns:
        A ``socket.socket``-like object, or ``None`` if none was created.
    """
    from fabric.state import env, output
    sock = None
    proxy_command = ssh_config().get('proxycommand', None)
    if env.gateway:
        gateway = normalize_to_string(env.gateway)
        # ensure initial gateway connection
        if replace or gateway not in cache:
            if output.debug:
                logging.debug("Creating new gateway connection to %r" % gateway)
            cache[gateway] = connect(*normalize(gateway) + (cache, False))
        # now we should have an open gw connection and can ask it for a
        # direct-tcpip channel to the real target. (bypass cache's own
        # __getitem__ override to avoid hilarity - this is usually called
        # within that method.)
        sock = direct_tcpip(dict.__getitem__(cache, gateway), host, port)
    elif proxy_command:
        sock = ssh.ProxyCommand(proxy_command)
    return sock
Example #30
0
File: RRS.py Project: Slacanch/RRS
    def sshObjectInit(self, host=None):
        """"""
        logText = self.getLogFunction()
        if not host:
            host = self.configDict['host']

        ssh_config = paramiko.SSHConfig()
        user_config_file = os.path.expanduser("~/.ssh/config")

        try:
            with open(user_config_file) as f:
                ssh_config.parse(f)
        except:
            logText("Could not parse ~/.ssh/config, does the file exist?")
            return None

        try:
            hpcConfig = ssh_config.lookup(host)
            proxy = paramiko.ProxyCommand(hpcConfig['proxycommand'])
            assert hpcConfig['hostname']
            assert hpcConfig['user']
        except:
            logText(
                "host configuration seems to be incorrect. the host must be "
                "configured to have a hostname, username and proxy command.")
            return None

        try:
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.load_system_host_keys()

            client.connect(hpcConfig['hostname'],
                           username=hpcConfig['user'],
                           sock=proxy)
            client.get_transport().set_keepalive(15)
            self.sshAlive = True

            return client
        except:
            logText("failed to connect to the hpc, internet not working?")
            return None