0

I need to scan several systems in the network within IP range 172.18.x.x to 172.25.x.x and check whether each of them is active or not, and then dump the result in a file. Following is the script:

@echo off
for /L %%x in (18,1,25) do (
    for /L %%y in (1,1,254) do (
        for /L %%z in (1,1,254) do (

    ping -n 1 172.%%x.%%y.%%z | find "Reply" > NUL
    IF NOT ERRORLEVEL 1 (set state=UP) ELSE (set state=DOWN)

    echo 172.%%x.%%y.%%z is %state% >> state.txt
)))

But, the result concludes that every system is 'UP' even though some IP addresses are unused.

Any problem with the script?

  • Why use a script ? Why not use one of the many solutions that are out there already ? – Lawrence Mar 25 '14 at 5:58
  • The root cause behind this is the following- networkengineering.stackexchange.com/questions/6993/… – Mandar Shinde Mar 25 '14 at 6:06
  • i suppose it would be a problem with the script.. if it is saying an IP is up when it is not. You write "But, the result concludes that every system is 'UP' even though some IP addresses are unused." can you state an IP it says is up that doesn't exist? – barlop Mar 25 '14 at 6:43
  • IP addresses in the range 172.22.x.x to 172.24.x.x are existing, rest are not. But the result displays "172.18.1.1 is UP", "172.18.1.2 is UP" and so on. – Mandar Shinde Mar 25 '14 at 6:48
  • Since you are a linux man, You may love a program called Cygwin for Window cygwin.com s. It has a load of linux commands for windows. Also gnuwin32 gnuwin32.sourceforge.net/packages.html download coreutils and others. – barlop Mar 25 '14 at 10:40
0

the problem in your script was a very weird thing about batch scripts. To get environment variables to update within an IF or FOR you have to use setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION and !var!. If you are curious, the absurdity (what happens if you don't include that setlocal line with the !var!) is explained further in set /?.

setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
for /L %%x in (18,1,25) do (
    for /L %%y in (1,1,254) do (
        for /L %%z in (1,1,254) do (

    ping -n 1 172.%%x.%%y.%%z | find "Reply" > NUL
    IF NOT ERRORLEVEL 1 (set state=UP) ELSE (set state=DOWN)

    echo 172.%%x.%%y.%%z is !state! >> state.txt
)))

And if you only want the ones that are up. Try this

setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
for /L %%x in (18,1,25) do (
    for /L %%y in (1,1,254) do (
        for /L %%z in (1,1,254) do (

    ping -n 1 -w 1000 172.%%x.%%y.%%z | find "Reply" > NUL
    IF NOT ERRORLEVEL 1 (
        set state=UP
        echo 172.%%x.%%y.%%z is !state! >> state.txt
    ) ELSE (
        set state=DOWN
    )
)))
  • Shall I replace "Reply" with "TTL"? It is creating problem when the result is-"Reply from x.x.x.x: Destination Host is Unreachable" – Mandar Shinde Mar 25 '14 at 6:54
  • @MandarShinde yeah makes sense – barlop Mar 25 '14 at 7:00
  • @MandarShinde you could do -w 1000 too so it doesn't try for more than a second – barlop Mar 25 '14 at 7:02
  • Now, issue with "TTL" as following error occurs - Reply from x.x.x.x: TTL expired in transit. – Mandar Shinde Mar 25 '14 at 7:57
  • @MandarShinde do | find "TTL=" – barlop Mar 25 '14 at 9:56
1

I suggest spiceworks(I am suggesting this after viewing your question https://networkengineering.stackexchange.com/questions/6993/tool-or-script-for-collecting-network-inventory ). All the details you needed is available with spiceworks inventry tool.It is best fit for Windows AD, but it can be done in the either way also.

  • I tried that too (but a trial version) and believe me, it took more than half an hour for scanning 8 nodes providing their IP addresses and Host names only. – Mandar Shinde Mar 25 '14 at 7:47
0

I don't know how many systems you have to check.. Found a solution right here.. If you find it easy to do like this, accept this answer...

For /f %%i in (testservers.txt) do ping -n 1 %%i >>state.txt

**include all ip's in testservers.txt

  • There are about 15,000-20,000 systems. So, including IP address of each and every system in a file manually will not be feasible, I guess. – Mandar Shinde Mar 25 '14 at 6:17
  • Why do you need a batch script?? Can't you use any third party application for this ?? – Vysakh Mar 25 '14 at 6:27
  • I need to collect network inventory details of all the systems deployed across our infrastructure. I am planning to do this using scripts. With this portion of script, I can get IP addresses of all active machines in the network. – Mandar Shinde Mar 25 '14 at 6:33
0

I would use a tool like Angry IP Scanner to do this job. Scripting it seems pointless when there are so many other tools out there that can do the exact same thing.

  • Thanks Lawrence. Actually, I need to collect network inventory details of all the systems deployed across our infrastructure. I am planning to do this using scripts. With this portion of script, I can get IP addresses of all active machines in the network. – Mandar Shinde Mar 25 '14 at 6:36
  • What details do you need from your systems ? What OS are they running ? – Lawrence Mar 25 '14 at 6:39
  • IP Address, MAC Address, Host Name, Make Model, User Details, Processor & OS. 95% of the systems are running Windows (XP, Vista or Win 7) and others are running Linux (RHEL or SLES). We can ignore Linux systems, as of now. – Mandar Shinde Mar 25 '14 at 6:44
  • I'd do it in Powershell rather than Batch script then. – Lawrence Mar 25 '14 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.