I am working on a project to help me learn python. I realize that I am probably reinventing the wheel, as there is probably a ping module out there. If so please don't advise this, as this project is to help me learn python.
I wont go in detail with my entire project. At the present moment, I am simply trying to assign the output of the ping command to a variable. For the most part I am only interested in a certain portion of the ping output. The code works fine when the address exist. So my question is how can I fix this so that it works when the the network address does not exist and ping returns negative results?
#! /usr/bin/perl
import subprocess
p1 = subprocess.check_output("ping -q -o -t 4 192.168.1.113", shell=True)
ping1 = p1[131:137]
print ping1
The results are as follows
>>> ================================ RESTART ================================
>>>
0.0%
>>>
When the IP Address does not exist, I get the following:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "/Users/dmartin/scripts/python/netscan/netscanv2.py", line 6, in <module>
p1 = subprocess.check_output("ping -q -o -t 4 192.168.1.114", shell=True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command 'ping -q -o -t 4 192.168.1.114' returned non-zero exit status 2