1

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.

  • Look at the following post for the first bit: http://stackoverflow.com/questions/15558123/python-get-hostname-values-from-each-line-etc-hosts – Alex Nov 06 '15 at 14:30
  • And this post for pinging a server in Windows/Linux: http://stackoverflow.com/questions/2953462/pinging-servers-in-python – Alex Nov 06 '15 at 14:32

1 Answers1

2

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
Nishant Srivastava
  • 379
  • 1
  • 2
  • 12
  • This is really helpful! I just need to figure out how to take the input from the file. But i think i will figure that part out. Thank you very much. – John James Smith Doe Nov 23 '15 at 13:02
  • i installed pure python ping into the library and tweaked my code a bit, im getting a TypeError: must be string, not list. on: **addr = socket.gethostbyname(lines)** 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 – John James Smith Doe Nov 23 '15 at 14:35
  • so sorry for this, in my foolishness i thought break row would work. it did not. – John James Smith Doe Nov 23 '15 at 14:42