0

这是我使用 python 的第一周,所以如果我的问题听起来很愚蠢,我想提前道歉。

基本上我写了这段代码:

__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

所以基本上我可以查询特定计算机“//computername”的IIS并且它可以工作。

但是我有20台电脑。我想创建一个列表 list = [computer1,computer2,computer3] 然后有一个功能:对于列表中的每个 c 将计算机的名称替换为 subprocess.check_output 中唯一的唯一参数“//computername”调用 winexe 命令,因此我不必为我拥有的所有计算机编写命令。

感谢您的帮助和建议。

谢谢,

侯萨姆

4

1 回答 1

0

我实际上找到了答案:

我创建了一个服务器列表

服务器 = [//computer1", "//computer2"]

然后我添加了一个 for 语句并将列表放入 popen 中,如下所示:

   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

然后我把它放在一个函数中,如下所示:

 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
于 2014-07-02T20:09:51.447 回答