0

I wanted to make a very simple network scanner in python, and I wrote this:

import subprocess
ip_range = input("Enter ip range:")
start_ip = input("Enter start ip:")
end_ip = input("Enter end ip:")
result = "Result:"
for ping in range(int(start_ip), int(end_ip)):
    ip = ip_range + str(ping)
    connect = subprocess.call(["ping", "-c1",ip])
    if connect == 0:
        result += "---"
        result += "Ping to ",ip,"was OK"
        result += "---"
    else:
        result + "Couldn't ping to " + str(ip)
        result + "---"
        pass
print(result)

All of them work correct except one thing. In this part

result + "Couldn't ping to " + str(ip)
result + "---"

The result doesn't add up to this code, I mean it just write result.

2
  • Look closely at the difference between the two branches of your if statement. You use two different operators. Commented Apr 9, 2020 at 14:28
  • result += "Couldn't ping to " + str(ip) result += "---" Commented Apr 9, 2020 at 14:28

1 Answer 1

1

result + "Couldn't ping to " + str(ip) is an expression, not a statement. You don't assign that value to result. Try changing the + to +=. That will store the value result + "Couldn't ping to " + str(ip) in result.

Sign up to request clarification or add additional context in comments.

3 Comments

I tries += instead of + but the answere will be replace with the last value and doesn't add together.
@HoseinAzimi I don't understand your comment. Can you be more explicit about what code you are using and the output you are expecting?
I wanna user input a ip range and then script go and ping the ips and know what ips are up and what ips are down,then add result to a string variable for example: ip_range:192.168.1.100 - 192.168.1.110 ping all of them was ok result:--Ping to 192.168.1.100 was OK------Ping to 192.168.1.101 was OK------ etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.