Пример #1
0
def runSSHScatter(args, nprocs, outBufSize, errBufSize):
    #parse the args with ssh syntax
    parser = SSHArgsParser('scp')
    parser.parse(args)
    #parse the multi-host args
    if len(parser.posargs) != 2:
        raise SyntaxError(
            'Expect 2 positional args, got %s: %s'
            %(len(parser.posargs), parser.posargs))
    ##the first is the src path, the second is the all hosts dst path
    src = parser.posargs[0]
    dsts = parser.posargs[1]
    ##parse hosts
    if '@' in dsts:
        user, dsts = dsts.split('@')
    else:
        user = getpass.getuser()
    hostfile, r, dstdir = parseAddrString(dsts)
    hosts = fileToList(hostfile)
    src = normalizeName(src)
    dstdir = normalizeName(dstdir)
    sshCmds = []
    for i in r:
        try:
            host = hosts[i]
        except IndexError:
            print 'Host file only have %s entries'%len(hosts)
            break
        command = 'scp %s %s %s@%s:%s'%(' '.join(parser.options),
                                        src, user, host, dstdir)
        sshCmds.append(SSHCmd(command, outBufSize, errBufSize))
    runCommands(sshCmds, nprocs)
Пример #2
0
def runSSHCmd(args, nprocs, outBufSize, errBufSize):
    #parse the args with ssh syntax
    parser = SSHArgsParser('ssh')
    parser.parse(args)
    #parse the multi-host args
    if len(parser.posargs) != 2:
        raise SyntaxError(
            'Expect 2 positional args, got %s: %s'
            %(len(parser.posargs), parser.posargs))
    ##the first must be the hosts, the second is a command
    hosts = parser.posargs[0]
    cmd = parser.posargs[1]
    ##parse hosts
    if '@' in hosts:
        user, hosts = hosts.split('@')
    else:
        user = getpass.getuser()
    try:
        hostfile, rstr = re.split(':', hosts, 1)
        r = RangeStringParser().parse(rstr)
    except Exception as e:
        raise SyntaxError(
            'Hosts string should be in the form hostfile:range: %s'
            'Error message: %s' %(hosts, e))
    hosts = fileToList(hostfile)
    sshCmds = []
    for i in r:
        try:
            host = hosts[i]
        except IndexError:
            print 'Host file only have %s entries'%len(hosts)
            break
        command = 'ssh %s %s@%s %s'%(' '.join(parser.options),
                                     user, host, cmd)
        sshCmds.append(SSHCmd(command, outBufSize, errBufSize))
    runCommands(sshCmds, nprocs)