3

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)

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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 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?

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.