一. 概要

有个同学来反应自己的暗影精灵9(Windows11)在校园网环境下无法登录 Moodle 和学校官网,经检查发现其笔记本安装了代理软件 Clash。

排查问题:在连接 eduroam 时无法通过 DHCP 自动获取到本地 DNS 服务器地址,默认 DNS 被锁死为 114.114.114.114 / 8.8.8.8,因此无法正常解析学校域名。暂时不太清楚为什么会造成这种情况,卸载和停止运行 Clash 也没有解决问题。

之前有给他直接设置hosts文件,路径在 C:\\Windows\\System32\\drivers\\etc\\hosts,内容如下:

1
2
10.150.4.94 moodle.gtiit.edu.cn
10.103.4.45 gtiit.edu.cn

通过改hosts的方式,在校园网下当然可以访问,但是如果连上手机热点就无法访问了,因为这里配置的是学校局域网IP。

后来这个同学装了一个火绒安全软件,使用了里面的断网修复工具后,发现又没办法上 Moodle 和学校官网了。这可能是工具自行修改了hosts文件导致的。

为了比较完美解决这个问题,不如写一个脚本:当这位同学使用校园网的时候,就自动配置好IP、网关和DNS等,在使用手机热点或家里的网络时再一键恢复为自动获取网络配置。

二. 使用方式

SETP 1:管理员身份运行BAT脚本

STEP 1:运行脚本

STEP 2: 选择校园网环境

记得要先连接无线WIFI:eduroam,然后在命令行中输入2并回车,如图所示:

STEP 2:选择环境

这里我固定了前24位IPV4地址,后8位自己填写。当然也可以改成直接自动获取(详见第三部分脚本2)。

然后脚本会运行相关命令进行设置:

脚本运行中

STEP3:恢复默认网络配置

当我们不使用校园网时,我们要自动获取IP和DNS等,因此要恢复网络设置。

STEP 3:恢复网络设置

三. 脚本源码

脚本1详细代码如下,该脚本在选择校园网环境时可以指定IP(固定了前24位,也可以自己修改代码):

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
@echo off&color 1E&title GTIIT-NETWORK-SWITCH-TOOL
echo ---------------------------------------------------------------------------------------------
echo * *
echo * To switch the network environment, select your current network condition(FOR WIFI) *
echo * Need to run as Administrator!!! *
echo * By GTIIT-Zach github.com/cszmzh *
echo * *
echo ---------------------------------------------------------------------------------------------
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

:choice
set choice=
set /p choice= Restore Network Config please input 1, For GTIIT-Campus-Network please input 2 and enter: [1,2] ?
if %choice%==2 goto school_lan
if %choice%==1 (goto lab_lan) else (echo ERROR, please input again&goto choice)

:lab_lan
set eth="WLAN"
set ip=
set netmask=
set gw=
set dns1=
set dns2=
echo.
echo Restore to default network configuration
echo.
goto public

:school_lan
set eth="WLAN"
set ip=10.211.137.
set netmask=255.255.0.0
set gw=10.211.0.1
set dns1=10.150.4.13
set dns2=10.103.4.10
echo.
echo Switch to GITTI Campus network configuration
echo.
goto switch

:switch
set code=
set /p code= input your IP number[3-254] %ip%
set "ip=%ip%%code%"
echo Setting the IP address to %ip%
netsh interface ip set address %eth% static %ip% %netmask% %gw% 1
echo Setting the DNS1 address to %dns1%
netsh interface ip set dns %eth% static %dns1% register=PRIMARY validate=no
if defined dns2 (
echo Setting the DNS2 address to %dns2%
netsh interface ip add dns %eth% %dns2% index=2 validate=no
)
echo.
echo IP setting succeeded: %ip%
echo.
goto end

:public
echo Setting the IP address to automatically obtain
netsh interface ip set address %eth% dhcp
echo Setting the DNS address to automatically obtain
netsh interface ip set dns %eth% dhcp
echo Obtaining IP automatically, hang on for a while...
echo.

::for /L %%x in (1 1 10) do set /p gu=<nul&ping /n 2 127.1>nul
for /f "tokens=16" %%i in ('ipconfig ^|find /i "ipv4"') do (
set myip=%%i
:: 正常情况下find查询只有一行结果,如果主机安装了虚拟机则会有多个适配器有ip地址。第一个才是本机IP,故使用goto保证for只执行一次就跳出循环,防止后续myip的值被覆盖
goto out
)
:out
echo Get local IP: %myip%

echo 100%%
echo.
echo The network is restored successfully. Everything is the default configuration.
echo.
goto end

:end
@echo on
@pause

脚本2详细代码如下(自动获取校园网IP,只是改DNS,完美解决该同学的问题):

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
@echo off&color 1E&title GTIIT-NETWORK-SWITCH-TOOL
echo ---------------------------------------------------------------------------------------------
echo * *
echo * To switch the network environment, select your current network condition(FOR WIFI) *
echo * Need to run as Administrator!!! *
echo * By GTIIT-Zach github.com/cszmzh *
echo * *
echo ---------------------------------------------------------------------------------------------
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

:choice
set choice=
set /p choice= Restore Network Config please input 1, For GTIIT-Campus-Network please input 2 and enter: [1,2] ?
if %choice%==2 goto school_lan
if %choice%==1 (goto lab_lan) else (echo ERROR, please input again&goto choice)

:lab_lan
set eth="WLAN"
set ip=
set netmask=
set gw=
set dns1=
set dns2=
echo.
echo Restore to default network configuration
echo.
goto public

:school_lan
set eth="WLAN"
set dns1=10.150.4.13
set dns2=10.103.4.10
echo.
echo Switch to GITTI Campus network configuration
echo.
goto switch

:switch
echo Setting the DNS1 address to %dns1%
netsh interface ip set dns %eth% static %dns1% register=PRIMARY validate=no
if defined dns2 (
echo Setting the DNS2 address to %dns2%
netsh interface ip add dns %eth% %dns2% index=2 validate=no
)
echo.
goto end

:public
echo Setting the IP address to automatically obtain
netsh interface ip set address %eth% dhcp
echo Setting the DNS address to automatically obtain
netsh interface ip set dns %eth% dhcp
echo Obtaining IP automatically, hang on for a while...
echo.

::for /L %%x in (1 1 10) do set /p gu=<nul&ping /n 2 127.1>nul
for /f "tokens=16" %%i in ('ipconfig ^|find /i "ipv4"') do (
set myip=%%i
:: 正常情况下find查询只有一行结果,如果主机安装了虚拟机则会有多个适配器有ip地址。第一个才是本机IP,故使用goto保证for只执行一次就跳出循环,防止后续myip的值被覆盖
goto out
)
:out
echo Get local IP: %myip%

echo 100%%
echo.
echo The network is restored successfully. Everything is the default configuration.
echo.
goto end

:end
@echo on
@pause

注意:eth对应接口名,可以在控制面板-网络适配器中查看名字。一般名称为:以太网、WLAN、Wi-Fi、Ethernet等。

获取eth接口名

eth接口名也可以用以下脚本代码获取:

1
2
3
4
5
@Echo Off
For /F "Skip=1Delims=" %%a In (
'"WMIC NIC Where (Not NetConnectionStatus Is Null) Get NetConnectionID"'
) Do For /F "Tokens=*" %%b In ("%%a") Do Echo=%%b
Timeout -1