help needed w Windows batch script

I have in mind to write the following Windows batch script to poll entire range of 254 IP subnet to see
if the IP has web server listening on it;  help me fix the codes below to get it working:

echo off
do while reading c:\temp\wgetoutput.txt
wget -S <IP_from_IPlist.txt>  2> c:\temp\wgetoutput.txt
if %errorlevel% == 0 then find "Server:" >> c:\temp\websvrtype.txt
echo "not a webserver" >> >> c:\temp\websvrtype.txt

I have the wget tool & it's tested working.
The error output  (ie 2> )  of  'wget -S IP_addr' is as below:

If successfully connected to a web server: (ie errorlevel is 0)
Connecting to connected.
HTTP request sent, awaiting response...
  HTTP/1.0 301 Redirect
  Server: GoAhead-Webs
  Date: Fri Jan 14 18:41:53 2106
  Pragma: no-cache
  Cache-Control: no-cache
  Content-Type: text/html
  Location: index.asp

If unsuccessfully connected to a web server: (errorlevel is usually 1 but can be high value if IP invalid; so all non-zero values)
           => `index.html.1'
Connecting to failed: Connection refused.
Who is Participating?
QlemoConnect With a Mentor DeveloperCommented:
As you seem to get a list of web server types, too, none of the tools suggested will help you IMHO. I've hardcoded the IP subnet below. The script will go thru all 254 addresses. Also, your suggested output does not show the IP address, which makes the resulting file rather useless - I've added that part, too.
@echo off
setlocal EnableDelayedExpansion
set net=192.168.0
del c:\temp\websvrtype.txt 2>nul
for /L %%A in (1,1,254) do (
  set srv=%net%.%%A
  set type=
  for /F "tokens=1* delims=: " %%R in ('wget -S %net%.%%A  2^>^&1 ^| find "Server:"') do set type=%%S
  if "!type!" == "" (
    echo !srv!: not a web server 
  ) else (
    echo !srv!: !type!
) >> c:\temp\websvrtype.txt

Open in new window

sunhuxAuthor Commented:

if %errorlevel% == 0 then find "Server:" >> c:\temp\websvrtype.txt
    should read
if %errorlevel% == 0 then find "Server:" c:\temp\wgetoutput.txt >> c:\temp\websvrtype.txt
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
You might consider one of the programs that already exist like AngyIP scanner.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Dave BaldwinFixer of ProblemsCommented:
Here is another that doesn't require JAVA.
Joe Winograd, EE Fellow 2017, MVE 2016, MVE 2015Connect With a Mentor DeveloperCommented:
Hi sunhux,
I can't help you with that batch file, but along the lines of Dave's posts, this EE article provides a solution that may interest you:
Test IP Addresses with PING

Regards, Joe
John HurstConnect With a Mentor Business Consultant (Owner)Commented:
I also use Advanced IP Scanner. It works well, does what you want and can save the results to a file.
Joe Winograd, EE Fellow 2017, MVE 2016, MVE 2015Connect With a Mentor DeveloperCommented:
One other thing. If you don't want to install AutoHotkey and prefer to download an executable (an EXE file), such as with the two links that Dave provided, then I'll be happy to compile it for you into a stand-alone executable (an EXE file that requires no installation — just run it), as mentioned in my PING article. Regards, Joe
sunhuxAuthor Commented:
excellent, I'll test it out tomorrow
sunhuxAuthor Commented:
I just made one minor change so that it doesn't take very long when wget polls
IP addresses that are not in use or not responding :

('c:\wget\wget -S -t 1 --timeout=2 %net%.%%A  2^>^&1 ^| find "Server:"')

-t 1 : number of tries is 1
--timeout=2 : timeout after 2 secs

& it worked marvelously without triggering IPS/IDS/firewall events in our Arcsight
Good move to lessen the timeout and retries. Also, with this (non)speed it doesn't surprise me no IDS is complaining - it is just too slow to do so. Anyway, usually the target IDS is the one checking for attacks, not your local one.
All Courses

From novice to tech pro — start learning today.