【发布时间】:2012-09-12 17:17:56
【问题描述】:
我有一个 IP 列表,我想针对这些 IP 运行 whois(使用 linux 工具 whois)并且只看到 Country 选项。
这是我的脚本:
import os
import time
iplist = open('ips.txt').readlines()
for i in iplist:
time.sleep(2)
print "Country: IP {0}".format(i)
print os.system("whois -h whois.arin.net + {0} | grep Country ".format(i))
所以我想显示正在运行的 IP,然后我只想使用 grep 查看 Country 信息。当我运行它并且 grep 没有运行时,我看到了这个错误:
sh: -c: line 1: syntax error near unexpected token `|'
sh: -c: line 1: ` | grep Country '
下面的代码有效,所以它一定是我的 for 循环的问题:
print os.system("whois -h whois.arin.net + {0} | grep Country ".format('8.8.8.8'))
我做错了什么?谢谢!!!!
【问题讨论】:
-
出现错误之前在打印语句中打印了什么? (我猜你得到一个空字符串或像我一样的东西)
-
我看到输出就好像我运行了这个:'whois -h whois.arin.net + {0}'。它基本上忽略了'| grep 国家/地区'
-
我找到了一个便宜的解决方法...我可以删除 "| grep Country" 并添加到 "print os.system("echo Country: IP {0}".format( i))”,然后运行像“python whois.py | grep Country”这样的脚本
-
这部分... print "Country: IP {0}".format(i)