Link to home
Start Free TrialLog in
Avatar of thandel
thandel

asked on

Develope DOS Batch program to determine IP Address

Is there a way through something like a DOS Batch program or anything else that I could use to ping computers on my network and find out thier IP address and computer name?  I'm looking for something that  I could run a locally on a USB flash drive.

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sure there is.
Windows scripting via vbs would do it a treat.
Here gives an example http://www.myitforum.com/articles/11/view.asp?id=9506
(It throws it into Excel, so you'll need that installed on the machine you run it on. It also adds if it's an SMS client which you can comment out if it's not needed.)

Terry
PLEASE NOTE:
With that script it quite literally has to go through all 255 addresses to see if that address is alive.
It pings EACH address, then if it gets a response it gets the machine name and wether or not it's a SMS client.

Thats a sample of what you can do via scripting, but scripting does take time to complete.
Example being the script took 3-4 minutes to scan 10.1.1.0 through to 10.1.1.255
farhankazi's option took 59 seconds.

I suppose it comes down to what output you need the resuls as and the range you are trying to scan.

Terry
If you are more interested in batch you can use following batch code.

:: ================
:: READ THIS FIRST
:: ================
:: * Copy and Paste following script into notepad and save it with any name having .cmd extension.
:: SCRIPT START
@ECHO OFF
SETLOCAL EnableDelayedExpansion
IF EXIST Computers.txt DEL /F /Q Computers.txt
IF EXIST NetworkRpt.txt DEL /F /Q NetworkRpt.txt
ECHO Scanning Network please wait...
FOR /F "skip=2 delims=\\ " %%c IN ('Net View ^|FIND "\\"') DO ECHO %%c>>Computers.txt
IF NOT EXIST Computers.txt Goto ShowErr
FOR %%R IN (Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST IP2NameRpt.txt DEL /F /Q IP2NameRpt.txt
FOR /F %%c IN ('Type Computers.txt') Do (
    Echo Processing: %%c
      FOR /F "tokens=4 delims=: " %%i IN ('PING -n 1 -w 1000 %%c ^|FIND /I "statistics"') DO (ECHO %%c: %%i >>NetworkRpt.txt)
)      
ECHO Scanning finish check 'NetworkRpt.txt' for output.
Goto EndScript
:ShowErr
Echo "Computers.txt" file does not exist or file is empty!
:EndScript
ENDLOCAL
EXIT /B 0
:: SCRIPT END
Avatar of bpreiss
bpreiss

Here is another approach:

The batch file scans all systems within a particular ip range and returns upon success, meaning that the device (PC, Router, Gateway, Accesspoint etc.) is running and responded to a ping, detailed information including IP Address, MAC, Host, NetBIOS name and the Domain or Workgroup the system is attached to. The output on the screen will look like this:
IP Address          = 192.168.0.114
Mac Address         = 00-1A-92-19-AF-25
Host name           = statistics
NetBIOS name        = I1505
Domain or workgroup = AMERICAS

Also, every system that responded will be logged to a text file like:
IP      MacAddress      Hostname      NetBIOS      Domain
192.168.0.1      00-0d-88-01-b8-fa      statistics      not available      not available
192.168.0.114      00-1A-92-19-AF-25      statistics      I1505      AMERICAS
...

This is done by the following batch file that will be called from a command prompt like

for /l %i in (1,1,254) do findpc 192.168.0.%i

:: Begin of ScanNet.cmd
:: ============================================
@ECHO OFF
:: Save initial environment
SETLOCAL
ECHO.
:: ============================================
::       check command line parameters
:: ============================================
IF [%1]==[] GOTO Syntax
ECHO.%1 | FIND "?" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax

:: ============================================
::       set variables to default values
:: ============================================
SET netbiosname=not available
SET domain=not available
SET macaddr=

:: ============================================
::     checking availability of the ip
:: ============================================
PING %1 | FIND "TTL" >NUL
IF ERRORLEVEL 1 GOTO IsNotUp

:: ============================================
::  finding hostname & mac (only on local lan)
:: ============================================
FOR /F "tokens=2 delims= " %%A IN ('PING -a %1 -n 1 ^| FIND "%1" ^| FIND /V "TTL="') DO SET hostname=%%A
FOR /F "tokens=2 delims= " %%A IN ('ARP -a %1 ^| FIND "%1"') DO SET macaddr=%%A
IF [%macaddr%]==[] FOR /F "TOKENS=4" %%A IN ('NBTSTAT -a %1 ^| FIND "="') DO SET macaddr=%%A
IF [%macaddr%]==[] SET macaddr=not available

:: ============================================
::       checking if netbios infos exist
:: ============================================
NBTSTAT -a %1 | FIND "NetBIOS" >NUL
IF ERRORLEVEL 1 GOTO OtherDomain

:: ============================================
::           finding netbios infos
:: ============================================
FOR /F "tokens=1* delims= " %%A IN ('NBTSTAT -a %1 ^| FIND "<20>"')        DO IF NOT [%%A]==[] SET netbiosname=%%A
FOR /F "tokens=1* delims= " %%A IN ('NBTSTAT -a %1 ^| FIND "<00>  GROUP"') DO IF NOT [%%A]==[] SET domain=%%A
GOTO Display

:OtherDomain
IF [%hostname%]==[%1] GOTO Display
ECHO.%hostname% | FIND "." >NUL
IF ERRORLEVEL 1 GOTO Display
CALL :ParseDomain %hostname:.= %

:: ============================================
::              displaying infos
:: ============================================
:Display
ECHO IP Address          = %1
ECHO Mac Address         = %macaddr%
ECHO Host name           = %hostname%
ECHO NetBIOS name        = %netbiosname%
ECHO Domain or workgroup = %domain%

::==============================================
:: saving infos in a txt file for large scanning
:: ECHOed text is TAB delimited
::==============================================
IF NOT EXIST HostDB.txt (ECHO IP      MacAddress      Hostname      NetBIOS      Domain>HostDB.txt)
(ECHO %1      %macaddr%      %hostname%      %netbiosname%      %domain%) >>HostDB.txt

GOTO End

:ParseDomain
IF [%3]==[] (
      SET domain=%1.%2
      GOTO:EOF
) ELSE (
      SHIFT
      GOTO ParseDomain
)
GOTO:EOF
:: ============================================
:: End of ScanNet.cmd
:: ============================================



::==============================================
:: ERRORS
::==============================================

:IsNotUp
(ECHO The IP %1 seems to be unreachable) 1>&2
GOTO End

:Syntax
ECHO.
ECHO Usage:  FindPC  ^<IP_address^>

:End
:: Restore initial environnement
ENDLOCAL
:: ============================================
:: End of ScanNet.cmd
:: ============================================

Source: http://www.robvanderwoude.com/files/hostname2_nt.txt

As all batch solutions it is relatively slow b/c the entire range of ip's will be scanned, up to 254,
and the Ping response needs time. Aprox 1.5 secs per scaned device.

Best Regards,
Bernhard
Avatar of thandel

ASKER

Thanks, I've tried the following batch files with these results:

farhankazi:
Only found one computer and said the more output could be found in NetworkRpt.txt but that file didn't exist.

bpreiss:
I tried you solution but had an error of: "i was unexpected at this time."

One thing that I forgot to add was that I'm would like to scan across three networks: 192.168.0.X, 192.168.1.X, 192.168.2.X all with a sub mask of 255.255.255.0.

If it helps this is a small office and we have less than 10 computers on each site, so typically the IP stopes at 192.168.X.9.
Hi,

I apologize, the line for /l %i in (1,1,254) do findpc 192.168.0.%i nneds to be corrected.
It must be ScanNet instead of FindPC.

After changing this is should be working, at least on the systems I have here I can test witch it works without any issues.

Thank you
Actually my batch file was searching for computer names that are listed through 'Net View' command.
Did you check "Angry IP scanner" that I mentioned in my first comment?
Check following if this works for you.

:: Copy and Paste following script into notepad and save it with any name having .cmd extension.
:: SCRIPT START
@ECHO OFF
SETLOCAL EnableDelayedExpansion
IF EXIST IPList.txt DEL /F /Q IPList.txt
FOR /L %%I IN (1,1,254) DO @ECHO 192.168.0.%%I >>IPList.txt
FOR /L %%I IN (1,1,254) DO @ECHO 192.168.1.%%I >>IPList.txt
FOR /L %%I IN (1,1,254) DO @ECHO 192.168.2.%%I >>IPList.txt
IF EXIST NetworkRpt.txt DEL /F /Q NetworkRpt.txt
FOR /F %%I IN ('Type IPList.txt') Do (
    ECHO Checking: %%I
    PING -n 1 -w 1000 %%I|Find /I "TTL" >NUL
    IF NOT ErrorLevel 1 (
            FOR /F "Tokens=2" %%C IN ('PING -a -n 1 -w 1000 %%I ^|Find "["') DO ECHO %%I: %%C >>NetworkRpt.txt
    )ELSE (Echo %%I: System offline)
)      
:EndScript
REM IF EXIST IPList.txt DEL /F /Q IPList.txt
ENDLOCAL
EXIT /B 0
:: SCRIPT END
Avatar of thandel

ASKER

bpreiss: this is what I have with the same error:
for /l %i in (1,1,254) do ScanNet 192.168.0.%i

farhankazi:

It started to work but then I terminated it... I really just need to scan 192.168.1.100 - 110 and the same for 192.168.0.X and 192.168.2.X.
Avatar of thandel

ASKER

farhankazi: I got this working thus far.....

FOR /L %%I IN (100,1,110) DO @ECHO 192.168.0.%%I >>IPList.txt
FOR /L %%I IN (100,1,110) DO @ECHO 192.168.1.%%I >>IPList.txt
FOR /L %%I IN (100,1,110) DO @ECHO 192.168.2.%%I >>IPList.txt
Avatar of thandel

ASKER

Just seems that we need to get the computer name and cleanup the output and I think we are OK.