1

I am working on a small IP address scanner, and am running into issues with the results from .poll(). I get a return from what I'm assuming is the router, so I get a 0 either way. I would've commented on a different post but I don't have the rep. So how can I check these results in a way that takes this into account?

import subprocess
from netaddr import *
import os
hosts = []
for ip in IPNetwork('192.168.14.157/22'):
    hosts.append(ip)
    p = subprocess.Popen('ping ' + str(ip))
    p.wait()
    print(p.poll())

idle output looks pretty much like:

0
1
0
0
3221225786 <When I exit a prompt before stopping the program

and cmd outputs are:

Pinging 192.168.12.5 with 32 bytes of data:
Reply from 192.168.14.157: Destination host unreachable.
Reply from 192.168.14.157: Destination host unreachable.
Reply from 192.168.14.157: Destination host unreachable.
Reply from 192.168.14.157: Destination host unreachable.

and 

Pinging 192.168.12.1 with 32 bytes of data:
Reply from 192.168.12.1: bytes=32 time=9ms TTL=64
Reply from 192.168.12.1: bytes=32 time=9ms TTL=64
Reply from 192.168.12.1: bytes=32 time=7ms TTL=64
Reply from 192.168.12.1: bytes=32 time=7ms TTL=64
Adam Smooch
  • 1,167
  • 1
  • 12
  • 27
  • What is your goal with this code ? What is expected output, what is the actual output ? – TheEagle Apr 26 '21 at 17:51
  • Yes, but not perfect : what is your problem ? – TheEagle Apr 26 '21 at 17:55
  • @Programmer I am eventually going to be printing out a list of usable addresses, but right now I am looking for a reliable way of checking ping results – Dad-Bod Steve Apr 26 '21 at 17:55
  • Does this answer your question? [Store output of subprocess.Popen call in a string](https://stackoverflow.com/questions/2502833/store-output-of-subprocess-popen-call-in-a-string) – Woodford Apr 26 '21 at 17:58
  • [This](https://stackoverflow.com/a/921441/14909980) may be helpful, too – TheEagle Apr 26 '21 at 18:01

0 Answers0