[Tutor] Finding Hostname-IP DNS mappings and if machine is dead or alive

Danny Yoo dyoo at hashcollision.org
Tue May 13 01:36:17 CEST 2014


On Mon, May 12, 2014 at 2:44 PM, KIRAN D.S.
<kirands at yahoo.com.dmarc.invalid> wrote:
> Hi,
>
> I have a UNIX shell script that:
> a.  lists out the Hostname-IP DNS mappings
> b.  checks whether the machine is pingable, and prints "dead" or "alive"
> depending on status.

There will be a few things you'll want to look at.  In particular, if
you can avoid calling out to the shell, that would be nice.


To get the host name given its IP address, you can use
socket.gethostbyaddr(), which does that lookup for you:

    https://docs.python.org/2/library/socket.html#socket.gethostbyaddr
    http://stackoverflow.com/questions/2575760/python-lookup-hostname-from-ip-with-1-second-timeout


To check dead-or-liveness of the site, you could shell out to ping.
See the subprocess library for details on how to do that:

    https://docs.python.org/2/library/subprocess.html

But alternatively, there may be native ping libraries out there for
Python that you can reuse.  A Google search for the term "Python Ping"
hits a few pure-Python ping libraries that people have written.


For command line processing, look into argparse:

    https://docs.python.org/2/howto/argparse.html#id1



If you can say more of where you're getting stuck, we can give more
specific advice.


More information about the Tutor mailing list