LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep has no effect - does not grep anything in this for loopa (https://www.linuxquestions.org/questions/linux-newbie-8/grep-has-no-effect-does-not-grep-anything-in-this-for-loopa-916652/)

LinuxChiq 12-01-2011 07:10 PM

grep has no effect - does not grep anything in this for loopa
 
#!/bin/bash

for ip in $(seq 200 255); do
nc -vvz 192.168.15.$ip 25 |grep "open" &
done



Thats the code I have, I basically just want to find all the smtp servers in the range, I'm successfully doing it but want to see only the lines that have the word 'open' in it.

When I run the above script, it just shoots out everything as if the grep wasnt there. Not sure what is going on, I'm sure grep probably isn't broken..

Tinkster 12-01-2011 07:36 PM

That's because your nc output is going to STDERR ... try
Code:

#!/bin/bash

for ip in $(seq 200 255); do
nc -vvz 192.168.15.$ip 25 2>&1|grep "open" &
done


David the H. 12-01-2011 09:03 PM

seq is unnecessary, as bash has brace expansion.

Code:

for ip in {200..255}; do
  ...



All times are GMT -5. The time now is 03:13 PM.