Exemplo n.º 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)
Exemplo n.º 2
0
 def who(self, argv):
     if len(argv) < 1:
         print "ec2 who <regionName> <values or path>"
         sys.exit(-1)
     regionName = argv[0]
     #get values
     valuesOrPath = argv[1]
     if os.path.isfile(normalizeName(valuesOrPath)):
         values = fileToList(valuesOrPath)
     else:
         values = valuesOrPath.split(',')
         for i in range(len(values)):
             values[i] = values[i].strip()
     #get ec2 instances
     region = boto.ec2.get_region(regionName)
     conn = region.connect()
     resvList = conn.get_all_instances()
     for value in values:
         for resv in resvList:
             for instance in resv.instances:
                 for key, val in instance.__dict__.iteritems():
                     if re.search(value, str(val)):
                         print ('value=%s: id=%s, public=%s, private=%s'
                                %(value, instance.id,
                                  instance.ip_address,
                                  instance.private_ip_address))
                         break
Exemplo n.º 3
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)