Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

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.
Quote
#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.
Quote
#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
Quote
#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
Quote
#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
Quote
#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.
Quote
#7
See http://www.opentechguides.com/how-to/art...ubnet.html for sample code
Quote
#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
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)