IP,子网,网关,DNS都需要备份还原- @echo off
- setlocal
-
- REM 设置备份文件名
- set BACKUP_FILE=%USERPROFILE%\Desktop\network_config_backup.txt
-
- REM 获取当前的网络配置信息
- ipconfig /all > %BACKUP_FILE%
-
- REM 提取IP地址、子网掩码、默认网关和DNS服务器
- for /f "tokens=2 delims=:" %%a in ('findstr /i "IPv4 Address" %BACKUP_FILE%') do set IP=%%a
- for /f "tokens=2 delims=:" %%a in ('findstr /i "Subnet Mask" %BACKUP_FILE%') do set MASK=%%a
- for /f "tokens=2 delims=:" %%a in ('findstr /i "Default Gateway" %BACKUP_FILE%') do set GATEWAY=%%a
- for /f "tokens=2 delims=:" %%a in ('findstr /i "DNS Servers" %BACKUP_FILE%') do set DNS=%%a
-
- REM 将提取的信息写入备份文件
- echo IP Address: %IP% >> %BACKUP_FILE%
- echo Subnet Mask: %MASK% >> %BACKUP_FILE%
- echo Default Gateway: %GATEWAY% >> %BACKUP_FILE%
- echo DNS Servers: %DNS% >> %BACKUP_FILE%
-
- echo Network configuration has been backed up to %BACKUP_FILE%.
-
- endlocal
- pause
-
- 还原IP
- @echo off
- setlocal
-
- REM 设置备份文件名
- set BACKUP_FILE=%USERPROFILE%\Desktop\network_config_backup.txt
-
- REM 检查备份文件是否存在
- if not exist "%BACKUP_FILE%" (
- echo Backup file does not exist. Please make sure you have a valid backup file.
- pause
- exit /b
- )
-
- REM 从备份文件中读取网络配置
- for /f "tokens=2 delims=:" %%a in ('findstr /i "IP Address" %BACKUP_FILE%') do set IP=%%a
- for /f "tokens=2 delims=:" %%a in ('findstr /i "Subnet Mask" %BACKUP_FILE%') do set MASK=%%a
- for /f "tokens=2 delims=:" %%a in ('findstr /i "Default Gateway" %BACKUP_FILE%') do set GATEWAY=%%a
- for /f "tokens=2 delims=:" %%a in ('findstr /i "DNS Servers" %BACKUP_FILE%') do set DNS=%%a
-
- REM 去除可能存在的空格
- set IP=%IP:~1%
- set MASK=%MASK:~1%
- set GATEWAY=%GATEWAY:~1%
- set DNS=%DNS:~1%
-
- REM 设置网络配置
- netsh interface ip set address "本地连接" static %IP% %MASK% %GATEWAY% 1
- netsh interface ip add dns "本地连接" %DNS% index=1
-
- echo Network configuration has been restored from %BACKUP_FILE%.
-
- endlocal
- pause
复制代码 这个是AI写的用不了,麻烦大神指导一下,谢谢! |