I'm trying to Read/Write process file with command grep
and cut
to get part of the IP address using pipe operator, here is the process simply written.
import subprocess as sub
import shlex
def discover_Host():
with open("Get_IP.txt", "w+") as q_process:
cmd1 = "grep Host xmap_ip.gnmap"
cmd2 = "cut -d ' ' -f 2"
arg1 = shlex.split(cmd1)
arg2 = shlex.split(cmd2)
proc1 = sub.Popen(args1, stdout = sub.PIPE)
proc2 = sub.Popen(args2, stdin = proc1.stdout, stdout = q_process)
proc1.stdout.close()
out, err = proc2.communicate()
return
Now the file Get_IP.txt
contains data like this Host: 172:22:179:156 (abc.efg.net) Status: Up
so I'm trying to get only IP address from it as I run the grep and cut command with pipe directly on the terminal it works but as I try it with SubProcess it doesn't write on the file.