使用Python子进程和字符串格式化(Using Python subprocess and string formating)
我试图使用python的
subprocess
调用带有相关参数的windows命令。 命令正在执行,参数及其值看起来是正确的,但是当使用“本地模式”-l
时,它似乎只能正常工作。 使用远程模式时,我收到invalid argument/option
错误。 可以指出哪里出错?任何人都可以指出如何正确格式化
subprocess.check_ouput()
参数以包含执行脚本时在命令行中给出的变量? 正如你所看到的,我尝试使用字符串格式化,无论是新旧还是尝试让它工作,因为我无法锻炼如何在过滤器(/ FI)参数值之间添加最后一个domain
变量而没有字符串格式。预期的命令行执行
tasklist /V /S 192.168.1.122 /U 'DOMAIN'\'USERNAME' /P 'PASSWORD' /FI "USERNAME eq 'DOMAIN'\*"
使用此命令行脚本示例:
hunter.py -d DOMAIN -u USERNAME -p PASSWORD -s servers.txt
这是错误:
ERROR: Invalid argument/option - '/S 192.168.1.122'. Type "TASKLIST /?" for usage.
显然,论证无论如何都是正确的“视觉上”正确,这里是任务列表的用法:
Description: This tool displays a list of currently running processes on either a local or remote machine. Parameter List: /S system Specifies the remote system to connect to. /U [domain\]user Specifies the user context under which the command should execute. /P [password] Specifies the password for the given user context. Prompts for input if omitted. /M [module] Lists all tasks currently using the given exe/dll name. If the module name is not specified all loaded modules are displayed. /SVC Displays services hosted in each process. /APPS Displays Store Apps and their accociated processes. /V Displays verbose task information. /FI filter Displays a set of tasks that match a given criteria specified by the filter. /FO format Specifies the output format. Valid values: "TABLE", "LIST", "CSV". /NH Specifies that the "Column Header" should not be displayed in the output. Valid only for "TABLE" and "CSV" formats. /? Displays this help message.
这是我到目前为止的python 代码 ;
#!/usr/bin/python """ Description: Used for checking users logged into a list of servers. Usage: hunter.py [-u <username>] [-p <password>] [-s <FILE>] (-d <domain>) hunter.py (-d <domain>) (-l) hunter.py -h | --help hunter.py --version Options: -l --local -u --username -h --help Show this screen. --version Show version. -p --password -d --domain -s --serverfile=FILE """ from docopt import docopt import subprocess from subprocess import CalledProcessError def tldomain(serverlist, domain, username, password): nlist = serverlist for serverl in nlist: try: print subprocess.check_output(["tasklist", "/V", "/S " + serverl, "/U" + domain, "\\" + username, "/P" + password, "/FI", "'USERNAME eq %s\\\*'"]) % domain except CalledProcessError as e: print(e.returncode) def tllocal(domain): try: cmd = 'tasklist /V /FI "USERNAME eq {0}\\*"' .format(domain) subprocess.call(cmd) except OSError as e: print e def getservers(servers): slist = open(servers).readlines() return [s.replace('\n', '') for s in slist] if __name__ == "__main__": arguments = docopt(__doc__, version='0.1a') print arguments if (arguments['--local']) == False: serverlist = getservers(arguments['--serverfile']) tldomain(serverlist, arguments['<domain>'], arguments['<username>'], arguments['<password>']) else: tllocal(arguments['<domain>'])
I am attempting to call a windows command with relevant arguments using python's
subprocess
. The command is executing and the arguments and their values look to be correct, however It only seems to be working correctly when using the "local mode"-l
. I'm getting aninvalid argument/option
error when using the remote mode. Could any point out where im going wrong?Could anyone point out how to format the
subprocess.check_ouput()
arguments correctly to include the variables given at commandline when executing the script? As you can see ive tryd using string formating, both old and new to try get it working as I cant workout how to add the lastdomain
variable inbetween the filter (/FI) argument value without string formatting.expected commandline to execute
tasklist /V /S 192.168.1.122 /U 'DOMAIN'\'USERNAME' /P 'PASSWORD' /FI "USERNAME eq 'DOMAIN'\*"
with this commandline example of the script:
hunter.py -d DOMAIN -u USERNAME -p PASSWORD -s servers.txt
This is the error:
ERROR: Invalid argument/option - '/S 192.168.1.122'. Type "TASKLIST /?" for usage.
Clearly the argument is correct "visually" correct anyway, here is the usage for the tasklist:
Description: This tool displays a list of currently running processes on either a local or remote machine. Parameter List: /S system Specifies the remote system to connect to. /U [domain\]user Specifies the user context under which the command should execute. /P [password] Specifies the password for the given user context. Prompts for input if omitted. /M [module] Lists all tasks currently using the given exe/dll name. If the module name is not specified all loaded modules are displayed. /SVC Displays services hosted in each process. /APPS Displays Store Apps and their accociated processes. /V Displays verbose task information. /FI filter Displays a set of tasks that match a given criteria specified by the filter. /FO format Specifies the output format. Valid values: "TABLE", "LIST", "CSV". /NH Specifies that the "Column Header" should not be displayed in the output. Valid only for "TABLE" and "CSV" formats. /? Displays this help message.
This is the python code i have so far;
#!/usr/bin/python """ Description: Used for checking users logged into a list of servers. Usage: hunter.py [-u <username>] [-p <password>] [-s <FILE>] (-d <domain>) hunter.py (-d <domain>) (-l) hunter.py -h | --help hunter.py --version Options: -l --local -u --username -h --help Show this screen. --version Show version. -p --password -d --domain -s --serverfile=FILE """ from docopt import docopt import subprocess from subprocess import CalledProcessError def tldomain(serverlist, domain, username, password): nlist = serverlist for serverl in nlist: try: print subprocess.check_output(["tasklist", "/V", "/S " + serverl, "/U" + domain, "\\" + username, "/P" + password, "/FI", "'USERNAME eq %s\\\*'"]) % domain except CalledProcessError as e: print(e.returncode) def tllocal(domain): try: cmd = 'tasklist /V /FI "USERNAME eq {0}\\*"' .format(domain) subprocess.call(cmd) except OSError as e: print e def getservers(servers): slist = open(servers).readlines() return [s.replace('\n', '') for s in slist] if __name__ == "__main__": arguments = docopt(__doc__, version='0.1a') print arguments if (arguments['--local']) == False: serverlist = getservers(arguments['--serverfile']) tldomain(serverlist, arguments['<domain>'], arguments['<username>'], arguments['<password>']) else: tllocal(arguments['<domain>'])
原文:https://stackoverflow.com/questions/22582168
最满意答案
我已经解决了这个问题。 实际上我没有意识到但问题是固定的。 问题解决后我做了Windows更新:)下面显示的更新可能有助于其他程序员。
I have solved this problem. Actually I did not consciously but probleb was fixed. I did Windows Updates after problem solved :) Updates shown below might be help another programmers.
相关问答
更多-
最后,我弄清楚我做错了什么。 虽然我需要一个例子来弄清楚...没有它,我永远不会弄明白,因为解决方案首先没有意义。 让我分享解决方案,万一你不知道。 首先,我的控制器类看起来像这样(在某些地方可能有不同的名字) public class FileUploadController : Controller { [HttpPost] public ActionResult UploadFiles(IEnumerable
files) { ... -
在我看来,在IE中出现了一个javascript错误。 问题似乎与水印有关。 巫婆导致javascript崩溃。 那么你的提交处理程序/验证不起作用。 所以问题是当加载水印的代码时,它没有链接到IE中的JQuery。 怎么修? 尝试更改脚本加载顺序或更新代码库http://code.google.com/p/jquery-watermark/downloads/list As I see it, in IE there's a javascript error that shows up. the prob ...
-
这个错误的常见原因是WebDAV 。 确保卸载它。 Common cause for this error is WebDAV. Make sure you uninstall it.
-
“翻译”的HTTP动词(HTTP verb for 'translate')[2024-02-22]
我正在学习POST,因为我看到PUT,PATCH,DELETE等作为特定选项。 GET将是“适当的”,因为它的起源是“获得一个给定的资源”,例如。 根据您解析的一些数据获取我的翻译。 但是我相信可以说,翻译不存在,而只是生成,因此你POST了你的字符串,web服务创建翻译,并返回它。 此外,正如您所提到的,GET请求被URL长度阻止。 I'm learning towards POST, as i see PUT, PATCH, DELETE etc. as specific options. GET wo ... -
确保您在发布网址( http://yoursite/app/Page.aspx而不是http:// yoursite / app )中包含网页文件名称 您可能需要明确允许POST到您网页的文件类型。 有关如何在IIS中执行此操作的说明,请参阅此处 。 (这些说明适用于IIS 6.对于IIS 7,只需转到处理程序映射,单击适当的条目的编辑,单击请求限制按钮,然后转到动词选项卡。) Make sure you are including the web page file name in the post UR ...
-
HTTP动词(GET,POST,...),包括( - >)或注释(#)(HTTP Verb (GET, POST, …), include (->) or comment (#) expected)[2022-07-31]
尝试换行: Get /ploegen controllers.Application.getPloegen() 至: GET /ploegen controllers.Application.getPloegen() Try to change line: Get /ploegen controllers.Application.getPloegen() to: G ... -
如何修复“405 - 不允许用于访问此页面的HTTP动词”?(How to fix “405 - HTTP verb used to access this page is not allowed”?)[2022-11-28]
事实证明,在网站的目录中是一个名为“web.config”的文件 当我复制网站的文件时,我不小心将其删除了。 此文件定义了用于我的* .py文件的处理程序。 (或您可能使用的任何其他语言文件)。 使用这个: https : //community.rackspace.com/products/f/25/t/484我已经为我的IIS添加了一个处理程序,它解决了这个问题 It turns out that in the directory that the website is was a file calle ... -
您正在向服务器发送GET请求而不是POST。 尝试: MultivaluedMap
formParams = new MultivaluedHashMap(); formParams.put("title", ...); formParams.put("isbn", ...); ... Boolean ok = target.path("rest"). path("hello/2"). reque ... -
我已经解决了这个问题。 实际上我没有意识到但问题是固定的。 问题解决后我做了Windows更新:)下面显示的更新可能有助于其他程序员。 I have solved this problem. Actually I did not consciously but probleb was fixed. I did Windows Updates after problem solved :) Updates shown below might be help another programmers.
-
不允许使用用于访问路径'/test.html'的HTTP动词POST(The HTTP verb POST used to access path '/test.html' is not allowed)[2022-03-28]
你使用任何URL重写? ( http://forums.asp.net/t/953470.aspx ) 在Web服务器中,POST是否允许HTML文件? 尝试将扩展名更改为.ASPX或.PHP Are you using any URL rewriting? (http://forums.asp.net/t/953470.aspx) In the webserver, is POST allowed for HTML files? Try changing the extension to .ASPX or ...