Пример #1
0
    def from_string(config_str):
        """Construct RemoteAccountSSHConfig object from a string that looks like

        Host the-host
            Hostname the-hostname
            Port 22
            User ubuntu
            IdentityFile /path/to/key
        """
        config = SSHConfig()
        config.parse(config_str.split("\n"))

        hostnames = config.get_hostnames()
        if '*' in hostnames:
            hostnames.remove('*')
        assert len(
            hostnames
        ) == 1, "Expected hostnames to have single entry: %s" % hostnames
        host = hostnames.pop()

        config_dict = config.lookup(host)
        if config_dict.get("identityfile") is not None:
            # paramiko.SSHConfig parses this in as a list, but we only want a single string
            config_dict["identityfile"] = config_dict["identityfile"][0]

        return RemoteAccountSSHConfig(host, **config_dict)
Пример #2
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)
Пример #3
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
Пример #4
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)
Пример #6
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
Пример #7
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
Пример #8
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)
Пример #9
0
    def test_main(self):
        import mock
        import copy
        import getpass
        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,
                'OWNER': getpass.getuser()
        }):
            # 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])
Пример #10
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
Пример #11
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'])
Пример #12
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()
Пример #13
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: ')
Пример #14
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)
Пример #15
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
Пример #16
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'])
Пример #17
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'])
Пример #18
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)
Пример #19
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)
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()
Пример #21
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
Пример #22
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()
Пример #23
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
Пример #25
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
                )
Пример #26
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)
Пример #27
0
 def test_quoted_host_in_config(self):
     conf = SSHConfig()
     correct_data = {
         "param": ["param"],
         '"param"': ["param"],
         "param pam": ["param", "pam"],
         '"param" "pam"': ["param", "pam"],
         '"param" pam': ["param", "pam"],
         'param "pam"': ["param", "pam"],
         'param "pam" p': ["param", "pam", "p"],
         '"param" pam "p"': ["param", "pam", "p"],
         '"pa ram"': ["pa ram"],
         '"pa ram" pam': ["pa ram", "pam"],
         'param "p a m"': ["param", "p a m"],
     }
     incorrect_data = ['param"', '"param', 'param "pam', 'param "pam" "p a']
     for host, values in correct_data.items():
         assert conf._get_hosts(host) == values
     for host in incorrect_data:
         self.assertRaises(Exception, conf._get_hosts, host)
Пример #28
0
 def test_quoted_host_in_config(self):
     conf = SSHConfig()
     correct_data = {
         "param": ["param"],
         '"param"': ["param"],
         "param pam": ["param", "pam"],
         '"param" "pam"': ["param", "pam"],
         '"param" pam': ["param", "pam"],
         'param "pam"': ["param", "pam"],
         'param "pam" p': ["param", "pam", "p"],
         '"param" pam "p"': ["param", "pam", "p"],
         '"pa ram"': ["pa ram"],
         '"pa ram" pam': ["pa ram", "pam"],
         'param "p a m"': ["param", "p a m"],
     }
     incorrect_data = ['param"', '"param', 'param "pam', 'param "pam" "p a']
     for host, values in correct_data.items():
         assert conf._get_hosts(host) == values
     for host in incorrect_data:
         with raises(ConfigParseError):
             conf._get_hosts(host)
Пример #29
0
def update_database():
    if os.uname()[1] in ('workstation', ):
        print("Actualizando copia local de la base de datos...")
        host = 'ha-remote'
        ssh = SSHClient()
        ssh_config_file = os.path.expanduser("~/.ssh/config")
        if os.path.exists(ssh_config_file):
            conf = SSHConfig()
            with open(ssh_config_file) as f:
                conf.parse(f)
            host_config = conf.lookup(host)
        proxy = ProxyCommand("ssh {}@{} -p {} nc {} 22".format(
            "pi", "remote-ssh.proxy.host", 2222, 'homeassistant-host'))
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        #ssh.connect(host, username=user, pkey=pkey, sock=proxy)
        ssh.connect(host, username=host_config['user'], sock=proxy)
        with SCPClient(ssh.get_transport()) as scp:
            src = '/home/homeassistant/devel/hamlfeeder/database.sqlite'
            dst = '/home/roberto/devel/python/hamlfeeder/database.sqlite'
            scp.get(src, dst)
Пример #30
0
def start_procs(interval, 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 interval > 0.0:
            process.join()
            sleep(interval)
        processes.append(process)

    while multiprocessing.active_children():
        try:
            sleep(0.3)
        except KeyboardInterrupt:
            for p in processes:
                p.stop()
            break
    return processes