ont

Command line arguments/pipe sniffer

ont
Jun 28th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/python2
  2. import os
  3. import sys
  4. import fcntl
  5. import subprocess
  6. import select
  7.  
  8. f = open('/tmp/sendmail.args', 'a')
  9. f.write('-->>> ' + ' '.join(sys.argv) + '\n')
  10. f.flush()
  11.  
  12. p = subprocess.Popen(['/usr/sbin/sendmail.backup'] + sys.argv[1:], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  13.  
  14. def setNonBlocking(fd):
  15.     flags = fcntl.fcntl(fd, fcntl.F_GETFL)
  16.     flags = flags | os.O_NONBLOCK
  17.     fcntl.fcntl(fd, fcntl.F_SETFL, flags)  
  18.  
  19. setNonBlocking(sys.stdin.fileno())
  20. setNonBlocking(p.stdout.fileno())
  21. setNonBlocking(p.stderr.fileno())
  22.  
  23. while True:
  24.     rr, _, _ = select.select([sys.stdin, p.stdout, p.stderr], [], [])
  25.  
  26.     for r in rr:
  27.         data = r.read()
  28.  
  29.         if not data:
  30.             exit(0)
  31.  
  32.         if r == sys.stdin:
  33.             f.write('>>> ' + data)
  34.             f.flush()
  35.             p.stdin.write(data)
  36.  
  37.         if r == p.stdout:
  38.             f.write('<<< ' + data)
  39.             f.flush()
  40.             sys.stdout.write(data)
  41.             sys.stdout.flush()
  42.  
  43.         if r == p.stderr:
  44.             sys.stderr.write(data)
  45.             sys.stderr.flush()
  46.  
  47. f.close()
Advertisement
Add Comment
Please, Sign In to add comment