Guest User

Untitled

a guest
Jun 25th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. :: Purpose: Allows you to quickly change your IP address without using the GUI
  2. :: Requirements: Windows XP, Vista or 7
  3. :: Notes:
  4. :: Version: 3.5 Major re-write:
  5. :: -- Updated command-line argument ability. Can now specify DHCP or static after adapter name
  6. :: -- Added help section that will spit out when run from the command line
  7. :: -- Code cleanup and restructure
  8. :: 3.1 Removed warning, fixed error when deleting temp file
  9. :: 3.0 Major re-write:
  10. :: -- Added support for command-line invocation, with [optional] name of adapter to be changed
  11. :: -- Combined both scripts into one, and added warning to the beginning
  12. :: -- Script can now be invoked with adapter name to be changed. If not it will prompt for it.
  13. :: -- Re-wrote entire menu structure and added new options (DHCP, Quit)
  14. :: -- Now using %TEMP% variable for storing the working text files (for menus) instead of C:\
  15. :: -- Script now offers to use the OpenDNS servers as backup to the default gateway (for DNS)
  16. :: 2.0 Changed hard-coded adapter name to a script-wide variable and cleaned up menus
  17. :: 1.5 Created basic (ugly) menu structure
  18. :: 1.0 Initial write
  19.  
  20.  
  21. :: Prep
  22. @echo off
  23. set VERSION=3.5
  24. SETLOCAL
  25.  
  26. ::::::::::::::::::::::::::
  27. :: Initialize variables ::
  28. ::::::::::::::::::::::::::
  29. :: \/ The first argument passed to the script becomes the adapter to be changed. \/
  30. set ADAPTER=%1
  31. set IP=
  32. set GATEWAY=
  33. set MASK=
  34. set DNS1=
  35. set DNS2=208.67.222.220
  36. set DNS3=208.67.220.222
  37.  
  38. :::::::::::::::::::::
  39. :: Check arguments ::
  40. :::::::::::::::::::::
  41.  
  42. :: Catch the most commong "help" arguments
  43. if '%1%'=='h' goto help
  44. if '%1%'=='-h' goto help
  45. if '%1%'=='/h' goto help
  46. if '%1%'=='?' goto help
  47. if '%1%'=='-?' goto help
  48. if '%1%'=='/?' goto help
  49.  
  50.  
  51. :: Normal Argument catching
  52. :: 1. Did the user pass an argument upon invocation? If not, go to the menu
  53. :: 2. Did the user say to use DHCP? If so, skip directly to DHCP
  54. :: 3. Did the user say to use Static? If so, skip directly to asking for the values
  55. if '%1%'=='' goto specify_adapter
  56. if '%2%'=='dhcp' goto dhcp
  57. if '%2%'=='static' goto enter_values
  58. goto start
  59.  
  60.  
  61. ::::::::::
  62. :: Help ::
  63. ::::::::::
  64. :help
  65. echo.
  66. echo setip v%VERSION% -- Set your IP address from the command line.
  67. echo.
  68. echo USAGE:
  69. echo %0% [adapter name] [dhcp | static]
  70. echo where:
  71. echo adapter name -- Optional: specify which adapter to modify
  72. echo dhcp -- Optional: specify to attempt to get an IP address via DHCP
  73. echo static -- Optional: specify you want to set a static IP address
  74. echo.
  75. echo If you invoke the script with no arguments it will go to the interactive menu.
  76. echo.
  77. goto end
  78.  
  79.  
  80. :::::::::::::::::::::
  81. :: Specify Adapter :: - Only called if the user didn't pass an adapter name as the first argument
  82. :::::::::::::::::::::
  83. :specify_adapter
  84. color 07
  85. cls
  86. title TCP/IP Configuration for LAN
  87. ipconfig /all > "%TEMP%\tempLANconfig.txt"
  88. findstr /R "IPv4 IPv6 Subnet DHCP Dhcp IP.A Default Ethernet DNS.Ser" <"%TEMP%\tempLANconfig.txt" >"%TEMP%\tempLANconfig2.txt"
  89. echo.
  90. echo ------------------------------
  91. echo Current TCP/IP Configuration
  92. echo ------------------------------
  93. echo.
  94. type "%TEMP%\tempLANconfig2.txt"
  95. echo.
  96. echo ************************************************************
  97. echo To save time, invoke the script with the name of the adapter
  98. echo you want changed.
  99. echo.
  100. echo Examples: setip "Local Area Connection 1"
  101. echo setip Wireless static
  102. echo setip Wireless dhcp
  103. echo ************************************************************
  104. echo When setting the adapter here, use quotes around the name if
  105. echo it contains spaces.
  106. echo.
  107. set /p ADAPTER=Enter the name of the ADAPTER to change:
  108. goto start
  109.  
  110.  
  111. :::::::::::::::::
  112. :: Main Screen ::
  113. :::::::::::::::::
  114. :start
  115. color 07 & title TCP/IP Config for %ADAPTER% & cls
  116. ipconfig /all > "%TEMP%\tempLANconfig.txt"
  117. findstr /R "IPv4 IPv6 Subnet DHCP Dhcp IP.A Default Ethernet DNS.Ser" <"%TEMP%\tempLANconfig.txt" >"%TEMP%\tempLANconfig2.txt"
  118. echo.
  119. echo ------------------------------
  120. echo Current TCP/IP Configuration
  121. echo ------------------------------
  122. echo.
  123. type "%TEMP%\tempLANconfig2.txt"
  124. echo.
  125. echo.
  126. echo Adapter to be changed is: %ADAPTER%
  127. echo -----------------------------------
  128. echo Enter "1" to change the IP address manually.
  129. echo Enter "2" to set the adapter to DHCP and attempt to acquire an address.
  130. echo Enter "3" to pick a different adapter to change.
  131. echo Enter "4" to quit.
  132. :menu
  133. set choice=
  134. echo.
  135. set /p choice=Choice:
  136. if not '%choice%'=='' set choice=%choice:~0,1%
  137. if '%choice%'=='1' goto enter_values
  138. if '%choice%'=='2' goto dhcp
  139. if '%choice%'=='3' goto specify_adapter
  140. if '%choice%'=='4' goto cancel
  141. echo.
  142. echo "%choice%" is not valid, please try again
  143. echo.
  144. goto menu
  145. pause
  146.  
  147. ::::::::::::::::::
  148. :: Enter Values ::
  149. ::::::::::::::::::
  150. :enter_values
  151. echo.
  152. echo Modifying adapter %ADAPTER%
  153. echo.
  154. set /p IP=Enter the new IP address:
  155. set /p MASK=Enter the new subnet mask:
  156. set /p GATEWAY=Enter the new default gateway:
  157. set DNS1=%GATEWAY%
  158.  
  159. echo.
  160. echo These default DNS servers will be loaded:
  161. echo Primary: %GATEWAY% (default gateway)
  162. echo Secondary: %DNS2% (OpenDNS)
  163. echo Tertiary: %DNS3% (OpenDNS)
  164. echo.
  165. set /p CHOICE=Is this okay? [Y/n]:
  166. if '%CHOICE%'=='y' goto execute_prompt
  167. if '%CHOICE%'=='n' goto enter_dns
  168.  
  169. :::::::::::::
  170. :: EXECUTE ::
  171. :::::::::::::
  172. :execute_prompt
  173. color f0
  174. cls
  175. echo.
  176. echo About to apply this configuration:
  177. echo.
  178. echo IP Address: %IP%
  179. echo Subnet Mask: %MASK%
  180. echo Default Gateway: %GATEWAY%
  181. echo DNS Servers: %DNS1% (Primary)
  182. echo %DNS2%
  183. echo %DNS3%
  184. echo.
  185. set /p CHOICE=Apply this configuration? [Y/n]:
  186. if '%CHOICE%'=='y' goto execute_do
  187. if '%CHOICE%'=='n' goto start
  188.  
  189. :execute_do
  190. color 07 & cls
  191. echo.
  192. echo Applying the TCP/IP configuration.
  193. echo This could take up to 30 seconds, please be patient...
  194. echo.
  195. echo Setting IP address to %IP%...
  196. netsh interface ip set address name=%ADAPTER% source=static %IP% %MASK% %GATEWAY% 1
  197. echo Setting the default gateway %GATEWAY% as primary DNS...
  198. netsh interface ip set dns name=%ADAPTER% static %DNS1% primary
  199. echo Setting %DNS2% and %DNS3% as the alternate DNS servers...
  200. netsh interface ip add dns name=%ADAPTER% %DNS2% index=2
  201. netsh interface ip add dns name=%ADAPTER% %DNS3% index=3
  202. goto done
  203.  
  204.  
  205. ::::::::::
  206. :: DHCP ::
  207. ::::::::::
  208. :dhcp
  209. echo.
  210. echo Using DHCP to acquire IP address for adapter %ADAPTER%...
  211. echo.
  212. netsh interface ip set address name=%ADAPTER% source=dhcp
  213. echo Using DHCP to acquire DNS servers...
  214. netsh interface ip set dns name=%ADAPTER% source=dhcp
  215. ipconfig /renew
  216. goto done
  217.  
  218.  
  219. ::::::::::::::::
  220. :: Manual DNS :: Only used when user isn't okay with default DNS settings. Is skipped most of the time.
  221. ::::::::::::::::
  222. :enter_dns
  223. set /p DNS1=Enter IP address of first DNS server:
  224. set /p DNS2=Enter IP address of second DNS server:
  225. set /p DNS3=Enter IP address of third DNS server:
  226. goto execute_prompt
  227.  
  228. ::::::::::
  229. :: DONE ::
  230. ::::::::::
  231. :done
  232. cls & color f0
  233. :: Show user the results. We output IP config information to a file, strip out some stuff, then present the results. Finally, we delete the temp file.
  234. echo.
  235. echo Results:
  236. echo --------
  237. ipconfig /all > "%TEMP%\tempLANconfig.txt"
  238. findstr /R "IPv4 IPv6 Subnet DHCP Dhcp IP.A Default Ethernet DNS.Ser" <"%TEMP%\tempLANconfig.txt" >"%TEMP%\tempLANconfig2.txt"
  239. echo.
  240. type "%TEMP%\tempLANconfig2.txt"
  241. echo.
  242. del "%TEMP%\tempLANconfig.txt" /Q 2>nul
  243. del "%TEMP%\tempLANconfig2.txt" /Q 2>nul
  244. pause
  245. goto end
  246.  
  247. :cancel
  248. title Shell
  249. cls
  250. echo.
  251. echo Canceled! Goodbye.
  252.  
  253. :end
  254. title Shell
  255. del "%TEMP%\tempLANconfig.txt" /Q 2>nul
  256. del "%TEMP%\tempLANconfig2.txt" /Q 2>nul
  257. ENDLOCAL
  258. color
Advertisement
Add Comment
Please, Sign In to add comment