2

I am doing a simple ip scan using ping in python. I can run commands in parallel as demonstrated in this answer. However, I cannot suppress the output since it uses Popen, and I can't use check_output since the process returns with a exit status of 2 if a host is down at a certain ip address, which is the case for most addresses. Using a Pipe is also out of the question since too many processes are running concurrently.

Is there a way to run these child processes in python concurrently while suppressing output? Here is my code for reference:

def ICMP_scan(root_ip):
  host_list = []
  cmds = [('ping', '-c', '1', (root_ip + str(block))) for block in range(0,256)]
  try:
      res = [subprocess.Popen(cmd) for cmd in cmds]
      for p in res:
        p.wait()
  except Exception as e:
      print(e)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Stuart
  • 151
  • 7

1 Answers1

1

How about piping the process output to /dev/null.

Basing on this answer:

import os

devnull = open(os.devnull, 'w')

subproc = subprocess.Popen(cmd, stdout=devnull, stderr=devnull)
Phoenix
  • 1,553
  • 2
  • 13
  • 29
  • Thanks, specifying stdout suppresses output and also is able to run in parallel. However, in order to do anything with the ping responses, I have to specify the output stdout=somefile.txt, which means that I have to parse the data with regex after the operations. This is fine, but seems a little hacky... is there a better approach? if not, I will mark the answer complete – Stuart Sep 10 '19 at 01:32
  • @Stuart in your question, you said "Using a Pipe is also out of the question". when you're launching a subprocess, AFAIK your options are 1. inherit stdout (which your question is about avoiding) 2. pipe stdout (which you've said you don't want to do) 3. pipe it to a file (which is what you're talking about now) or 4. pipe it to a special file, like dev/null (which is what I demonstrate in my answer). does that clarify things? – Phoenix Sep 10 '19 at 02:04