动态创建列表变量

时间:2014-03-31 09:12:21

标签: python

我希望在每个服务器上通过subprocess.check_output迭代执行命令的服务器列表。每个的输出使用csv.DictReader分割。我希望能够为每个服务器/ cmd输出创建字典,然后在完全迭代server_list之后加入它们。然后我可以过滤所有输出。可能吗?如果是这样,有人可以指出我正确的方向。

def testremote(server_list, domain, username, password, user_list, debug):
    for server in server_list:
        try:
            cmd = subprocess.check_output(["tasklist", "/V", "/S", server, "/U", domain + "\\" + username, "/P", password, "/FO", "CSV"])
            tasks = csv.DictReader(cmd.splitlines(), dialect="excel") # would like to create dynamic tasks (ie tasks1, tasks2 etc for each server)

        except CalledProcessError as e:
            errormessage = e.output
            if "RPC" in errormessage:
                print "{0}: RPC service is not responding, most likley the server is not live." .format(server)
                print "Removing server from list.\n"
                server_list.remove(server)
            elif "password" in errormessage:
                print "{0}: The username or password are incorrect." .format(server)
                print "Removing server from list.\n"
                server_list.remove(server)
            else:
                print errormessage
    #Filtering output        
    users = set()
    for task in tasks:
        if task['User Name'] == 'N/A': continue
        task_domain, task_user = task['User Name'].split('\\')

        if domain == task_domain and task_user in user_list:
            users.add(task['User Name'])
    print '\n'.join(users) 

NewCode:包含来自以下答案的螺旋锁建议

def testremote(server_list, domain, username, password, user_list, debug):
    all_tasks = []

    for server in server_list:
        try:
            cmd = subprocess.check_output(["tasklist", "/V", "/S", server, "/U", domain + "\\" + username, "/P", password, "/FO", "CSV"], stderr=subprocess.STDOUT)
            tasks = csv.DictReader(cmd.splitlines(), dialect="excel") # would like to create dynamic tasks (ie tasks1, tasks2 etc for each server)
            all_tasks.append(tasks)

        except CalledProcessError as e:
            errormessage = e.output
            if "RPC" in errormessage:
                print "{0}: RPC service is not responding, most likley the server is not live." .format(server)
                print "Removing server from list.\n"
                server_list.remove(server)
            elif "password" in errormessage:
                print "{0}: The username or password are incorrect." .format(server)
                print "Removing server from list.\n"
                server_list.remove(server)
            else:
                print errormessage        

    users = set()
    for task in itertools.chain(*all_tasks):
        if task['User Name'] == 'N/A': continue
        task_domain, task_user = task['User Name'].split('\\')

        if domain == task_domain and task_user in user_list:
            users.add(task['User Name'])
    print '\n'.join(users) 

1 个答案:

答案 0 :(得分:1)

由于csv.DictReader是迭代器,因此使用itertools.chain将所有迭代链接在一起:

import itertools
all_tasks = []
for server in server_list:
    cmd = subprocess.check_output(["tasklist", "/V", "/S", server, "/U", domain + "\\" + username, "/P", password, "/FO", "CSV"])
    tasks = csv.DictReader(cmd.splitlines(), dialect="excel")
    all_tasks.append(tasks)

for task in itertools.chain(*all_tasks):
    ...
    ...