Python Forum
How to print the IP which shows destination host unreachable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to print the IP which shows destination host unreachable (/thread-1534.html)



How to print the IP which shows destination host unreachable - MeeranRizvi - Jan-11-2017

Hello Guys,
Here i am writing a script to ping a certain range of ip in python.I just did with which ip is pingable and which ip is not pingable,i just need to print which ip shows destination host unreachable also.How to do that?
print "Enter the start ip:"
save=raw_input()
print "Enter the end ip:"
save1=raw_input()
start=int(save.split('.')[-1])
end=int(save1.split('.')[-1])
b=save.rsplit('.', 1)[0]

def ip():
    import os
    for i in range(start,end):
        IP= str(b)+'.'+str(i)
        a = os.system("ping -n 1 " + IP)
        if a == 0:
            print IP, 'is pingable'
        else:
            print IP, "is not pingable"
ip()            
         
Output:
Enter the start ip: 172.30.36.90 Enter the end ip: 172.30.36.95 IP STATUS: 172.30.36.90 is pingable 172.30.36.91 is pingable 172.30.36.92 is not pingable 172.30.36.93 is pingable 172.30.36.94 is pingable
When i ping 172.30.36.91 in windows cmd it shows Destination host unreachable.
Output:
C:\Users\Meeran>ping 172.30.36.91 Pinging 172.30.36.91 with 32 bytes of data: Reply from 172.30.36.93: Destination host unreachable. Reply from 172.30.36.93: Destination host unreachable. Reply from 172.30.36.93: Destination host unreachable. Reply from 172.30.36.93: Destination host unreachable. Ping statistics for 172.30.36.91:     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
i need the output like this
Output:
IP STATUS: 172.30.36.90 is pingable 172.30.36.91 is  Destination host unreachable. 172.30.36.92 is not pingable 172.30.36.93 is pingable 172.30.36.94 is Destination host unreachable.



RE: How to print the IP which shows destination host unreachable - buran - Jan-11-2017

You should use subrocess.check_output.

Note that this is also recommended in the docs for os.system:

Quote:subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.



RE: How to print the IP which shows destination host unreachable - wavic - Jan-11-2017

Hello!

You have to check which system you run the script and do the ping the right way. On Windows pings counting option key is -n but -c in Linux

See: https://docs.python.org/2/library/sys.html#sys.platform


RE: How to print the IP which shows destination host unreachable - buran - Jan-11-2017

(Jan-11-2017, 10:11 AM)wavic Wrote: On Windows pings counting option key is -c not -n

Actually that's not true, at least on Win7 :-)
the option switch is -n


RE: How to print the IP which shows destination host unreachable - wavic - Jan-11-2017

(Jan-11-2017, 10:18 AM)buran Wrote: Actually that's not true, at least on Win7 :-)
the option switch is -n
Hmm! Since Windows Vista according to this. I didn't use Windows for many years. May be I have to install it as one of my systems


RE: How to print the IP which shows destination host unreachable - buran - Jan-11-2017

Well, did you check your link?
There is no -c option in either version... In both cases: -n count Number of echo requests to send.


RE: How to print the IP which shows destination host unreachable - Larz60+ - Jan-11-2017

See http://www.opentechguides.com/how-to/article/python/57/python-ping-subnet.html for sample code


RE: How to print the IP which shows destination host unreachable - wavic - Jan-11-2017

(Jan-11-2017, 11:04 AM)buran Wrote: Well, did you check your link?
There is no -c option in either version... In both cases: -n count Number of echo requests to send.

Well, -c is in Linux. Where is my mind  Dodgy

Editing the prev. post