Scripting: Determine Workstation's IP AddressThe following script determines the workstation's IP address. @echo off ping %COMPUTERNAME% -4 -n 1 | find /i "ping statistics" > %TEMP%\results.txt for /f "tokens=1-7,* delims=:. " %%a in ('type %TEMP%\results.txt') do set IP1=%%d& set IP2=%%e& set IP3=%%f& set IP4=%%g del %TEMP%\results.txt echo Your IP is %IP1%.%IP2%.%IP3%.%IP4% How It WorksOutput from the PING command is piped into FIND to return only the results line, which is output to a text file. The contents of the text file is then fed into FOR which extracts the 4th (%%d), 5th (%%e), 6th (%%f) and 7th (%%g) parameters. These are then stored in environment variables so they are available to any further commands. Advantages- Uses native Windows commands.
- Works on Windows 2000 and above (not tested Windows NT).
- Returns the most likely usable IP address the workstation is accessible on.
- Does not require local administrator or elevated privileges.
Disadvantages- Relies on current network allowing the machine to ping its own name.
- May return wifi address in preference to ethernet address.
|