Skip to content
Advertisement

Python, using basic unix command and list of hosts

I am trying to get the output of a list of hosts, using the unix command nslookup + the list of hosts, but I am getting an error although it does work when I only have one host.

Is there an easier way to do what I am doing or can you help me fix this simple script?

My script is:

#!/usr/bin/python

import commands, os, string

hostname = ['host1', 'host2']
response = commands.getoutput("nslookup " + ' '.join(hostname))
print response

Error:

user@hostname ~/scripts> ./nslookup
^CTraceback (most recent call last):
  File "./nslookup", line 6, in <module>
    response = commands.getoutput("nslookup " + ' '.join(hostname))
  File "/usr/lib64/python2.6/commands.py", line 46, in getoutput
    return getstatusoutput(cmd)[1]
  File "/usr/lib64/python2.6/commands.py", line 56, in getstatusoutput
    text = pipe.read()
KeyboardInterrupt

Advertisement

Answer

Try this:

#!/usr/bin/python

import commands, os, string

hostnames = ['host1', 'host2']

for hostname in hostnames:
    response = commands.getoutput("nslookup " + hostname)
    print response

(http://linux.die.net/man/1/nslookup)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement