Solved

Ping host script

Posted on 2004-09-30
4
2,103 Views
Last Modified: 2012-06-27
Hi, I am confused on how to get the ping.py module working.

Could someone write me a simple script that will ping a host, let's say www.yahoo.com and return the result to the command line using the ping.py module?  

I am using windows xp and have installed the ping.py module I downloaded from
http://www.python.org/~jeremy/python.html

Thanks
0
Comment
Question by:Airgazm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:drush_koders
ID: 12196104
There is a more comprehensive ping module that is part of the QOS project - which incidentally can monitor a lot more than just responsiveness to IP traffic. (If you need it)

In any event, here is a snippet that times a ping:
        pingTime = time.time()
        # ping first
        result = 0
        for i in range(3):
            try:
                result = ping2.ping(se['pingHost'], 2)
            except:
                pass
            if (result == 1):
                break

        pingTime = time.time() - pingTime
        pingCount = i
        se['WpingTime'] = '%0.3f' % pingTime
        se['WpingCount'] = '%0d' % pingCount
        if (result != 1):
            se['Wstatus'] = 'P'
            se['Wmesg']   = "Can't ping"
            return

You can see the rest of the code here:
http://www.koders.com/python/fid15C0979D51B9552AB3DAC00F7AF344460B4DF4DD.aspx?s=ping+host+IP





0
 

Author Comment

by:Airgazm
ID: 12196828
I'm just experimenting with Python, (very new to it) and wanted to write a very simple program that pings a few hosts, so I thought I would start small.

I am trying to make heads or tails of the above code.  Should there be some import statements, for ping and system, etc in there?  I can't get it to work correctly.  I get an error "se not defined".

Couldn't we trim the script down to a very simple ping just one host

I was figuring it was as simple as something like

# testping.py
import sys, ping
responce = ping('www.yahoo.com')
print responce

Thanks, and sorry for being a newb. :)
0
 
LVL 1

Accepted Solution

by:
drush_koders earned 500 total points
ID: 12197854
If you're having problems integrating other modules, then another way to approach the problem is to use the existing "os" module to call the native Windows ping function. Here's a simple one-liner that spits out the output of a native call to ping. It should work out of the box.

#!/usr/bin/python
import os
print os.popen('ping yahoo.com -n 1').read()


I found another example of the same approach at koders:
http://www.koders.com/python/fidC5707BAF53E9DE7FBEB1AEF0310D35E298C14444.aspx?s=popen+ping

Notes:
The "-n 1" argument to ping is to tell it to only send one ping packet.
0
 

Author Comment

by:Airgazm
ID: 12197899
Ahh, thanks, thats a good start.  
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question