hi all..

I need to issue a 'ping' to an ip address via my script and check whether it is up?

can I issue it using os.system? can I capture the output and analyze to see if the host is up??

@Gribouillis:
Thanks a lot :)
my further question, I have the output string now, but I don't want manual interruption to check the output, i.e., script itself should read the output and decide whether the host is up or not??

any way to do this?

@Gribouillis:
Thanks a lot :)
my further question, I have the output string now, but I don't want manual interruption to check the output, i.e., script itself should read the output and decide whether the host is up or not??

any way to do this?

Then you must write specific code to parse the output string and extract the desired information.

ok... I'll try doing that.. once again thanks a lot for the help :)

one more doubt..
what is com.failed returning here?? can u pls explain ?

am am getting this error message:

TypeError: default __new__ takes no parameters

what is it saying?

one more doubt..
what is com.failed returning here?? can u pls explain ?

am am getting this error message:

TypeError: default __new__ takes no parameters

what is it saying?

com.failed is only an integer (same as com.returncode). Please post the code that throws the exception and the full traceback.

This is the code:

#!/usr/bin/python

import subprocess
def main():
    class Command(object):
        #-- _init_() is first piece of code executed in a newly created instance of class
	#-- self refers to the current instance of the class
	def _init_(self,command):
            self.command = command
	def run(self, shell= True):
            process = subprocess.Popen(self.command,shell = shell,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
	    self.pid = process.pid
	    self.output, self.error = process.communicate()
	    self.failed = process.returncode
	    return self
	def returncode(self):
            return self.failed

    com = Command("ping 192.168.5.183").run()
    print com.output
    print com.error
    print com.failed

if __name__ == '__main__':
    main()

and this is the trace back:

Traceback (most recent call last):
  File "D:\Python24\ping.py", line 25, in -toplevel-
    main()
  File "D:\Python24\ping.py", line 19, in main
    com = Command("ping 192.168.5.183").run()
TypeError: default __new__ takes no parameters

This is the code:

#!/usr/bin/python

import subprocess
def main():
    class Command(object):
        #-- _init_() is first piece of code executed in a newly created instance of class
	#-- self refers to the current instance of the class
	def _init_(self,command):
            self.command = command
	def run(self, shell= True):
            process = subprocess.Popen(self.command,shell = shell,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
	    self.pid = process.pid
	    self.output, self.error = process.communicate()
	    self.failed = process.returncode
	    return self
	def returncode(self):
            return self.failed

    com = Command("ping 192.168.5.183").run()
    print com.output
    print com.error
    print com.failed

if __name__ == '__main__':
    main()

and this is the trace back:

Traceback (most recent call last):
  File "D:\Python24\ping.py", line 25, in -toplevel-
    main()
  File "D:\Python24\ping.py", line 19, in main
    com = Command("ping 192.168.5.183").run()
TypeError: default __new__ takes no parameters

You wrote _init_ with single underscores, instead of __init__ . Otherwise, you are using python 2.4 which is old, I'm not sure that the class will work. Why don't you upgrade to 2.7 ? Also, the normal way to write classes is at module level, not in functions. It would be better to move the Command class out of main() (why didn't you simply push the Toggle Plain Text button in the code snippet and copy/paste the code ?) Notice that in the snippet, "returncode" is a property, not a method.

ok....

thanks a lot.. yes i'll do as u said... I have written a simple code which serves my purpose. It may seem very crude, but it's helping me.. here it is:

os.system("ping 192.168.5.181> ping_out.txt")

fp=open("ping_out.txt","r")

if((fp.readlines()[9].split(",")[2])== ' Lost = 0 (0% loss)'):
    print"***Ping Succeeded.....Host reachable***"
else:
    print "***Ping Failed.....Host not reachable***"

@ Gribouillis:
can the above be used??

@ Gribouillis:
can the above be used??

Oh, yes, it can be used if you don't ping too often. It's only less efficient than subprocess.Popen() with a stdout=subprocess.PIPE argument, because you are writing to your hard drive each time.

About the parsing of the result, it doesn't look very robust, unless you know that the information you need is always the 3rd item of the 10th line. A search with regex would probably be better.

Edit: don't you need some options for the call to ping ? When I use ping on my computer, it seems to print indefinitely.

Ok...

ping address -t pings indefinitely until we close the terminal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.