Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for apipa.bat

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

  1. @ECHO OFF
  2. :: Check Windows version and command line
  3. IF NOT [%1]==[] IF NOT [%1]==[0] IF NOT [%1]==[1] GOTO Syntax
  4. IF NOT [%2]==[] IF /I NOT [%2]==[/Y] GOTO Syntax
  5. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  6. VER | FIND "Windows 2000" >NUL
  7. IF NOT ERRORLEVEL 1 GOTO Begin
  8. VER | FIND "Windows XP" >NUL
  9. IF ERRORLEVEL 1 GOTO Syntax
  10.  
  11. :Begin
  12. SETLOCAL ENABLEEXTENSIONS
  13. :: Save command line parameter
  14. SET Enable=%1
  15. SET Force=%2
  16.  
  17. :: Make sure the impact of this batch file is understood
  18. IF DEFINED Enable IF NOT DEFINED Force CALL :Disclaimer
  19. IF [%Exit%]==[1] GOTO:EOF
  20.  
  21. :: Initialize variables
  22. SET Agreed=NO
  23. SET Count=0
  24. SET Exit=0
  25. SET Restore=0
  26. SET RollBackDir=%Temp%.
  27. TYPE NUL > _ApipaRollBack.txt 2>NUL
  28. IF NOT ERRORLEVEL 1 (
  29. 	DEL _ApipaRollBack.txt
  30. 	SET RollBackDir=.
  31. )
  32. SET Title=The following interface^^(s^^) with IP Autoconfiguration settings was/were found:
  33.  
  34. :: Enumerate interfaces
  35. REGEDIT /E %Temp%.\apipaorg.reg "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces"
  36. FOR /F "tokens=8 delims=\" %%A IN ('TYPE %Temp%.\apipaorg.reg ^| FIND "[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"') DO CALL :Enum "%%A"
  37.  
  38. :: Search for APIPA in each interface
  39. FOR /L %%A IN (1,1,%Count%) DO CALL :FindApipa "%%Interface.%%A%%"
  40.  
  41. :: Done
  42. ENDLOCAL
  43. GOTO:EOF
  44.  
  45.  
  46. :: End of main batch procedure
  47.  
  48.  
  49. :: Subroutines
  50.  
  51.  
  52. :Enum
  53. :: Store interface "name" in array
  54. SET /A Count += 1
  55. SET Interface=%~1
  56. SET Interface.%Count%=%Interface:]=%
  57. GOTO:EOF
  58.  
  59.  
  60. :FindApipa
  61. IF [%Exit%]==[1] GOTO:EOF
  62. :: Check if Autoconfiguration is specified for this interface
  63. REGEDIT /E %Temp%.\_apipa.reg "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%~1"
  64. TYPE %Temp%.\_apipa.reg | FIND /I "IPAutoconfiguration" >NUL
  65. :: If not, then leave
  66. IF ERRORLEVEL 1 GOTO:EOF
  67. :: Display some IP settings for this interface,
  68. :: to make sure we are going to edit the right one.
  69. :: Display and reset title
  70. ECHO.
  71. IF DEFINED Title (
  72. 	ECHO.%Title%
  73. 	SET Title=
  74. 	ECHO.
  75. )
  76. :: Display "header"
  77. TYPE %Temp%.\_apipa.reg | FIND /I "[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%~1"
  78. :: Convert IP addresses from hexadecimal to decimal
  79. FOR /F "tokens=2 delims=:" %%A IN ('TYPE %Temp%.\_apipa.reg ^| FIND /I "IPAddress" ^| FIND "hex("') DO CALL :X2D "%%A"
  80. :: Display converted IP addresses
  81. FOR /F "tokens=1,2* delims==" %%A IN ('TYPE %Temp%.\_apipa.reg ^| FIND /I "IPAddress" ^| FIND "hex("') DO ECHO %%~A=%IPAddress%
  82. :: Display IP addresses that were already stored in decimal
  83. FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\_apipa.reg ^| FIND /I "IPAddress" ^| FIND /I /V "hex("') DO ECHO %%~A=%%~B
  84. :: Display current Autoconfiguration settings
  85. FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\_apipa.reg ^| FIND /I "IPAutoconfiguration"') DO ECHO %%~A=%%~B
  86. ECHO.
  87. IF DEFINED Enable CALL :Confirm "%~1"
  88. GOTO:EOF
  89.  
  90.  
  91. :Confirm
  92. IF DEFINED Force GOTO SkipConf
  93. SET Message=Are you sure you want to
  94. IF %Enable%==0 (
  95. 	SET Message=%Message% disable
  96. ) ELSE (
  97. 	SET Message=%Message% enable
  98. )
  99. (SET Message=%Message% IP Autoconfiguration? [Yes/No] )
  100. SET /P Continue=%Message%
  101. SET Continue=%Continue:~0,1%
  102. IF /I NOT [%Continue%]==[Y] GOTO:EOF
  103. :SkipConf
  104. CALL :Settings "%~1"
  105. GOTO:EOF
  106.  
  107.  
  108. :Settings
  109. :: Create temporary .REG file to change the settings
  110. > %Temp%.\Apipa%Count%.reg ECHO REGEDIT4
  111. >>%Temp%.\Apipa%Count%.reg ECHO.
  112. >>%Temp%.\Apipa%Count%.reg ECHO [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%~1]
  113. >>%Temp%.\Apipa%Count%.reg ECHO "IPAutoconfigurationEnabled"=dword:0000000%Enable%
  114. >>%Temp%.\Apipa%Count%.reg ECHO.
  115. START /WAIT REGEDIT /S %Temp%.\Apipa%Count%.reg
  116. DEL %Temp%.\Apipa%Count%.reg
  117. REGEDIT /E %Temp%.\Apipa%Count%.reg "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%~1"
  118. ECHO Checking new setting:
  119. TYPE %Temp%.\Apipa%Count%.reg | FIND "IPAutoconfigurationEnabled"
  120. IF ERRORLEVEL 1 (
  121. 	ECHO Error modifying registry setting.
  122. 	ECHO Start REGEDIT and check the following key:
  123. 	ECHO HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%~1
  124. 	ECHO IPAutoconfigurationEnabled
  125. 	ECHO.
  126. 	ECHO At startup of this batch file your original settings were backed up in:
  127. 	ECHO.
  128. 	ECHO %Temp%.\apipaorg.reg
  129. 	ECHO.
  130. 	ECHO This file can be used to restore your original settings, either manually by
  131. 	ECHO doubleclicking it, or automatically by this batch file.
  132. 	SET /P Restore=Would you like to automatically restore your original settings now? [Yes/No] 
  133. 	SET Restore=%Restore:~0,1%
  134. 	IF /I [%Restore%]==[/Y] REGEDIT /S %Temp%.\apipaorg.reg
  135. 	ECHO.
  136. 	SET /P Restore=Would you like to abort this batch file now? [Yes/No] 
  137. 	SET Restore=%Restore:~0,1%
  138. 	IF /I [%Restore%]==[/Y] SET Exit=1
  139. )
  140. IF [%Exit%]==[1] GOTO:EOF
  141. :: Create rollback file
  142. > "%RollBackDir%\ApipaRollback.reg" ECHO REGEDIT4
  143. >>"%RollBackDir%\ApipaRollback.reg" ECHO.
  144. >>"%RollBackDir%\ApipaRollback.reg" ECHO [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\%~1]
  145. >>"%RollBackDir%\ApipaRollback.reg" ECHO "IPAutoconfigurationEnabled"=-
  146. >>"%RollBackDir%\ApipaRollback.reg" ECHO.
  147. ECHO.
  148. ECHO A rollback file has been created:
  149. ECHO "%RollBackDir%\ApipaRollback.reg"
  150. ECHO.
  151. ECHO Either doubleclick this file to restore Windows' default APIPA settings,
  152. ECHO or run the following command from a command prompt:
  153. ECHO.
  154. ECHO REGEDIT /S "%RollBackDir%\ApipaRollback.reg"
  155. ECHO.
  156. IF NOT DEFINED Force PAUSE
  157. GOTO:EOF
  158.  
  159.  
  160. :X2D
  161. :: Convert IP address from hexadecimal to decimal
  162. SET IPAddress=%~1
  163. SET IPAddress=%IPAddress:,00=,%
  164. SET IPAddress=%IPAddress:2e,=.,%
  165. SET IPAddress=%IPAddress:30,=0,%
  166. SET IPAddress=%IPAddress:31,=1,%
  167. SET IPAddress=%IPAddress:32,=2,%
  168. SET IPAddress=%IPAddress:33,=3,%
  169. SET IPAddress=%IPAddress:34,=4,%
  170. SET IPAddress=%IPAddress:35,=5,%
  171. SET IPAddress=%IPAddress:36,=6,%
  172. SET IPAddress=%IPAddress:37,=7,%
  173. SET IPAddress=%IPAddress:38,=8,%
  174. SET IPAddress=%IPAddress:39,=9,%
  175. SET IPAddress=%IPAddress:,=%
  176. GOTO:EOF
  177.  
  178.  
  179. :Disclaimer
  180. CLS
  181. ECHO.
  182. ECHO APIPA.BAT,  Version 1.00 for Windows 2000 / XP
  183. ECHO Display or modify APIPA settings (Automatic Private IP Addressing)
  184. ECHO.
  185. ECHO * * What is APIPA? * *
  186. ECHO APIPA or Automatic Private IP Addressing is the mechanism in Windows 2000
  187. ECHO and XP DHCP clients that automatically configures these clients to use an
  188. ECHO IP address from the private range 169.254.*.* whenever a DHCP server is
  189. ECHO not available.
  190. ECHO More details are available at the following URLs:
  191. ECHO http://webopedia.internet.com/TERM/A/APIPA.html
  192. ECHO http://www.win2000mag.com/Articles/Index.cfm?ArticleID=7464
  193. ECHO.
  194. ECHO * * Why would I want to disable it? * *
  195. ECHO Though APIPA may be great for stand-alone computers or small networks, it
  196. ECHO will cause problems both on large networks and on small networks using a
  197. ECHO shared cable or DSL connection.
  198. ECHO Disabling APIPA will result in DHCP error messages whenever a DHCP server
  199. ECHO is not available. Using APIPA may result in network error messages
  200. ECHO whenever you try to connect to another computer on your network or on the
  201. ECHO internet.
  202. ECHO It's up to you to decide which option you prefer.
  203. ECHO.
  204. PAUSE
  205. CLS
  206. ECHO.
  207. ECHO APIPA.BAT,  Version 1.00 for Windows 2000 / XP
  208. ECHO Display or modify APIPA settings (Automatic Private IP Addressing)
  209. ECHO.
  210. ECHO * * Is it safe to disable APIPA? * *
  211. ECHO Disabling APIPA does not seem to impose any risk. After all, we have done
  212. ECHO without it for over two decades. However, disabling APIPA means modifying
  213. ECHO the registry. Though hundreds of modifications are made every day on every
  214. ECHO computer running Windows, it is NEVER without risk! Modifying the registry
  215. ECHO incorrectly may make your computer inaccessible, forcing you to reinstall
  216. ECHO Windows from scratch, thereby possibly losing data and settings.
  217. ECHO.
  218. ECHO * * DISCLAIMER * *
  219. ECHO I have done my utmost best to prevent damage and to enable simple
  220. ECHO rollbacks. However, I cannot guarantee that this batch file will function
  221. ECHO correctly on every computer. I cannot take any responsibility for damage
  222. ECHO caused by the use of, or inability to use this batch file or its rollback
  223. ECHO script. Use this batch file entirely at your own risk.
  224. ECHO.
  225. ECHO Type YES and press Enter if you have read and understood these terms,
  226. ECHO and agree to them:
  227. SET /P Agreed=
  228. IF /I NOT [%Agreed%]==[YES] (
  229. 	ECHO Canceled on user's request.
  230. 	ENDLOCAL
  231. 	SET Exit=1
  232. )
  233. CLS
  234. GOTO:EOF
  235.  
  236.  
  237. :Syntax
  238. IF "%OS%"=="Windows_NT" CALL :Disclaimer
  239. ECHO.
  240. ECHO APIPA.BAT,  Version 1.00 for Windows 2000 / XP
  241. IF     "%OS%"=="Windows_NT" ECHO Display or modify APIPA settings ^(Automatic Private IP Addressing^)
  242. IF NOT "%OS%"=="Windows_NT" ECHO Display or modify APIPA settings (Automatic Private IP Addressing)
  243. ECHO.
  244. IF     "%OS%"=="Windows_NT" ECHO Usage:  %~n0  [ 0 ^| 1  [ /Y ] ]
  245. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  %~n0  [ 0 or 1  [ /Y ] ]
  246. ECHO.
  247. ECHO     No parameters     Display current settings for each interface
  248. ECHO     0                 Disable APIPA for current interface
  249. ECHO     1                 Enable APIPA for current interface
  250. IF "%OS%"=="Windows_NT" ECHO     /Y                Do not ask for confirmation ^(valid with 0 or 1 only^)
  251. ECHO.
  252. ECHO Written by Rob van der Woude
  253. ECHO http://www.robvanderwoude.com
  254. ECHO.
  255.  
  256.  
  257. :End
  258.  

page last modified: 2024-04-16; loaded in 0.0257 seconds