简体   繁体   中英

My ping sweep program is not working. What am I doing wrong?

I have written a ping sweep program in Python3.

import os
import platform
from datetime import datetime

net = input('Enter the network address: ')

net1 = net.split('.')

a = '.'

net2 = net1[0] + a + net1[1] + a + net1[2] + a

st1 = int(input('Enter the starting number: '))

en1 = int(input('Enter the last number: '))

en1 += en1

oper = platform.system()

if(oper == 'Windows'):
    ping1 = 'ping -n 1 '
elif(oper == 'Linux'):
    ping1 = 'ping -c 1 '
else:
    ping1 = 'ping -c 1 '

starttime = datetime.now()
print('Scanning in process...')

for ip in range(st1, en1):
    addr = net2 + str(ip)
    command = ping1 + addr
    response = os.popen(command)
    for line in response.readlines():
        if (line.count('TTL')):
            print(addr, ' ---> Live')
            break

endtime = datetime.now()
totaltime = endtime-starttime
print('Scanning was completed in ', totaltime)

Whenever I put in the required inputs it only prints out "Scanning in process" and stays like that until I exit the program. I am on Linux Mint using this on the command line, what is wrong with my code?

As I see it, there are four problems with your code.

Pings that fail are taking too long to fail

On my Linux system (openSuSE), the ping timeout is 10 seconds by default. It's 5 seconds on Windows. Your code is probably not hanging, just spending a lot of time waiting for pings to time out.

You can change the timeout of the Linux ping utility by using the command-line parameter -W to specify a timeout in seconds. Note that the timeout must be a whole number of seconds; a timeout of, say, 0.5 will be interpreted as 0 and the ping will appear to wait forever. On Windows you specify a timeout using the -w parameter, but the timeout period is in milliseconds as opposed to seconds with Linux.

Pings that succeed are not being reported as succeeding

Here's the output from a successful ping:

$ ping -c 1 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.046 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms

Look carefully at this output and at the code you use to check whether the target IP address was up. Notice anything?

Your check for the address being up is in the line if (line.count('TTL')): , in other words, you need a line of the output to contain TTL . No lines of the output above contain TTL , so your code deems this a failure.

You probably want to check for the string TTL appearing case-insensitively . The output doesn't contain TTL but it does contain ttl . So try changing if (line.count('TTL')): to if (line.upper().count('TTL')): .

Pings that fail are not being reported as failing

Your code prints out whether pings succeed, but doesn't print out anything if they fail. I can appreciate that once you've got this script working you might not need it to print out failed pings, but at least while developing and debugging it it's useful to have this information.

Fortunately it's easy to add: add the following lines after break :

    else:
        print(addr, ' ---> Down')

(the else should line up with the inner for , customise the message as you wish). The code in the else block executes if the for loop didn't break, ie there were no lines that contained TTL .

You are pinging too many addresses

Your code contains the line

en1 += en1

which has the effect of doubling en1 .

You probably meant to write en1 += 1 , so that the end address gets included in the loop for ip in range(st1, en1): .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM