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
Post a Comment