python - Using nslookup to find domain name and only the domain name -


currently have text file mutiple ip's attempting pull domain name set of information given using nslookup (code below)

with open('test.txt','r') f:     line in f:         print os.system('nslookup' + " " + line) 

this works in far pulls information first ip's. can't passed first ip i'm attempting clean information recived domain name of ip. there way or need use diffrent module

like igorn, wouldn't make system call use nslookup; use socket. however, answer shared igorn provides hostname. requestor asked domain name. see below:

import socket open('test.txt', 'r') f:     ip in f:         fqdn = socket.gethostbyaddr(ip)  # generates tuple in form of: ('server.example.com', [], ['127.0.0.1'])         domain = '.'.join(fqdn[0].split('.')[1:])         print(domain) 

assuming test.txt contains following line, resolves fqdn of server.example.com:

127.0.0.1 

this generate following output:

example.com 

which (i believe) op desires.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -