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) 
4
  • Explicitly use a dictionary for mapping names to lists Commented Mar 31, 2014 at 9:16
  • @wim I've had a quick Google, the only reference to "Explicit use of dictionaries" i can find is here the first section "General Concepts" - Explicit code. Is this what your referring to? Commented Mar 31, 2014 at 9:26
  • Are you looking for something like this? stackoverflow.com/questions/38987/… Commented Mar 31, 2014 at 9:32
  • @iNoob put line 3 in the new code at line 6 instead. Commented Mar 31, 2014 at 9:58

1 Answer 1

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):
    ...
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.