-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetip.bat
156 lines (131 loc) · 3.2 KB
/
setip.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
@echo off
setlocal enabledelayedexpansion
set prog_name=%~n0%~x0
set user_dir="%~dp0"
set /a verbose=0
set iname=
set ip=
set gw=
set nwm=255.255.240.0
set ipv=ipv4
set /a list_interfaces=0
set /a check=0
if [%~1] == [] goto usage
GOTO :ParseParams
:ParseParams
if [%1]==[/?] goto help
if /i [%1]==[/h] goto help
if /i [%1]==[/help] goto help
IF /i "%~1"=="/c" (
SET /a check=1
goto reParseParams
)
IF /i "%~1"=="/g" (
SET gw=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/i" (
SET ip=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/l" (
SET /a list_interfaces=1
goto reParseParams
)
IF /i "%~1"=="/m" (
SET nwm=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/n" (
SET "iname=%~2"
SHIFT
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 [%ip%] NEQ [] set /a c=1
if [%gw%] NEQ [""] set /a c=1
if %list_interfaces% == 1 set /a c=1
if %c% == 0 goto usage
if %verbose% == 1 (
echo name=%iname%
echo ip=%ip%
echo nwm=%nwm%
echo gw=%gw%
)
if %list_interfaces% == 1 (
echo List interfaces:
netsh interface %ipv% show interfaces
echo.
goto exitMain
)
if %check% == 1 (
set nameParam=
if ["%iname%"] NEQ [] (
if ["%iname%"] NEQ [""] (
set nameParam=name="%iname%"
)
)
set command=netsh interface %ipv% show config !nameParam!
if %verbose% == 1 (
echo !command!
)
!command!
goto exitMain
)
:checkPermissions
net session >nul 2>&1
if NOT %errorlevel% == 0 (
echo [e] Admin privileges required!
exit /b 1
)
:: error checks
if [%ip%] EQU [] (
echo [e] No ip given!
exit /b 1
)
if [%iname%] EQU [] (
echo [e] No interface name or id given!
exit /b 1
)
if [%ip%] == [auto] (
set command=netsh interface %ipv% set address name=%iname% dhcp
) else (
set command=netsh interface %ipv% set address name=%iname% static %ip% %nwm% %gw%
)
if %verbose% == 1 (
echo !command!
)
!command!
:exitMain
if %verbose% == 1 echo exit status : %errorlevel%
endlocal
exit /B %errorlevel%
:usage
echo Usage: %prog_name% /i ^<ip^> /g ^<gateway^> [/m ^<mask^>] [/n ^<name^>] [/l] [/v]
exit /B 0
:help
call :usage
echo.
echo Options:
echo /i IP as dotted string. Or 'auto' to automatically obtain ip address (dhcp).
echo /g Gateway ip as dotted string.
echo /m Network mask as dotted string. Default: 255.255.240.0
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 /l List interfaces
echo /l Check interface configuration
echo /v Verbose mode
exit /B 0