Thank you both for your answers.
@kaz2100: hehe, yup, thats the code fragment that works. Its the other one that doesnt
The main reason I am doing this is so I can pipe the results of the bash commands into a python script for parsing.
e.g to test my network
Code:
nmap -sS -p $PORTS $IPRANGE | grep -B 2 " open " | sed -n 's/\(Interesting ports on \)\(\([0-9]*\.\)\{3\}[0-9]\{1,3\}\):/\2/p' | ./somePythonScript.py
All the above should do is feed IP addresses to the python script as it scans. (Has to be done concurrently)
The python script would look somewhat similar to the following
Code:
#!/usr/bin/env python
import os, sys
ipaddress = sys.stdin.readline()
while ipaddress and ipaddress != '':
print '[+] ip ' + ipaddress
# Do some stuff with the IP address here
ipaddress = sys.stdin.readline()
But the python script doesn't appear to recieve the result from the sed command until the nmap scan is finished. Is this a result of the same buffering you mentioned earlier? I would have thought piping the output would avoid this kind of buffering. If not, is there any way to avoid the buffering or to implement what I'm trying to do?
edit: ok, curiously enough, there again seems to be some sort of buffering on the pipe. If I leave the above command run long enough e.g ensure nmap generates a lot of data it eventually works. I guess the buffering is something I'm going to have to live with.