python 批量处理(通过ip地址获取机器名)
发布网友
发布时间:2023-11-02 12:27
我来回答
共1个回答
热心网友
时间:2024-11-26 13:49
import sys, socket
try:
ipLines = open('ip.txt').readlines()
results = []
for ip in ipLines:
result = socket.gethostbyaddr(ip.replace('\n',''))
results.append(result[0])
print "Primary hostname:"
print " " + result[0]
print "\nAddresses:"
for item in result[2]:
print " " + item
wr = open('hostname.txt','w+')
for host in results:
wr.write(host)
wr.write('\n')
wr.close()
except socket.herror, e:
print "Couldn't look up name:", e