Guest User

[BATCH] SetIP v3.6b (2014-07-23)

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