我有我需要 nslookup 并发送到 csv 的地址列表
I have list of adresses which i need to nslookup and send to csv
我正在尝试对 adrese.txt 文件中的地址执行 nslookup,我想将它们保存为 .csv。目前我最大的问题是它只对一个地址而不是所有地址执行 nslookup。它只是以 0 退出,在我的文件中只有一个地址。我是 python 的新手,不知道如何修复它。在输出文件中用 csv 替换 .txt 也很好。
编辑:从文本文件获取地址有效,第二部分是问题,不知道为什么
import subprocess
f = open("adrese.txt")
next = f.read()
ip=[]
while next != "":
ip.append(next)
next = f.read()
file_ = open('nslookup.txt', 'w')
for i in ip:
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0]
file_.write(output)
file_.close()
之所以这样做是因为 while next != ""
没有按照您的意愿去做。
相反,考虑一下:
import subprocess
with open('adrese.txt') as i, open('nslookup.txt', 'w') as o:
for line in i:
if line.strip(): # skips empty lines
proc = subprocess.Popen(["nslookup", line.strip()],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
o.write('{}\n'.format(proc.communicate()[0]))
print('Done')
您实际上并没有遍历 adrese.txt
中的所有条目
ip = []
f = open("adrese.txt")
for line in f:
ip.append(line)
f.close()
file_ = open('nslookup.txt', 'w')
for i in ip:
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0]
file_.write(output)
file_.close()
您可以使用 check_call 并将标准输出直接重定向到文件:
import subprocess
with open('adrese.txt') as f, open('nslookup.txt', 'w') as out:
for line in map(str.rstrip, f):
if line: # skips empty lines
subprocess.check_call(["nslookup", line],
stdout=out)
你永远不会使用 stderr 所以捕获它是没有意义的,如果有任何非零退出状态你可以捕获 CalledProcessError
:
import subprocess
with open('adrese.txt') as f, open('nslookup.txt', 'w') as out:
for line in map(str.rstrip, f):
if line: # skips empty lines
try:
subprocess.check_call(["nslookup", line],
stdout=out)
except subprocess.CalledProcessError:
pass
我正在尝试对 adrese.txt 文件中的地址执行 nslookup,我想将它们保存为 .csv。目前我最大的问题是它只对一个地址而不是所有地址执行 nslookup。它只是以 0 退出,在我的文件中只有一个地址。我是 python 的新手,不知道如何修复它。在输出文件中用 csv 替换 .txt 也很好。
编辑:从文本文件获取地址有效,第二部分是问题,不知道为什么
import subprocess
f = open("adrese.txt")
next = f.read()
ip=[]
while next != "":
ip.append(next)
next = f.read()
file_ = open('nslookup.txt', 'w')
for i in ip:
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0]
file_.write(output)
file_.close()
之所以这样做是因为 while next != ""
没有按照您的意愿去做。
相反,考虑一下:
import subprocess
with open('adrese.txt') as i, open('nslookup.txt', 'w') as o:
for line in i:
if line.strip(): # skips empty lines
proc = subprocess.Popen(["nslookup", line.strip()],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
o.write('{}\n'.format(proc.communicate()[0]))
print('Done')
您实际上并没有遍历 adrese.txt
中的所有条目ip = []
f = open("adrese.txt")
for line in f:
ip.append(line)
f.close()
file_ = open('nslookup.txt', 'w')
for i in ip:
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0]
file_.write(output)
file_.close()
您可以使用 check_call 并将标准输出直接重定向到文件:
import subprocess
with open('adrese.txt') as f, open('nslookup.txt', 'w') as out:
for line in map(str.rstrip, f):
if line: # skips empty lines
subprocess.check_call(["nslookup", line],
stdout=out)
你永远不会使用 stderr 所以捕获它是没有意义的,如果有任何非零退出状态你可以捕获 CalledProcessError
:
import subprocess
with open('adrese.txt') as f, open('nslookup.txt', 'w') as out:
for line in map(str.rstrip, f):
if line: # skips empty lines
try:
subprocess.check_call(["nslookup", line],
stdout=out)
except subprocess.CalledProcessError:
pass