1
import os

ipRange = []
for i in range(1, 254):
    ipRange.append('192.168.5' + '.' + str(i)) 

for e in ipRange:
    print os.system('nslookup ' + str(e))

This outputs the full output of nslookup for each ip - is there a way to discard the empty results and make the output look more like this?

192.168.5.5 testbox4
192.168.5.6 box3
192.168.5.8 hellobox
192.168.5.9 server2012
192.168.5.18 dnsbox
192.168.5.19 sallysbox
192.168.5.20 bobsbox
192.168.5.21 serverx
Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
S F
  • 69
  • 1
  • 5

1 Answers1

2

Do you need to use system? This would do without system calls:

import socket

for i in range(0, 255):
    ipa = "130.233.192." + str(i)
    try:
        a = socket.gethostbyaddr(ipa)
        print (ipa, a[0])
    except socket.herror:
        pass

EDIT: change 255 to 256 if you want to query .255 as well but in class C networks this is the broadcast address and not in DNS. If you are trawling through class A or B networks, then .255 could be valid as well

Hannu
  • 11,685
  • 4
  • 35
  • 51