-
Notifications
You must be signed in to change notification settings - Fork 0
/
setDns.bat
173 lines (144 loc) · 3.52 KB
/
setDns.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
@echo off
setlocal
set iname=
set dns=
set alt=
set ipv=ipv4
set validate=no
set /a list_interfaces=0
set prog_name=%~n0%~x0
set user_dir="%~dp0"
set verbose=0
GOTO :ParseParams
:ParseParams
REM IF "%~1"=="" GOTO Main
if [%1]==[/?] goto help
if /i [%1]==[/h] goto help
if /i [%1]==[/help] goto help
IF /i "%~1"=="/a" (
SET alt=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/d" (
SET dns=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/n" (
SET iname=%2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/c" (
SET validate=yes
goto reParseParams
)
IF /i "%~1"=="/6" (
SET ipv=ipv6
goto reParseParams
)
IF /i "%~1"=="/l" (
SET /a list_interfaces=1
goto reParseParams
)
IF /i "%~1"=="/v" (
SET /a verbose=1
goto reParseParams
) ELSE (
echo Unknown option : "%~1"
)
:reParseParams
SHIFT
if [%1]==[] goto main
GOTO :ParseParams
:main
set /a c=0
:: if not [%iname%] == [] set /a c=1
:: if not [%dns%] == [] set /a c=1
:: if not [%dns%] == [""] set /a c=1
:: if %list_interfaces% == 1 set /a c=1
if %c% == 1 goto usage
if %verbose% == 1 (
echo name=%iname%
echo dns=%dns%
echo alt=%alt%
echo ipv=%ipv%
echo validate=%validate%
)
if %list_interfaces% == 1 (
echo List interfaces:
netsh interface %ipv% show interfaces
echo.
goto mainend
)
:checkPermissions
:: echo checking Admin permissions...
net session >nul 2>&1
if NOT %errorlevel% == 0 (
echo [e] Please run as Admin!
call
goto mainend
)
if not [%dns%] == [] (
call :setDns %ipv% %iname% %dns% %validate% %alt%
) else (
call :checkDns %ipv% %iname%
)
:mainend
endlocal
exit /B %errorlevel%
:setDns
setlocal
set ipv=%1
set iname=%2
set dns=%3
set validate=%4
set alt=%5
if [%iname%] == [] (
echo [e] No interface name or id set!
exit /b 1
)
if [%dns%] == [auto] (
set command=netsh interface %ipv% set dns name=%iname% dhcp validate=%validate%
) else (
set command=netsh interface %ipv% set dns name=%iname% static %dns% validate=%validate%
)
if %verbose% == 1 (
echo %command%
)
%command%
if not [%alt%] == [] (
netsh interface %ipv% add dns name=%iname% %alt% index=2 validate=%validate%
)
if [%verbose%]==[1] (
call :checkDns %ipv% %iname%
)
endlocal
exit /B %errorlevel%
:checkDns
setlocal
set ipv=%1
set iname=%2
if [%iname%] == [] (
netsh interface %ipv% show dns
) else (
netsh interface %ipv% show dns name=%iname%
)
endlocal
exit /B %errorlevel%
:usage
echo Usage: %prog_name% /d ^<dnsIp^> [/n ^<name^>] [/a ^<altIp^>] [/6] [/c] [/l] [/v] [/h]
exit /B 0
:help
call :usage
echo.
echo Options:
echo /d The preferred DNS server ip address as dotted string. Or 'auto' to automatically obtain dns server (dhcp).
echo /a The alternative DNS server ip address as dotted string.
echo /n The interface name. If name does not work (Element not found), try replacing the name with the index found by listing (/l) the interfaces.
echo /c Validate the settings.
echo /6 Set ip version to ipv6.
echo /l List interfaces.
echo /v Verbose mode
exit /B 0