简体   繁体   中英

subprocess.popen(replacing a parameter from a list and place it on a command)

It's my first week with python, so i would like to apologize in advance if my question sounds stupid.

Basically I wrote this code:

__author__ = 'houssam'

import subprocess
from subprocess import Popen, PIPE

check = subprocess.Popen(["winexe", "--system", "-U","mydomain\\myusername%mypassword", "//computername", "cmd /C c:\\Windows\\System32\\inetsrv\\appcmd list site"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout,stderr) = check.communicate()

if check.returncode == 0:
print 'IIS is installed on this system in the location below:'
print stdout


elif check.returncode == 1:
print 'IIS is NOT installed on this system ' and stderr

So basically I can query an IIS of a specific computer "//computername" and it works.

However i have 20 computers. I want to create a list list = [computer1,computer2,computer3] and then have a function that does: for each c in list replace the name of the computer into that only unique parameter "//computername" inside the subprocess.check_output that calls the winexe command so i don't have to write the command for all the computers I have.

I appreciate your help and suggestions.

Thank you,

Houssam

I actually found the answer:

I created a list of servers

servers = [//computer1", "//computer2"]

then I added a for statement and put the list inside the popen as follow:

   for server in servers:
   check= subprocess.Popen(["winexe", "--system", "-    
   U","mydomain\\myusername%mypassword",     
   server, "cmd /C c:\\Windows\\System32\\inetsrv\\appcmd list     
   site"],stderr=subprocess.PIPE,   
   stdout=subprocess.PIPE)
   (stdout,stderr) = check.communicate()

       if check.returncode == 0:
         print 'IIS is installed on this system in the location below:'
         print stdout


       elif check.returncode == 1:
         print 'IIS is NOT installed on this system ' and stderr

Then I put it in a function as follow:

 def iis_check(servers):
    for server in servers:

         check= subprocess.Popen(["winexe", "--system", "-  
         U","mydomain\\myusername%mypassword",     
         server, "cmd /C c:\\Windows\\System32\\inetsrv\\appcmd list    
         site"],stderr=subprocess.PIPE,   

         stdout=subprocess.PIPE)
        (stdout,stderr) = check.communicate()

         if check.returncode == 0:
            print 'IIS is installed on this system in the location below:'
            print stdout


         elif check.returncode == 1:
            print 'IIS is NOT installed on this system ' and stderr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM