コード例 #1
0
ファイル: __init__.py プロジェクト: tiradani/py_stdlib
def do_pscp(pssh_config):
    rtn_code = 0

    options = PSSHOptions(pssh_config)
    hosts = psshutil.read_host_files(options.host_files, default_user='******')

    manager = Manager(options)
    for host, port, user in hosts:
        cmd = ['scp', '-qC']
        if port:
            cmd += ['-P', port]
        if options.recursive:
            cmd.append('-r')
        if user:
            cmd.append('%s@%s:%s' % (user, host, options.remote))
        else:
            cmd.append('%s:%s' % (host, options.remote))
        cmd.append('%s' % options.outdir)
        t = Task(host, port, user, cmd, options)
        manager.add_task(t)
    try:
        statuses = manager.run()
    except FatalError:
        rtn_code = 1

    if min(statuses) < 0:
        # At least one process was killed.
        rtn_code = 3

    for status in statuses:
        if status != 0:
            rtn_code = 4

    return rtn_code
コード例 #2
0
ファイル: api.py プロジェクト: krig/parallel-ssh
def copy(hosts, src, dst, opts=Options()):
    """
    Copies from the local node to a set of remote hosts
    hosts: [(host, port, user)...]
    src: local path
    dst: remote path
    opts: CopyOptions (optional)
    Returns {host: (rc, stdout, stdin) | Error}
    """
    if opts.outdir and not os.path.exists(opts.outdir):
        os.makedirs(opts.outdir)
    if opts.errdir and not os.path.exists(opts.errdir):
        os.makedirs(opts.errdir)
    manager = Manager(limit=opts.limit,
                      timeout=opts.timeout,
                      askpass=opts.askpass,
                      outdir=opts.outdir,
                      errdir=opts.errdir,
                      callbacks=_CopyOutputBuilder())
    for host, port, user in _expand_host_port_user(hosts):
        cmd = _build_copy_cmd(host, port, user, src, dst, opts)
        t = Task(host, port, user, cmd,
                 stdin=opts.input_stream,
                 verbose=opts.verbose,
                 quiet=opts.quiet,
                 print_out=opts.print_out,
                 inline=opts.inline,
                 inline_stdout=opts.inline_stdout,
                 default_user=opts.default_user)
        manager.add_task(t)
    try:
        return manager.run()
    except FatalError:
        sys.exit(1)
コード例 #3
0
ファイル: api.py プロジェクト: krig/parallel-ssh
def call(hosts, cmdline, opts=Options()):
    """
    Executes the given command on a set of hosts, collecting the output
    Returns {host: (rc, stdout, stdin) | Error}
    """
    if opts.outdir and not os.path.exists(opts.outdir):
        os.makedirs(opts.outdir)
    if opts.errdir and not os.path.exists(opts.errdir):
        os.makedirs(opts.errdir)
    manager = Manager(limit=opts.limit,
                      timeout=opts.timeout,
                      askpass=opts.askpass,
                      outdir=opts.outdir,
                      errdir=opts.errdir,
                      callbacks=_CallOutputBuilder())
    for host, port, user in _expand_host_port_user(hosts):
        cmd = _build_call_cmd(host, port, user, cmdline,
                              options=opts.ssh_options,
                              extra=opts.ssh_extra)
        t = Task(host, port, user, cmd,
                 stdin=opts.input_stream,
                 verbose=opts.verbose,
                 quiet=opts.quiet,
                 print_out=opts.print_out,
                 inline=opts.inline,
                 inline_stdout=opts.inline_stdout,
                 default_user=opts.default_user)
        manager.add_task(t)
    try:
        return manager.run()
    except FatalError:
        sys.exit(1)
コード例 #4
0
def do_pscp(pssh_config):
    rtn_code = 0

    options = PSSHOptions(pssh_config)
    hosts = psshutil.read_host_files(options.host_files, default_user='******')

    manager = Manager(options)
    for host, port, user in hosts:
        cmd = ['scp', '-qC']
        if port:
            cmd += ['-P', port]
        if options.recursive:
            cmd.append('-r')
        if user:
            cmd.append('%s@%s:%s' % (user, host, options.remote))
        else:
            cmd.append('%s:%s' % (host, options.remote))
        cmd.append('%s' % options.outdir)
        t = Task(host, port, user, cmd, options)
        manager.add_task(t)
    try:
        statuses = manager.run()
    except FatalError:
        rtn_code = 1

    if min(statuses) < 0:
        # At least one process was killed.
        rtn_code = 3

    for status in statuses:
        if status != 0:
            rtn_code = 4

    return rtn_code
コード例 #5
0
ファイル: pscp.py プロジェクト: craiig/remote-perf
def do_pssh(hosts, remote_file, local_dir, opts):
    if opts.outdir and not os.path.exists(opts.outdir):
        os.makedirs(opts.outdir)
    if opts.errdir and not os.path.exists(opts.errdir):
        os.makedirs(opts.errdir)
    if opts.send_input:
        stdin = sys.stdin.read()
    else:
        stdin = None
    manager = Manager(opts)
    for host, port, user in hosts:
        # scp -r user@host:file local_file
        from_str = "{user}@{host}:{remote_file}".format(user=user, host=host, remote_file=remote_file)
        to_str = "{local_dir}/{host}".format(local_dir=local_dir, host=host)
        if not os.path.isdir(to_str):
            os.makedirs(to_str)

        cmd = ['scp', '-o', 'NumberOfPasswordPrompts=1',
                '-o', 'SendEnv=PSSH_NODENUM PSSH_HOST'
                ]

        if opts.options:
            for opt in opts.options:
                cmd += ['-o', opt]
        if port:
            cmd += ['-P', port]
        if opts.extra:
            cmd.extend(opts.extra)
        print cmd

        # src and destination parameters must tbe the last
        cmd += [from_str, to_str]

        t = Task(host, port, user, cmd, opts, stdin)
        manager.add_task(t)
    try:
        statuses = manager.run()
    except FatalError:
        sys.exit(1)

    if min(statuses) < 0:
        # At least one process was killed.
        sys.exit(3)
    # The any builtin was introduced in Python 2.5 (so we can't use it yet):
    #elif any(x==255 for x in statuses):
    for status in statuses:
        if status == 255:
            sys.exit(4)
    for status in statuses:
        if status != 0:
            sys.exit(5)
コード例 #6
0
 def setup_manager(self, hosts, args, opts):
     pattern = args[0]
     manager = Manager(opts)
     for host, port, user in hosts:
         cmd = ['ssh', host, '-o', 'NumberOfPasswordPrompts=1']
         if opts.options:
             for opt in opts.options:
                 cmd += ['-o', opt]
         if user:
             cmd += ['-l', user]
         if port:
             cmd += ['-p', port]
         if opts.extra:
             cmd.extend(opts.extra)
         cmd.append('pkill -9 %s' % pattern)
         t = Task(host, port, user, cmd, opts)
         manager.add_task(t)
     return manager
コード例 #7
0
ファイル: cli.py プロジェクト: pfhayes/parallel-ssh
 def setup_manager(self, hosts, args, opts):
     pattern = args[0]
     manager = Manager(opts)
     for host, port, user in hosts:
         cmd = ['ssh', host, '-o', 'NumberOfPasswordPrompts=1']
         if opts.options:
             for opt in opts.options:
                 cmd += ['-o', opt]
         if user:
             cmd += ['-l', user]
         if port:
             cmd += ['-p', port]
         if opts.extra:
             cmd.extend(opts.extra)
         cmd.append('pkill -9 %s' % pattern)
         t = Task(host, port, user, cmd, opts)
         manager.add_task(t)
     return manager
コード例 #8
0
ファイル: psshcmd.py プロジェクト: kiterunner-t/pssh
def do_pssh(hosts, cmdline, opts):
    if opts.outdir and not os.path.exists(opts.outdir):
        os.makedirs(opts.outdir)
    if opts.errdir and not os.path.exists(opts.errdir):
        os.makedirs(opts.errdir)
    if opts.send_input:
        stdin = sys.stdin.read()
    else:
        stdin = None
    manager = Manager(opts)
    for host, port, user in hosts:
        ssh_cmd = ['ssh', host, '-o', 'NumberOfPasswordPrompts=1',
                '-o', 'SendEnv=PSSH_NODENUM PSSH_HOST']
        if opts.options:
            for opt in opts.options:
                ssh_cmd += ['-o', opt]
        if user:
            ssh_cmd += ['-l', user]
        if port:
            ssh_cmd += ['-p', port]
        if opts.extra:
            ssh_cmd.extend(opts.extra)
        if cmdline:
            ssh_cmd.append(cmdline)
        t = Task(host, port, user, ssh_cmd, opts, stdin)
        manager.add_task(t)
    try:
        statuses = manager.run()
    except FatalError:
        sys.exit(1)

    if min(statuses) < 0:
        # At least one process was killed.
        sys.exit(3)
    # The any builtin was introduced in Python 2.5 (so we can't use it yet):
    #elif any(x==255 for x in statuses):
    for status in statuses:
        if status == 255:
            sys.exit(4)
    for status in statuses:
        if status != 0:
            sys.exit(5)
コード例 #9
0
def do_pssh(l, opts):
    '''
    Adapted from psshlib. Perform command across list of hosts.
    l = [(host, command), ...]
    '''
    if opts.outdir and not os.path.exists(opts.outdir):
        os.makedirs(opts.outdir)
    if opts.errdir and not os.path.exists(opts.errdir):
        os.makedirs(opts.errdir)
    if opts.send_input:
        stdin = sys.stdin.read()
    else:
        stdin = None
    manager = Manager(opts)
    user = ""
    port = ""
    hosts = []
    for host, cmdline in l:
        cmd = ['ssh', host,
               '-o', 'PasswordAuthentication=no',
               '-o', 'SendEnv=PSSH_NODENUM',
               '-o', 'StrictHostKeyChecking=no']
        if opts.options:
            for opt in opts.options:
                cmd += ['-o', opt]
        if user:
            cmd += ['-l', user]
        if port:
            cmd += ['-p', port]
        if opts.extra:
            cmd.extend(opts.extra)
        if cmdline:
            cmd.append(cmdline)
        hosts.append(host)
        t = Task(host, port, user, cmd, opts, stdin)
        manager.add_task(t)
    try:
        return manager.run()  # returns a list of exit codes
    except FatalError:
        common_err("pssh to nodes failed")
        show_output(opts.errdir, hosts, "stderr")
        return False
コード例 #10
0
ファイル: ssh.py プロジェクト: ekorian/deploypl
def run_command(hosts, loginname, cmdline, keyloc=None, timeout=10, 
                                  threads=100, port=None, sudo=False):
   opts=SSHArgs({'send_input': None, 'par': threads, 'verbose': None, 
                 'inline_stdout': None, 'extra': None, 'askpass': None, 
                 'errdir': None, 'print_out': None, 'options': None, 
                 'user': loginname, 'timeout': timeout, 'inline': True, 
                 'host_strings': None, 'outdir': None})

   manager = Manager(opts)
   for host in hosts:
      cmd = ['ssh', host, '-o', 'NumberOfPasswordPrompts=1',
             '-o', 'StrictHostKeyChecking=no',
             '-o', 'SendEnv=PSSH_NODENUM PSSH_HOST']
      if keyloc:
         cmd.extend(['-i', keyloc])
      if opts.options:
         for opt in opts.options:
            cmd += ['-o', opt]
      if loginname:
         cmd += ['-l', loginname]
      if port:
         cmd += ['-p', port]
      if opts.extra:
         cmd.extend(opts.extra)
      if cmdline:
         _command = ""
         if sudo:
            _command = 'sudo -S '
         cmd.append(_command+cmdline)

      t = SSHCmdTask(host, port, loginname, cmd, opts, None)
      manager.add_task(t)

   # run tasks
   try:
      manager.run()
   except FatalError:
      return []
   
   return [t.result for t in manager.done]
コード例 #11
0
ファイル: ssh.py プロジェクト: ekorian/deploypl
def download(hosts, loginname, remotedir, localdir, keyloc=None, localfile=".", timeout=10, threads=100, port=None, recursive=False):
   opts = SSHArgs({'verbose': None, 'askpass': None, 'options': None, 
                   'errdir': None, 'timeout': timeout, 'recursive': recursive, 
                   'user': loginname, 'par': threads, 'extra': None, 
                   'outdir': None, 'host_strings': None, 'localdir': None})
   # create directory
   for host in hosts:
      dirname = "%s/%s" % (localdir, host)
      if not os.path.exists(dirname):
         os.mkdir(dirname)

   manager = Manager(opts)
   for host in hosts:
      cmd = ['scp', '-qC', '-o', 'StrictHostKeyChecking=no']
      if opts.options:
         for opt in opts.options:
             cmd += ['-o', opt]
      if keyloc:
         cmd.extend(['-i', keyloc])
      if port:
         cmd += ['-P', port]
      if opts.recursive:
         cmd.append('-r')
      if opts.extra:
         cmd.extend(opts.extra)
      cmd.append('%s@%s:%s' % (loginname, host, remotedir))

      localpath = "%s/%s/%s" % (localdir, host, localfile)
      cmd.append(localpath)

      t = SSHCmdTask(host, port, loginname, cmd, opts)
      manager.add_task(t)

   # run tasks
   try:
      manager.run()
   except FatalError:
      return [] 

   return [t.result for t in manager.done]
コード例 #12
0
def do_pssh(cmdline, pssh_config):
    rtn_code = 0

    options = PSSHOptions(pssh_config)
    hosts = psshutil.read_host_files(options.host_files, default_user='******')
    manager = Manager(options)

    for host, port, user in hosts:
        cmd = [
            'ssh', host, '-o', 'NumberOfPasswordPrompts=1', '-o',
            'SendEnv=PSSH_NODENUM PSSH_HOST'
        ]
        if user:
            cmd += ['-l', user]
        if port:
            cmd += ['-p', port]

        if cmdline:
            cmd.append(cmdline)

        t = Task(host, port, user, cmd, options)
        manager.add_task(t)
    try:
        statuses = manager.run()
    except FatalError:
        rtn_code = 1

    if min(statuses) < 0:
        # At least one process was killed.
        rtn_code = 3

    for status in statuses:
        if status == 255:
            rtn_code = 4

    for status in statuses:
        if status != 0:
            rtn_code = 5

    return rtn_code
コード例 #13
0
ファイル: api.py プロジェクト: krig/parallel-ssh
def slurp(hosts, src, dst, opts=Options()):
    """
    Copies from the remote node to the local node
    hosts: [(host, port, user)...]
    src: remote path
    dst: local path
    opts: CopyOptions (optional)
    Returns {host: (rc, stdout, stdin, localpath) | Error}
    """
    if os.path.isabs(dst):
        raise ValueError("slurp: Destination must be a relative path")
    localdirs = _slurp_make_local_dirs(hosts, dst, opts)
    if opts.outdir and not os.path.exists(opts.outdir):
        os.makedirs(opts.outdir)
    if opts.errdir and not os.path.exists(opts.errdir):
        os.makedirs(opts.errdir)
    manager = Manager(limit=opts.limit,
                      timeout=opts.timeout,
                      askpass=opts.askpass,
                      outdir=opts.outdir,
                      errdir=opts.errdir,
                      callbacks=_SlurpOutputBuilder(localdirs))
    for host, port, user in _expand_host_port_user(hosts):
        localpath = localdirs[host]
        cmd = _build_slurp_cmd(host, port, user, src, localpath, opts)
        t = Task(host, port, user, cmd,
                 stdin=opts.input_stream,
                 verbose=opts.verbose,
                 quiet=opts.quiet,
                 print_out=opts.print_out,
                 inline=opts.inline,
                 inline_stdout=opts.inline_stdout,
                 default_user=opts.default_user)
        manager.add_task(t)
    try:
        return manager.run()
    except FatalError:
        sys.exit(1)
コード例 #14
0
ファイル: __init__.py プロジェクト: tiradani/py_stdlib
def do_pssh(cmdline, pssh_config):
    rtn_code = 0

    options = PSSHOptions(pssh_config)
    hosts = psshutil.read_host_files(options.host_files, default_user='******')
    manager = Manager(options)

    for host, port, user in hosts:
        cmd = ['ssh', host, '-o', 'NumberOfPasswordPrompts=1',
                '-o', 'SendEnv=PSSH_NODENUM PSSH_HOST']
        if user:
            cmd += ['-l', user]
        if port:
            cmd += ['-p', port]

        if cmdline:
            cmd.append(cmdline)

        t = Task(host, port, user, cmd, options)
        manager.add_task(t)
    try:
        statuses = manager.run()
    except FatalError:
        rtn_code = 1

    if min(statuses) < 0:
        # At least one process was killed.
        rtn_code = 3

    for status in statuses:
        if status == 255:
            rtn_code = 4

    for status in statuses:
        if status != 0:
            rtn_code = 5

    return rtn_code
コード例 #15
0
ファイル: core.py プロジェクト: bcicen/tagshell
    def __init__(self, cmd, nodes, config, confirm=True):
        self.opts = TagShellConfig(config)
        self.opts.cmdline = cmd

        log.info('executing %s against %s' % (cmd, nodes))
        self.color = TermColors()

        print('executing "%s" against:' % cmd)
        [self.color.red(node) for node in nodes]

        if confirm:
            self._confirm()

        opts = self.opts

        for d in [opts.outdir, opts.errdir]:
            if not os.path.exists(d):
                os.makedirs(d)
                log.debug('created dir %s' % d)

        self.manager = Manager(opts)
        for node in nodes:
            t = self._maketask(node, cmd)
            self.manager.add_task(t)
        try:
            statuses = self.manager.run()
        except FatalError:
            sys.exit(1)

        if min(statuses) < 0:
            # At least one process was killed.
            sys.exit(3)
        elif any(x == 255 for x in statuses):
            sys.exit(4)
        for status in statuses:
            if status != 0:
                sys.exit(5)
コード例 #16
0
ファイル: ssh.py プロジェクト: ekorian/deploypl
def upload(hosts, loginname, localdirs, remotedir, timeout=10, 
           threads=50, port=None, recursive=False, keyloc=None):

   opts = SSHArgs({'verbose': None, 'askpass': None, 'options': None, 
                   'errdir': None, 'timeout': timeout, 'recursive': recursive, 
                   'user': loginname, 'par': threads, 'extra': None, 
                   'outdir': None, 'host_strings': None})
   manager = Manager(opts)
   
   for host in hosts:
      cmd = ['scp', '-qC', '-o', 'StrictHostKeyChecking=no']
      if opts.options:
         for opt in opts.options:
             cmd += ['-o', opt]
      if keyloc:
         cmd.extend(['-i', keyloc])
      if port:
         cmd += ['-P', port]
      if opts.recursive:
         cmd.append('-r')
      if opts.extra:
         cmd.extend(opts.extra)
      cmd.extend(localdirs)
      cmd.append('%s@%s:%s' % (loginname, host, remotedir))

      t = SSHCmdTask(host, port, loginname, cmd, opts)
      manager.add_task(t)   

   # run tasks
   try:
      manager.run()
   except FatalError:
      return []
   
   # return result
   return [t.result for t in manager.done]
コード例 #17
0
ファイル: psshcmd.py プロジェクト: kiterunner-t/pssh
    def __init__(self, opts):
        cmd.Cmd.__init__(self)
        self.prompt = ">> "   # define command prompt

        if opts.outdir and not os.path.exists(opts.outdir):
            os.makedirs(opts.outdir)

        if opts.errdir and not os.path.exists(opts.errdir):
            os.makedirs(opts.errdir)

        self.stdin = None
        if opts.send_input:
            self.stdin = sys.stdin.read()

        self.options = opts

        self.cmd_manager = Manager(self.options)
        self.plugin_manager = PluginManager()
        self.builtin_cmd = BuiltInCmd(self.options)
コード例 #18
0
def do_pssh(hosts, cmdline, opts):
    if opts.outdir and not os.path.exists(opts.outdir):
        os.makedirs(opts.outdir)
    if opts.errdir and not os.path.exists(opts.errdir):
        os.makedirs(opts.errdir)
    stdin = buffer_input()
    manager = Manager(opts)
    for host, port, user in hosts:
        cmd = ['ssh', host, '-o', 'NumberOfPasswordPrompts=1',
               '-o', 'SendEnv=PSSH_NODENUM', '-o', 'StrictHostKeyChecking=no']
        if not opts.verbose:
            cmd.append('-q')
        if opts.options:
            cmd += ['-o', opts.options]
        if user:
            cmd += ['-l', user]
        if port:
            cmd += ['-p', port]
        cmd.append(cmdline)
        t = Task(host, port, cmd, opts, stdin)
        manager.add_task(t)
    manager.run()
    if opts.report:
        manager.report()
コード例 #19
0
ファイル: core.py プロジェクト: bcicen/tagshell
    def __init__(self, cmd, nodes, config, confirm=True):
        self.opts = TagShellConfig(config)
        self.opts.cmdline = cmd

        log.info('executing %s against %s' % (cmd,nodes))
        self.color = TermColors()

        print('executing "%s" against:' % cmd)
        [ self.color.red(node) for node in nodes ]

        if confirm:
           self._confirm()

        opts = self.opts
        
        for d in [ opts.outdir, opts.errdir ]:
            if not os.path.exists(d):
                os.makedirs(d)
                log.debug('created dir %s' % d)

        self.manager = Manager(opts)
        for node in nodes:
            t = self._maketask(node,cmd)
            self.manager.add_task(t)
        try:
            statuses = self.manager.run()
        except FatalError:
            sys.exit(1)

        if min(statuses) < 0:
            # At least one process was killed.
            sys.exit(3)
        elif any(x==255 for x in statuses):
            sys.exit(4)
        for status in statuses:
            if status != 0:
                sys.exit(5)
コード例 #20
0
ファイル: core.py プロジェクト: bcicen/tagshell
class TagShell(object):
    """
    TagShell class is a wrapper around the psshlib package.
    params:
    cmd - string of command to run
    nodes - list of hosts to execute against
    config - dict of config values to override default
    """
    def __init__(self, cmd, nodes, config, confirm=True):
        self.opts = TagShellConfig(config)
        self.opts.cmdline = cmd

        log.info('executing %s against %s' % (cmd, nodes))
        self.color = TermColors()

        print('executing "%s" against:' % cmd)
        [self.color.red(node) for node in nodes]

        if confirm:
            self._confirm()

        opts = self.opts

        for d in [opts.outdir, opts.errdir]:
            if not os.path.exists(d):
                os.makedirs(d)
                log.debug('created dir %s' % d)

        self.manager = Manager(opts)
        for node in nodes:
            t = self._maketask(node, cmd)
            self.manager.add_task(t)
        try:
            statuses = self.manager.run()
        except FatalError:
            sys.exit(1)

        if min(statuses) < 0:
            # At least one process was killed.
            sys.exit(3)
        elif any(x == 255 for x in statuses):
            sys.exit(4)
        for status in statuses:
            if status != 0:
                sys.exit(5)

    def _maketask(self, node, cmd):
        """
        assemble command and return a pssh task object suitable
        for submitting to psshlib manager
        """
        opts = self.opts
        cmd = [
            'ssh', node, '-o', 'NumberOfPasswordPrompts=1', '-o',
            'SendEnv=PSSH_NODENUM PSSH_HOST'
        ]
        if opts.options:
            for opt in opts.options:
                cmd += ['-o', opt]
        if opts.user:
            cmd += ['-l', opts.user]
        if opts.port:
            cmd += ['-p', opts.port]
        if opts.cmdline:
            cmd.append(opts.cmdline)
        log.debug('full command compiled as: %s' % ' '.join(cmd))

        return Task(node, opts.port, opts.user, cmd, opts, opts.stdin)

    def _confirm(self):
        s = raw_input('confirm?(yes/no):')
        if s != 'yes':
            print('execution aborted')
            sys.exit(1)
コード例 #21
0
ファイル: psshcmd.py プロジェクト: kiterunner-t/pssh
class PsshCmd(cmd.Cmd):
    def __init__(self, opts):
        cmd.Cmd.__init__(self)
        self.prompt = ">> "   # define command prompt

        if opts.outdir and not os.path.exists(opts.outdir):
            os.makedirs(opts.outdir)

        if opts.errdir and not os.path.exists(opts.errdir):
            os.makedirs(opts.errdir)

        self.stdin = None
        if opts.send_input:
            self.stdin = sys.stdin.read()

        self.options = opts

        self.cmd_manager = Manager(self.options)
        self.plugin_manager = PluginManager()
        self.builtin_cmd = BuiltInCmd(self.options)


    def register_builtin(self):
        self.plugin_manager.register(self.builtin_cmd)


    def register(self, plugin):
        self.plugin_manager.register(plugin)


    def default(self, cmdline):
        if self.plugin_manager.run_plugins(cmdline):
            self.pssh_run(cmdline)


    def emptyline(self):
        # do nothing
        pass


    def do_quit(self, arg):
        return True


    def do_help(self, arg):
        self.plugin_manager.print_help()


    def pssh_run(self, cmdline):
        for host, port, user in self.options.hosts:
            cmd_ssh = ['ssh', host, '-o', 'NumberOfPasswordPrompts=1',
                    '-o', 'SendEnv=PSSH_NODENUM PSSH_HOST']
            if self.options.options:
                for opt in self.options.options:
                    cmd_ssh += ['-o', opt]
            if user:
                cmd_ssh += ['-l', user]
            if port:
                cmd_ssh += ['-p', port]
            if self.options.extra:
                cmd_ssh.extend(self.options.extra)
            if cmdline:
                cmd_ssh.append(cmdline)

            t = Task(host, port, user, cmd_ssh, self.options, self.stdin)
            self.cmd_manager.add_task(t)

        try:
            statuses = self.cmd_manager.run()
        except FatalError:
            sys.exit(1)
コード例 #22
0
ファイル: core.py プロジェクト: bcicen/tagshell
class TagShell(object):
    """
    TagShell class is a wrapper around the psshlib package.
    params:
    cmd - string of command to run
    nodes - list of hosts to execute against
    config - dict of config values to override default
    """
    def __init__(self, cmd, nodes, config, confirm=True):
        self.opts = TagShellConfig(config)
        self.opts.cmdline = cmd

        log.info('executing %s against %s' % (cmd,nodes))
        self.color = TermColors()

        print('executing "%s" against:' % cmd)
        [ self.color.red(node) for node in nodes ]

        if confirm:
           self._confirm()

        opts = self.opts
        
        for d in [ opts.outdir, opts.errdir ]:
            if not os.path.exists(d):
                os.makedirs(d)
                log.debug('created dir %s' % d)

        self.manager = Manager(opts)
        for node in nodes:
            t = self._maketask(node,cmd)
            self.manager.add_task(t)
        try:
            statuses = self.manager.run()
        except FatalError:
            sys.exit(1)

        if min(statuses) < 0:
            # At least one process was killed.
            sys.exit(3)
        elif any(x==255 for x in statuses):
            sys.exit(4)
        for status in statuses:
            if status != 0:
                sys.exit(5)

    def _maketask(self,node,cmd):
        """
        assemble command and return a pssh task object suitable
        for submitting to psshlib manager
        """
        opts = self.opts
        cmd = ['ssh', node, '-o', 'NumberOfPasswordPrompts=1',
                '-o', 'SendEnv=PSSH_NODENUM PSSH_HOST']
        if opts.options:
            for opt in opts.options:
                cmd += ['-o', opt]
        if opts.user:
            cmd += ['-l', opts.user]
        if opts.port:
            cmd += ['-p', opts.port]
        if opts.cmdline:
            cmd.append(opts.cmdline)
        log.debug('full command compiled as: %s' % ' '.join(cmd))

        return Task(node,opts.port,opts.user,cmd,opts,opts.stdin)

    def _confirm(self):
        s = raw_input('confirm?(yes/no):')
        if s != 'yes':
            print('execution aborted')
            sys.exit(1)