#!/usr/bin/python -u """ Waits for Nmap to start and records the number of file descriptors it has open to the file specified as its command-line argument. Once Nmap dies, the program terminates. """ import time import os import subprocess import sys with open(sys.argv[1], "w") as f: while True: p = subprocess.Popen("pgrep nmap", shell=True, stdout=subprocess.PIPE) pid_out = p.communicate()[0] if not pid_out: continue else: pid = pid_out.rstrip("\n") break try: while True: f.write("%s\n" % (len(os.listdir('/proc/%s/fd' % pid)))) f.flush() time.sleep(0.1) except OSError: pass