1
votes

the code that i currently use

    from pythonping import ping
import random

while 1:
     d1 = (random.randrange(1,255))
     d2 = (random.randrange(1,255))
     d3 = (random.randrange(1,255))
     d4 = (random.randrange(1,255))
     h = f'{d1}.{d2}.{d3}.{d4}'
     ping(h, verbose=True)

and the output is

Request timed out Request timed out ....

Request timed out

Traceback (most recent call last): File "C:\Users\dwatn\Documents\document1.py", line 17, in ping(h, verbose=True) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping_init_.py", line 78, in ping comm.run(match_payloads=match) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping\executor.py", line 335, in run payload_bytes_sent = self.send_ping(identifier, seq, payload) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping\executor.py", line 277, in send_ping self.socket.send(i.packet) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping\network.py", line 56, in send self.socket.sendto(packet, (self.destination, 0)) OSError: [WinError 10051] A socket operation was attempted to an unreachable network

1
This can generate IPs with an invalid network part, and in this case did after a few iterations, is that what you mean?robinsax
yes is there a way to keep it running the code or somthing elsecoder

1 Answers

2
votes

You can use try catch to handle exceptions.

from pythonping import ping
import random

while 1:
     d1 = (random.randrange(1,255))
     d2 = (random.randrange(1,255))
     d3 = (random.randrange(1,255))
     d4 = (random.randrange(1,255))
     h = f'{d1}.{d2}.{d3}.{d4}'
     try:
         ping(h, verbose=True)
     except:
         print("invalid ip")