The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140417-Windows-8.1-Update-says-it-failed-but-it-succeeded

    20140417-Windows-8.1-Update-says-it-failed-but-it-succeeded--Windows-Help-and-Support--cannot-find-any-info--how-surprising-it-never-does

    20140329-VMware-vSphere-Client-4.1-retry-with-compatibility-settings

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,250 other followers

Batch file to get TCP/IP info from local LAN

Posted by Jeroen Pluimers on 2011/08/24

I needed an automated way of inspecting the local LAN.

The batch files below will on IPv4 networks, with thanks to articles from Windows IT Pro and Rob van der Woude for some ideas:

  • find the TCP/IP gateways/netmasks
  • enumerate all the IP addresses on each subnet (assuming the netmask is 255.255.255.0)
  • ping each IP address and get ARP info, and dump that to the console

There are other tools that can do this too (like Angry IP Scanner), and more but the ones I tried could not copy the output to the clipboard.

find local subnets:

@echo off
setlocal
for /F "tokens=2,12,14 delims=: " %%i in ('ipconfig') do (
::  echo %%i
  if !%%i!==!Mask! call :mask %%k
  if !%%i!==!Gateway! call :gateway %%j
)
endlocal
goto :exit

:mask
  if !%*! == !! goto :exit
  set netmask=%*
  goto :exit

:gateway
  if !%*! == !! goto :exit
  echo netmask=%netmask% gateway=%*
  goto :exit

:exit

enumerate IPs:

@echo off
:: http://www.robvanderwoude.com/ntfortokens.php
for /F "tokens=1,2,3,4,5,6,7,8,9,10,* delims==. " %%a in ('find-local-subnets.bat') do (
:: simplify: only take the first three bytes from the netmask
  call :subnet %%g.%%h.%%i
)
goto :exit

:subnet
  for /l %%n in (1,1,254) do call :ip %1 %%n
goto :exit

:ip
  echo %1.%2
  for /F "tokens=1,2,3,* delims= " %%m in ('arpping.bat %1 %2 ^| find "dynamic"') do (
    echo %%m %%n %%o
  )
goto :exit

:exit

arpping:

:: http://www.windowsitpro.com/article/monitoring-and-analysis/how-can-i-get-a-list-of-mac-to-ip-addresses-on-the-network-
REM arpping.bat
ping -n 1 -l 1 %1.%2
arp -a %1.%2

–jeroen

4 Responses to “Batch file to get TCP/IP info from local LAN”

  1. John Wagner said

    This subnet mask and gateway query script did not run quite right for me. I changed the goto:exit to goto:eof in the subroutines so both of them would run and not exit after the first success. Without this change only the subnet mask was returned.


    @echo off

    setlocal
    for /F “tokens=2,12,14 delims=: ” %%i in (‘ipconfig’) do (
    :: echo %%i
    if !%%i!==!Mask! call :mask %%k
    if !%%i!==!Gateway! call :gateway %%j
    )
    endlocal
    pause
    goto :exit
    :mask
    if !%*! == !! goto :exit
    set netmask=%*
    goto :EOF

    :gateway
    if !%*! == !! goto :exit
    echo netmask=%netmask% gateway=%*
    goto :EOF

    :exit
    pause

    It was useful to turn echo on in debugging to see what was going on behind the scenes. In trouble shooting/design I can see how enumerating all the tokens would be helpful.

    JW

  2. John Wagner said

    If you loop this thing, you need to enable delayed Expansion. Otherwise your for next loops, and if statements can get messed up. I wonder why this is even an option! (This is an issue for bat files, NOT power shell and vbs.) I also dumped this to a file and I added a date time stamp to the file. Please note that Delayed Expansion behaves differently depending on the OS. This works in Windows 7. There is a programmer somewhere that needs slapped for sheer awkardness!

    @echo off
    setlocal ENABLEDELAYEDEXPANSION
    :main
    call set txtruntime=%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%.%time:~3,2%
    echo Run time = %txtruntime%
    set txtgateway = ”
    call :getsubnet
    echo Gateway = %txtgateway%
    echo “Log file in .\logs\GW.%txtgateway%.time.%txtruntime%.txt”
    call :runpings
    SET /P ANSWER=Do you want to run again(Y/N)?
    echo You chose: %ANSWER%
    if /i {%ANSWER%}=={y} (goto :main)
    if /i {%ANSWER%}=={yes} (goto :main)
    pause

    call :exit

    rem ***Sub routines follow***

    :runpings

    Echo *********** > “.\logs\GW.%txtgateway%.time.%txtruntime%.txt”

    Echo Ping google.com >> “.\logs\GW.%txtgateway%.time.%txtruntime%.txt”
    ping google.com -n 1 >> “.\logs\GW.%txtgateway%.time.%txtruntime%.txt”

    Echo *********** >> “.\logs\GW.%txtgateway%.time.%txtruntime%.txt”

    goto :EOF

    rem ***

    :getsubnet

    for /F “tokens=2,12,14 delims=: ” %%i in (‘ipconfig’) do (
    if !%%i!==!Gateway! call :gateway %%j)
    rem echo %txtgateway%
    goto :EOF

    rem ***

    :gateway
    if !%*! == !! goto :EOF
    rem echo Gateway = gateway=%*
    set txtgateway=%*
    rem echo %txtgateway%
    goto :EOF

    rem ***

    :exit
    exit

    JW

    • jpluimers said

      What can get messed up? (I never saw things getting messed up, but I want to be prepared for things that could go wrong in other batch files too).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 1,250 other followers

%d bloggers like this: