Powered by GeSHi

Source code for getip.bat

(view source code of getip.bat as plain text)

  1. @ECHO OFF
  2. ECHO.
  3.  
  4. REM Check command line
  5. IF NOT [%1]==[] GOTO Syntax
  6.  
  7. REM Check OS version
  8. IF "%OS%"=="Windows_NT" GOTO NT
  9. VER | FIND "/2" >NUL
  10. IF NOT ERRORLEVEL 1 GOTO OS2
  11.  
  12. :Syntax
  13. ECHO GetIP.cmd, Version 3.00 for Windows NT 4/2000/XP and OS/2 Warp/eCS
  14. ECHO Display this computer's IP address
  15. ECHO.
  16. ECHO Usage:  GETIP.CMD
  17. ECHO.
  18. ECHO Written by Rob van der Woude
  19. ECHO http://www.robvanderwoude.com
  20. GOTO End
  21.  
  22. REM Note: write access in the current directory is
  23. REM       required to create temporary batch files!
  24. :OS2
  25. REM Create temporary batch file to get IP address
  26. IFCONFIG lan0 | FIND /I " INET " >TEMP.CMD
  27. REM Create secondary batch file to retrieve IP address from first one
  28. > INET.CMD ECHO @ECHO OFF
  29. >>INET.CMD ECHO SET IPADDR=%%1
  30. >>INET.CMD ECHO ECHO IP address: %%IPADDR%%
  31. REM Call temporary batch files to display IP address
  32. REM and store it in environment variable
  33. CALL TEMP.CMD
  34. REM Clean up
  35. DEL  TEMP.CMD
  36. DEL  INET.CMD
  37. GOTO End
  38.  
  39. :NT
  40. :: Note: the following code will extract only the last IP address from the list
  41. FOR /F "TOKENS=2* DELIMS=:" %%A IN ('IPCONFIG /ALL ^| FIND "IP Address"') DO FOR %%B IN (%%A) DO SET IPADDR=%%B
  42. ECHO IP address: %IPADDR%
  43.  
  44. :End
  45.