Python Forum
How to print the IP which shows destination host unreachable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print the IP which shows destination host unreachable
#1
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.
Reply
#2
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.
Reply
#3
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(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
Reply
#5
(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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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.
Reply
#7
See http://www.opentechguides.com/how-to/art...ubnet.html for sample code
Reply
#8
(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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020