[英]Reading lines of a file into a subprocess in Python
So I'm trying to read a list of IP addresses from a .txt into a subprocess (Nmap) in Python. 因此,我正在尝试将.txt中的IP地址列表读入Python的子进程(Nmap)中。 It's also worth asking if the problem could be the use of quotes or not.
还值得一问的是问题是否可能是使用引号引起的。 Here's the code:
这是代码:
addressFile = raw_input("Input the name of the IP address list file. File must be in current directory." )
fileObj = open(addressFile, 'r')
for line in fileObj:
strLine = str(line)
command = raw_input("Please enter your Nmap scan." )
formatCom = shlex.split(command)
subprocess.check_output([formatCom, strLine])
Trusty error message: 可信任的错误消息:
Traceback (most recent call last):
File "mod2hw7.py", line 15, in <module>
subprocess.check_output([formatCom, strLine])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 566, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
AttributeError: 'list' object has no attribute 'rfind'
shlex.split
returns a list; shlex.split
返回一个列表; you should catenate this list with 1 element list containing strline
when building command line arguments: 在构建命令行参数时,应使用包含
strline
1个元素列表将此列表分类:
formatCom = shlex.split(command)
subprocess.check_output(formatCom + [strLine])
The error occurs because instead of 发生错误是因为
subprocess.check_output([ 'nmap', '-sT', '8.8.8.8' ])
you are executing something like 你正在执行类似
subprocess.check_output([ ['nmap', '-sT'], '8.8.8.8' ])
and subprocess
expects to be given a list of strings, not nested lists. 并且
subprocess
期望得到一个字符串列表,而不是嵌套列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.