Solved

Ping host script

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 …
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…
Suggested Courses

738 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