标题: [网络连接] 多网卡的IP地址备份还原批处理该怎么写? [打印本页]
作者: 19920030023 时间: 2024-9-25 23:38 标题: 多网卡的IP地址备份还原批处理该怎么写?
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写的用不了,麻烦大神指导一下,谢谢!
作者: 杨正齐 时间: 2024-9-26 21:40
@ECHO off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
setlocal enabledelayedexpansion
mode con cols=50 lines=4
:SystemBegin
netsh interface ip dump > d:\slyar_ip.txt
pause
netsh -f d:\slyar_ip.txt
pause
pushd interface ip
set address name="本地连接" source=static addr=172.24.0.1 mask=255.255.255.0
set address name="本地连接" gateway=172.24.0.254 gwmetric=0
set dns name="本地连接" source=static addr=172.16.5.133 register=PRIMARY
add dns name="本地连接" addr=172.16.5.132 index=2
set wins name="本地连接" source=static addr=none
popd
pause
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |