0
一直在玩這個。還沒有清理它。它基本上做nslookup做的。從文本文件中獲取(或多個)主機名並返回相應的IP地址。從多個字符串提取特定數據
def getIPAddresses(file):
emptyList = []
someString = ".com"
with open(file, "r+") as output:
for line in output:
emptyList.append(line)
my_new_list = [x.strip() + someString for x in emptyList]
return(my_new_list)
def lookup():
try:
while True:
enterSomething = input("Enter something: ")
if os.path.exists(enterSomething):
for n in getIPAddresses(enterSomething):
ip_info = socket.getaddrinfo(n, 80)
b = list(["nslookup of " + str(n) +"\n"] + ip_info +(["\n" + "Result of " +str(n)]))
values = ''.join(str(v) for v in b)
abc = values+ "\n"
print()
print(abc)
break
elif not os.path.exists(enterSomething):
print("No such file or directory " + str(enterSomething))
except(UnicodeError):
print("")
所以,如果我有以下的文本文件:
www.google
www.espn
它將返回
nslookup of www.google.com
(<AddressFamily.AF_INET: 2>, 0, 0, '', ('172.217.1.68', 80))(<AddressFamily.AF_INET6: 23>, 0, 0, '', ('2607:f8b0:4006:808::2004', 80, 0, 0))
Result of www.google.com
nslookup of www.espn.com
(<AddressFamily.AF_INET: 2>, 0, 0, '', ('199.181.133.5', 80))
Result of www.espn.com
我的問題是,是有辦法,我只能提取相應的www.google.com等ip地址,例如172.217.1.68
和2607:f8b0:4006:808::2004
等?
你怎麼樣只打印接口的信息,而不是做所有的字符串連接? –