Solved

Ping host script

Posted on 2004-09-30
4
2,098 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now