Results 1 to 9 of 9
#!/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 ...
- 12-01-2011 #1Just Joined!
- Join Date
- Aug 2011
- Posts
- 2
grep has no effect - does not grep anything in this for loop
#!/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..
- 12-02-2011 #2Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
Maybe the output of "nc" is going to stderr. Try:
The "2>&1" says redirect stderr and put it into stdout.Code:#!/bin/bash for ip in $(seq 200 255); do nc -vvz 192.168.15.$ip 25 2>&1 | grep "open" & done
- 12-02-2011 #3Just Joined!
- Join Date
- Sep 2008
- Posts
- 20
1) Maybe you'd better search for "succeeded" instead of "open", since nc -vvz for a determined port returns that status.
2) I should avoid to put "&" at the and of grep, since it is a pipelined command, so in this way it sends grep commands in background, while running next ncs and running other greps in bg .. Mmh, smells something like to be an unpredictable behaviour or so. I should run all the commands inside the loop, in standard sequential way.
Regards
- 12-02-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,855
alf55 should have solved you problem and I agree with gabolander on point 2, but I would do this with nmap (b/c nc can take longer, can hang and is not as robust):
Code:#!/bin/bash port=25 prot=tcp for oct in $(seq 200 255); do ip=192.168.1.${oct} printf "IP address $ip is " out=$(nmap -n -p $port $ip) echo $out|grep -q Host\ seems\ down && echo down && continue printf "up and port $port is " state=$(printf "$out"|awk "/^${port}\/${prot}/{print \$2}") echo $state done
- 12-02-2011 #5Just Joined!
- Join Date
- Sep 2008
- Posts
- 20
- 12-03-2011 #6Banned
- Join Date
- Nov 2011
- Location
- India
- Posts
- 29
- 12-03-2011 #7Linux Guru
- Join Date
- May 2011
- Posts
- 1,855
That netstat command would just show what is listening on the local machine.
Originally Posted by manojsamtani
- 12-05-2011 #8Just Joined!
- Join Date
- Aug 2011
- Posts
- 2
atreyu, I took out the nmap portion of your script and ran it just by itself to verify that it functions but it returned that every port had 25 open.
root~#: nmap -n -p 25 192.168.15.200-250
conversely, after adding 2>&1 to the script, it ran fine. detecting the following open smtp ports on the machines...
(UNKNOWN) [192.168.15.215] 25 (smtp) open
(UNKNOWN) [192.168.15.222] 25 (smtp) open
(UNKNOWN) [192.168.15.229] 25 (smtp) open
(UNKNOWN) [192.168.15.217] 25 (smtp) open
(UNKNOWN) [192.168.15.227] 25 (smtp) open
For the nmap command I tried other ways of scanning including
nmap -sS -p 25 182.168.15.200-250
nmap -sT -p 25 182.168.15.200-250
nmap -sV -p 25 182.168.15.200-250
nmap -sA -p 25 182.168.15.200-250
nmap -sU -p 25 182.168.15.200-250
etc
and they all came up with the same result that every port was open.. ?
- 12-06-2011 #9Linux Guru
- Join Date
- May 2011
- Posts
- 1,855
I'm not sure what you mean by 'every port open' - do you mean port 25 is open on every machine, and you know for a fact that it is not running on every machine? For example, I scanned a portion of my LAN, which has one SMTP server running:
There is no machine @192.168.1.5, btw. This nc command corroborates the nmap results:Code:# nmap -n -p 25 192.168.1.1-6|egrep ^'25|Nmap' Starting Nmap 5.50 ( http://nmap.org ) at 2011-12-06 08:50 EST Nmap scan report for 192.168.1.1 25/tcp closed smtp Nmap scan report for 192.168.1.2 25/tcp closed smtp Nmap scan report for 192.168.1.3 25/tcp closed smtp Nmap scan report for 192.168.1.4 25/tcp open smtp Nmap scan report for 192.168.1.6 25/tcp closed smtp Nmap done: 6 IP addresses (5 hosts up) scanned in 0.43 seconds #
Code:# for i in {1..6}; do nc -vzw 5 192.168.1.${i} 25;done nc: connect to 192.168.1.1 port 25 (tcp) failed: Connection refused nc: connect to 192.168.1.2 port 25 (tcp) failed: Connection refused nc: connect to 192.168.1.3 port 25 (tcp) failed: Connection refused Connection to 192.168.1.4 25 port [tcp/smtp] succeeded! nc: connect to 192.168.1.5 port 25 (tcp) failed: No route to host nc: connect to 192.168.1.6 port 25 (tcp) failed: Connection refused #


Reply With Quote
