【问题标题】:subprocess.popen(replacing a parameter from a list and place it on a command)subprocess.popen(从列表中替换参数并将其放在命令上)
【发布时间】:2014-06-26 19:57:32
【问题描述】:

这是我使用 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

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

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

感谢您的帮助和建议。

谢谢,

侯萨姆

【问题讨论】:

  • 我要提前感谢所有希望与我分享他的知识的人。

标签: python-2.7 parameters subprocess popen winexe


【解决方案1】:

我居然找到了答案:

我创建了一个服务器列表

服务器 = [//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

【讨论】:

    猜你喜欢
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2014-04-18
    • 2023-02-18
    • 2014-08-31
    • 2011-10-25
    • 1970-01-01
    相关资源
    最近更新 更多