Solved

Ping host script

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IronPython and Visual Studio 2013+ 2 105
looping 11 37
Python error - Need Help 12 108
I cannot read information from Windows Registry using python on windows 8.1 2 81
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

856 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