0

I'm looking to iterate a list of servers executing a command via subprocess.check_output on each. The output of each is split using csv.DictReader. I would like to be able to create dictionaries for each server/cmd output then join them after the server_list has been fully iterated. I can then filter all the output. Is it possible? if so could someone point me in the right direction please.

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: Includes spinlock's suggestion from Answer below

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) 
iNoob
  • 1,375
  • 3
  • 19
  • 47
  • Explicitly use a dictionary for mapping names to lists – wim Mar 31 '14 at 09:16
  • @wim I've had a quick Google, the only reference to "Explicit use of dictionaries" i can find is [here](http://docs.python-guide.org/en/latest/writing/style/) the first section "General Concepts" - Explicit code. Is this what your referring to? – iNoob Mar 31 '14 at 09:26
  • Are you looking for something like this? http://stackoverflow.com/questions/38987/how-can-i-merge-union-two-python-dictionaries-in-a-single-expression – Jasper Mar 31 '14 at 09:32
  • @iNoob put line 3 in the new code at line 6 instead. – spinlok Mar 31 '14 at 09:58

1 Answers1

1

Since csv.DictReader is an iterator, use itertools.chain to chain all the iterables together:

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):
    ...
    ...
spinlok
  • 3,561
  • 18
  • 27