Pages

Python Script: Check Ping Status and Lookup Hostname from IP List

Today I got a request to disable insecure SNMP v1 / v2c from a list of IP addresses. If you are still using SNMP v1 / v2c then it's really the time to disable the version and configure secured SNMP v3. Sometime ago I wrote a post on how to install and configure Net-SNMP v3 in Red Hat Enterprise Linux 4, you might wanna check that out as well. So, I had no idea what these servers are because I like to memorize my servers with their host name. And I didn't want to run nslookup or host or dig one by one on each IP. Also, I wanted to check whether these IP are alive / ping-able on my network or not. I could have used for loop and made one complicated command but for couple of reasons I chose not to do that,

  • I might get a long list of IPs again and writing one time long complicated command using for loop may not be the best way.

  • I had a list of few hundred IPs and for loop is slow and I wanted something fast.

So I decided to write a script hostlookup.py in Python and using it's multiprocessing module to make things fast. I encourage you to please download it from Bitbucket and give it try and don't hesitate to make a comment on this post if you have any issue or some suggestions to improve the script.


Usage: hostlookup.py [ -i ip-address] [ -f filename]

You may run the script with option -h to check the usage,

$ hostlookup.py -h

You may run the script with option -i by giving one IP address,

$ hostlookup.py -i 8.8.8.8

If you have a list of IPs then simply put them in a file (one IP per line) and run the script with -f option by giving the file name and file path,

$ hostlookup.py -f /home/username/ip-list.txt


This post appeared on the softlexicon.com by Sumit Goel. Copyright © 2012–2013 – softlexicon.com and Sumit Goel. All rights reserved. Not to be reproduced for commercial purposes without written permission.

7 comments:

  1. github link is not working..

    ReplyDelete
  2. Hi Nishant,

    The GitHub links are working for me. If you are trying to file an issue then you need to login at GitHub first otherwise you should be able to view the script just fine. Please let me know if you continue to face the issue.

    Sumit Goel

    ReplyDelete
  3. Hi,

    I am trying to run this script on Redhat Linux 9 32 bit. It throws me an error that ImportError: No module named multiprocessing. Please guide me how to solve this.

    ReplyDelete
  4. RK, It seems that you don't have Python Multiprocessing module installed on the system. Please use easy_install or pip to install the python-multiprocessing. What version of Python are you using?

    $ python -V

    - Sumit Goel

    ReplyDelete
  5. I've got similar problem to RK user :) but fortunately your answer Sumit :) all working fine right now. Thanks for sharing!

    ReplyDelete
  6. cannot run on windows ! Python 3.3
    though commented the:
    ### except ImportError, e:
    ### print ('ImportError:'), str(e)
    ### sys.exit(1)

    and used nul instead of /dev/null

    Help appreciated much , Sumit !!! And thanks for sharing such a handy script.

    ReplyDelete

Be sure to check back again because I do make every effort to reply to your comments here.