mercredi 29 avril 2015

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

Rookie looking for help on the code urgently

import subprocess 
cmd = 'ifconfig -a'

p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)

while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
       <<<HOW TO CMD SENT AND IT'S OUTPUT>>>
        file = open('outputFile.txt', 'w+')
        file.write(out)
        file.close()

Aucun commentaire:

Enregistrer un commentaire