Solved

Ping host script

Posted on 2004-09-30
4
2,093 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ahh, thanks, thats a good start.  
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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…
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 …

728 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

9 Experts available now in Live!

Get 1:1 Help Now