Find an IP address

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
locbtran
Posts: 3
Joined: 06 Mar 2009 22:52

Find an IP address

#1 Post by locbtran » 01 Apr 2010 02:06

What is the command used to display the list of IP address of the computers in LAN or is this even possible?

thanks

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Find an IP address

#2 Post by jeb » 01 Apr 2010 12:13

Hi locbtran,

you can use ipconfig.
But in batch files you can get trouble if you are running XP, because Microsoft does not know how line endings should work.
Ipconfig print LF CR instead of CR LF.

Under Vista it works correct.

jeb

locbtran
Posts: 3
Joined: 06 Mar 2009 22:52

Re: Find an IP address

#3 Post by locbtran » 01 Apr 2010 21:19

I open my cmd prompt and type in, "ipconfig print LF CR"
When I did this it gave me this message: Error: unrecongnized or incomplete command line

Am I doing something wrong

thanks

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Find an IP address

#4 Post by jeb » 02 Apr 2010 00:39

hi locbtran,

try only ipconfig

jeb

MarkL
Posts: 2
Joined: 04 Apr 2010 22:53

Re: Find an IP address

#5 Post by MarkL » 06 Apr 2010 10:42

Here's two methods.
The second method does require a file containing Carriage Returns ("CR") only.

Code: Select all

@echo off
 
:: This part pings each node reported by "net view".
  setlocal enabledelayedexpansion
 
  rem    List net view nodes.
  echo.
  echo   Listing net view nodes ...
  echo.
  for /f %%a in ('net.exe view ^| find "\\"') do (
    set node=%%a
    rem Strip the "\\" from the front.
    set node=!node:~2!
    rem Ping each of the node names; extract the IP address from the "Ping statistics" line.
    for /f "tokens=4-7 delims=.: " %%b in ('ping.exe !node! -n 1 ^| find.exe "Ping statistics"') do (
      rem Make each entry the same length.
      set IP="%%b.%%c.%%d.%%e    "
      set IP=!IP:~1,15!
      echo   !IP! - %%a
    )
  )
  endlocal
 
  echo.
  echo ---------------------------------------------------

:Part2
::--------------------------------------------------------
:: This part pings each address in the subnet of this LAN.

  setlocal enabledelayedexpansion
 
  echo.
  echo   Ping each address in the subnet of this LAN ...
  rem Set timeout for ping command.
  set TimeOut=500

  :: Assign a single CR to a variable; use a textfile with 3 bytes of 0x0d, 0x0d
  for /f "tokens=" %%x in ('type %~DP0cr.txt') do ( echo Hi! & set CR=%%x )

  rem Get our IP Address and Subnet Mask.
  for /F "tokens=1-6 delims=.: " %%a in ('ipconfig') do (
    if "%%a %%b" equ "IP Address"  (
      set IP1=%%c
      set IP2=%%d
      set IP3=%%e
      set IP4=%%f
    )
    if "%%a %%b" equ "Subnet Mask"  (
      set M1=%%c
      set M2=%%d
      set M3=%%e
      set M4=%%f
  ) )

  if "%IP1%" equ "" (
    echo. & echo  No IP Address found for this system. & echo.
    exit /b
  )
 
  for /L %%a in (1,1,4) do (
    rem Set each IP octet's lowest value according to the Mask octet.
    set /a IP%%a="!IP%%a! & !M%%a!"
    rem Set Mask octets to range.
    set /a M%%a=256-!M%%a! -1
  )
 
  if exist %TEMP%\%~n0.tmp  del %TEMP%\%~n0.tmp
  echo.
  rem Process the second octet.  (We don't bother with the first octet.)
  for /L %%a in (0,1,%M2%) do (
    rem Process the third octet.
    for /L %%b in (0,1,%M3%) do (
      rem Process the forth and last octet.
      for /L %%c in (1,1,%M4%) do (
        :: Generate the IP Address for ping.
        set /a IP2x=%IP2% + %%a
        set /a IP3x=%IP3% + %%b
        set /a IP4x=%IP4% + %%c
        rem Don't test x.x.x.255.
        if !IP4x! neq 255 (
          rem Reprint info on the same line.
          <nul set /p "= ++ Pinging %IP1%.!IP2x!.!IP3x!.!IP4x!!CR!"
          rem Look for a "Reply from" line from a successful ping.
          for /f "tokens=3-6 delims=.: " %%d in ('ping.exe %IP1%.!IP2x!.!IP3x!.!IP4x! -n 1 -w %TimeOut$ %IP1%.!IP2x!.!IP3x!.!IP4x! ^| find.exe "Reply from"') do (
            rem If we had a successful ping, save the IP Address for the temp file.
            >>%TEMP%\%~n0.tmp echo   IP=%%d.%%e.%%f.%%g
          )
          set /a I=!I! + 1
  ) ) ) )
  rem Clear the line
  <nul set /p "=                             "
 
  rem If we had any successful ping, the file exits -- print it.
  if exist %TEMP%\%~n0.tmp (
    more %TEMP%\%~n0.tmp
    del  %TEMP%\%~n0.tmp
  )

  endlocal


Post Reply