7

Ghost安装Win7/XP后自动恢复IP的批处理

 3 years ago
source link: https://zhang.ge/1885.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Jager · 3月4日 · 2014年IP恢复 3375次已读

今天维护人员找的我说,最近要推Win7了,暂时桌面云没推广,还是使用手动安装Win7的方式。所以,需要我帮忙写一个脚本,大概功能就是在Ghost Win7之前先导出XP的IP信息,在Ghost完成之后,自动读取这个IP信息用于恢复IP设置。

之前其实写过一个XP的,这次是Win7的,其实大同小异了,花了近半小时测试成功了,现在来记录一下。

懒得去折腾兼容方法,就写了2个导出IP的脚本和一个IP恢复脚本,用于Ghost远程安装Win7后自动恢复IP设置的。(繁体环境)

1、导出WinXP系统IP信息:

@echo off&setlocal enabledelayedexpansion
title WindowsXP IP信息導出工具(配合恢復腳本所用)
echo IP信息導出中...
for /f "tokens=1,2 delims=:" %%i in ('ipconfig') do (
echo %%i | find /i "IP Address">nul && echo IP:%%j>D:\ip.txt
echo %%i | find /i "Subnet Mask">nul && echo MK:%%j>>D:\ip.txt
if "!GW!" equ "1" call :end
echo %%i | find /i "Default Gateway">nul && echo GW:%%j>>D:\ip.txt && set GW=1
echo=
echo 導出完畢.任意鍵檢查...
pause>nul
start "" d:\ip.txt

2、导出Win7系统IP信息

@echo off&setlocal enabledelayedexpansion
title Windows 7 IP信息導出工具(配合IP恢復腳本所用)
echo IP信息導出中...
for /f "tokens=1,2 delims=:" %%i in ('ipconfig') do (
echo %%i | find /i "IPv4 位址">nul && echo IP:%%j>D:\ip.txt
echo %%i | find /i "子網路遮罩">nul && echo MK:%%j>>D:\ip.txt
if "!GW!" equ "1" call :end
echo %%i | find /i "預設閘道">nul && echo GW:%%j>>D:\ip.txt && set GW=1
echo=
echo 導出完畢.任意鍵檢查...
pause>nul
start "" d:\ip.txt

Ps:测试过程中,发现win7 使用ipconfig命令,会有个IPv6的网关,导致出错,只好用了一个 if 跳出循环。

3、最后输出到D:\ip.txt的格式如下(太偷懒了,就三行,手动写入也很快!):

IP: 10.197.198.173
MK: 255.255.255.254
GW: 10.197.198.1

最后,通过下面这个脚本读取d:\IP.txt内容并恢复IP:

@echo off&setlocal enabledelayedexpansion
title Ghost系統后的IP恢復工具
rem 判斷文件是否存在
if not exist d:\ip.txt goto error
rem 從IP.txt讀取信息,并設置變量
for /f "tokens=1,2 delims=: " %%a in (d:\ip.txt) do (
if /i "%%a"=="IP" set addr=%%b
if /i "%%a"=="MK" set mask=%%b
if /i "%%a"=="GW" set gateway=%%b
rem 獲取第一塊網卡的名稱,并設置IP,多塊網卡將至設置第一塊
for /f tokens^=1^,2^ delims^=^" %%j in ('netsh interface ip show config') do ( if "%%j"=="介面 " ( netsh interface ip set address name="%%k" source=static addr=!addr! mask=!mask! gateway=!gateway! call :end ) if "%%j"=="接口 " ( netsh interface ip set address name="%%k" source=static addr=!addr! mask=!mask! gateway=!gateway! call :end ) ) :end echo 設置完畢,3S后自動退出! ping -n 3 127.1 >nul exit :error cls echo 錯誤:D盤根目錄未發現ip.txt文件!請手動設置IP,任意鍵退出... pause>nul exit

这个脚本简、繁WinXP/7等系统都可以兼容设定IP,其实是修改自我以前写的Windows下获取网络连线实际名称,加强IP类设置脚本的兼容性中的脚本。

4、脚本用法:

1、封装Ghost系统镜像时,将设置IP的脚本加入启动项

2、Ghost安装系统之前先通过【导出脚本】或【手动的方法】在d盘建立ip.txt,写入如上三条信息

3、通过oneKey远程恢复Ghost系统镜像,等恢复完成之后,IP脚本自动恢复D盘设定好的IP,这时就可以远程连接电脑进行维护了,整个过程均不需要现场操作。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK