Just like the title says, I need to be pointed in the right direction on this. I'm not looking for "Here's your code" (I recently began coding in Python, so my understanding is at very basic level)
But basically I will have a txt file full of hostnamnes (one on each row) that I need to ping to see if they are 1. Active and 2. What the IP-address of that active hostname is. And then I need the program to output the hostname + IP to a txt file.
I read up a bit on the subject and it seems that subprocess is the way to go. Time isn't really relevant so I don't need it to multi thread.
So in conclusion, any good tips on where to begin (so that I may understand what I'm typing)?
EDIT:: So this is how far i have gotten:
import ping, socket
hostsFile = open('hostnames.txt', 'r')
lines = hostsFile.readlines()
addr = socket.gethostbyname(lines)
for lines in hostsFile:
print 'IP-address of', hostname
print 'is', addr
try:
ping.verbose_ping(count=3)
delay = ping.Ping(timeout=2000).do()
except socket.error, e:
print "Ping Error:", e
It now returns an error referring to the addr = socket.gethostbyname(lines) line, saying: "TypeError: must be string, not list"
While i do understand the error somewhat, i have no idea how to get around it.
You can use ping and socket modules of python to achieve the desired task. Socket module has a function gethostbyname() which takes hostname and return its ip address.
The relevant code is shown below, You can implement the functionality of reading from and writing to the text file in this code.
import ping, socket
hostname = 'maps.google.com'
addr = socket.gethostbyname(hostname)
print 'The address of ', hostname, 'is', addr
try:
ping.verbose_ping('www.google.com', count=3)
delay = ping.Ping('www.wikipedia.org', timeout=2000).do()
except socket.error, e:
print "Ping Error:", e