Python capture command send to unix, capture output and write to file -


import subprocess  cmd = 'ifconfig -a'  p = subprocess.popen(cmd, shell=true, stderr=subprocess.pipe)  while true:     out = p.stderr.read(1)     if out == '' , p.poll() != none:         break     if out != '':         sys.stdout.write(out)         sys.stdout.flush()        <<<how cmd sent , it's output>>>         file = open('outputfile.txt', 'w+')         file.write(out)         file.close() 

from subprocess import popen, pipe, stdout cmd = ["ifconfig", "-a"]  p = popen(cmd, stdout=pipe, stderr=stdout) stdout, stderr = p.communicate()  print stdout 

capture output , write file: python ifconfig.py > outputfile.txt


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -