[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[网络连接] 多网卡的IP地址备份还原批处理该怎么写?

IP,子网,网关,DNS都需要备份还原
  1. @echo off
  2. setlocal
  3. REM 设置备份文件名
  4. set BACKUP_FILE=%USERPROFILE%\Desktop\network_config_backup.txt
  5. REM 获取当前的网络配置信息
  6. ipconfig /all > %BACKUP_FILE%
  7. REM 提取IP地址、子网掩码、默认网关和DNS服务器
  8. for /f "tokens=2 delims=:" %%a in ('findstr /i "IPv4 Address" %BACKUP_FILE%') do set IP=%%a
  9. for /f "tokens=2 delims=:" %%a in ('findstr /i "Subnet Mask" %BACKUP_FILE%') do set MASK=%%a
  10. for /f "tokens=2 delims=:" %%a in ('findstr /i "Default Gateway" %BACKUP_FILE%') do set GATEWAY=%%a
  11. for /f "tokens=2 delims=:" %%a in ('findstr /i "DNS Servers" %BACKUP_FILE%') do set DNS=%%a
  12. REM 将提取的信息写入备份文件
  13. echo IP Address: %IP% >> %BACKUP_FILE%
  14. echo Subnet Mask: %MASK% >> %BACKUP_FILE%
  15. echo Default Gateway: %GATEWAY% >> %BACKUP_FILE%
  16. echo DNS Servers: %DNS% >> %BACKUP_FILE%
  17. echo Network configuration has been backed up to %BACKUP_FILE%.
  18. endlocal
  19. pause
  20. 还原IP
  21. @echo off
  22. setlocal
  23. REM 设置备份文件名
  24. set BACKUP_FILE=%USERPROFILE%\Desktop\network_config_backup.txt
  25. REM 检查备份文件是否存在
  26. if not exist "%BACKUP_FILE%" (
  27.     echo Backup file does not exist. Please make sure you have a valid backup file.
  28.     pause
  29.     exit /b
  30. )
  31. REM 从备份文件中读取网络配置
  32. for /f "tokens=2 delims=:" %%a in ('findstr /i "IP Address" %BACKUP_FILE%') do set IP=%%a
  33. for /f "tokens=2 delims=:" %%a in ('findstr /i "Subnet Mask" %BACKUP_FILE%') do set MASK=%%a
  34. for /f "tokens=2 delims=:" %%a in ('findstr /i "Default Gateway" %BACKUP_FILE%') do set GATEWAY=%%a
  35. for /f "tokens=2 delims=:" %%a in ('findstr /i "DNS Servers" %BACKUP_FILE%') do set DNS=%%a
  36. REM 去除可能存在的空格
  37. set IP=%IP:~1%
  38. set MASK=%MASK:~1%
  39. set GATEWAY=%GATEWAY:~1%
  40. set DNS=%DNS:~1%
  41. REM 设置网络配置
  42. netsh interface ip set address "本地连接" static %IP% %MASK% %GATEWAY% 1
  43. netsh interface ip add dns "本地连接" %DNS% index=1
  44. echo Network configuration has been restored from %BACKUP_FILE%.
  45. endlocal
  46. pause
复制代码
这个是AI写的用不了,麻烦大神指导一下,谢谢!

@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

TOP

返回列表