共计 2636 个字符,预计需要花费 7 分钟才能阅读完成。
因公司重新规划网络,以前的固定 IP 需重新分配,又不想一台台的去改,那个不是一般的累。用 DHCP 又不符合公司环境,所以就搞了这么个脚本出来。
一、VBS 脚本,通过 MAC 地址来修改 IP、子网掩码、网关和 DNS.
‘by Winters 2013-3-27 8win.net
‘ 为防止修改未成功而 IP 冲突,先修改 IP 获取为自动获取 IP 地址
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” & strComputer & “rootcimv2”)
Set colNetAdapters = objWMIService.ExecQuery _
(“Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE”)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableDHCP()
errDNS = objNetAdapter.SetDNSServerSearchOrder(null)
Next
‘ 取得本机器的网卡物理地址:MacAddress
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!” & strComputer & “rootcimv2”)
Set colNicConfigs = objWMIService.ExecQuery _
(“SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True”)
For Each objNicConfig In colNicConfigs
Set objNic = objWMIService.Get _
(“Win32_NetworkAdapter.DeviceID=” & objNicConfig.Index)
‘ 本机器的网卡物理地址写入变量 : strMacAddress
strMacAddress = objNic.MACAddress
Next
‘ 读取配置文件, 这里需要有个 IPlist.txt, 最好是用绝对地址
Set fso = CreateObject(“Scripting.filesystemobject”)
Set fr = fso.OpenTextFile(“iplist.txt”, 1)
Do While fr.AtEndOfStream <> True
t = UCase(fr.ReadLine)
If InStr(t, strMacAddress) Then
t = Split(t, “|”)
Name = t(0)
IPAddress = t(2)
Exit Do
End If
Loop
‘ 修改 IP 地址
Set objWMIService = GetObject(“winmgmts:” & strComputer & “rootcimv2”)
Set colNetAdapters = objWMIService.ExecQuery _
(“Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE”)
strIPAddress = Array(IPAddress)
strSubnetMask = Array(“255.255.255.0”)
strGateway = Array(“192.168.0.1”)
strGatewayMetric = Array(1)
strDNS = Array(“192.168.0.1″,”192.168.0.2”)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS)
Next
以上为 VBS 脚本部分,有同样需求的要根据自己的情况做些修改
二、IP&MAC 扫描。以下为批处理脚本:
@echo off&title 扫描 MAC,IP, 计算机名 &color a&mode con: cols=60 lines=22
setlocal enabledelayedexpansion
echo.
echo ============== 制作者:https://www.8win.net ===============
echo.
echo 扫描计算机名、MAC 地址和内网 IP,并保存到 iplist.txt 文件
echo.
echo ================== 制作不易,请保留版权 ==================
echo.
:: iqp -> 内网 IP 的前 3 位,ip1 -> 起始 ip,ip2 -> 结束 ip
set ipq=192.168.0
set /a ip1=1
set /a ip2=10
set /a con=0
set /a contotal=%ip2%-%ip1%+1
echo PCName MACAddress IPAddress >iplist.txt
for /l %%i in (!ip1!,1,!ip2!) do (
set ip=!ipq!.%%i
echo 扫描!ip!
ping !ip! -n 1 -w 1 >nul
if !errorlevel! equ 0 (
for /f “tokens=1” %%j in (‘nbtstat -a !ip! ^| find /i “UNIQUE” ^| find /i “00”‘) do set pcname=%%j
for /f “tokens=4 delims=* ” %%j in (‘nbtstat -a !ip! ^| find /i “MAC Address”‘) do set mac=%%j
echo !pcname!;!mac:~0,-1!;!ip! >>iplist.txt
set /a con=!con!+1
)
)
echo 共 %con% 条记录
echo 共扫描:%contotal% 台机器,扫描到:%con% 个有效记录 &pause >nul
1、修改脚本中 IPQ(网段)IP1(起始 IP)和 IP2(结束 IP)
2、修改生成的 iplist.txt 文件,每行有两处需修改。第一是修改间隔符“;”为“|”,第二是修改获取的 MAC 地址中间的分隔符“-”为“:”另外可以的话清除 IP 地址后面的那个空格。