根据mac来配置ip的vbs脚本
2011-07-09 23:44
435 查看
vbs 脚本 setnicip.vbs 内容如下:
'Option Explicit
Const ForReading= 1,ForWriting = 2,ForAppending = 8
Dim fso,file,msg
Dim WshShell,Path_init_file
Dim routmac,routip,routmask,routgw
Dim rinmac,rinip,rinmask,ringw
Dim k,m
Set WshShell=WScript.CreateObject( "WScript.Shell")
Path_init_file=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))& "nicinfo.ini"
'msgbox(path_init_file)
Set fso=CreateObject("Scripting.FileSystemObject")
Set file=fso.OpenTextFile(Path_init_file,ForReading)
While (Not file.AtEndOfLine)
msg=file.ReadLine
msg=trim(msg)
if instr(msg,"outmac:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routmac = Right(msg, m - k)
'msgbox(routmac)
else
end if
if instr(msg,"outip:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routip = Right(msg, m - k)
'msgbox(routip)
else
end if
if instr(msg,"outmask:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routmask = Right(msg, m - k)
'msgbox(routmask)
else
end if
if instr(msg,"outgw:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routgw = Right(msg, m - k)
'msgbox(routgw)
else
end if
'+++++++++++++++++++++++++++++++++++++++++++++
if instr(msg,"inmac:")>0 then
k = InStr(msg, ":")
m = Len(msg)
rinmac = Right(msg, m - k)
'msgbox(rinmac)
else
end if
if instr(msg,"inip:")>0 then
k = InStr(msg, ":")
m = Len(msg)
rinip = Right(msg, m - k)
'msgbox(rinip)
else
end if
if instr(msg,"inmask:")>0 then
k = InStr(msg, ":")
m = Len(msg)
rinmask = Right(msg, m - k)
'msgbox(rinmask)
else
end if
if instr(msg,"ingw:")>0 then
k = InStr(msg, ":")
m = Len(msg)
ringw = Right(msg, m - k)
if ringw="none" then
ringw=""
else
end if
'msgbox(ringw)
else
end if
Wend
file.Close
Set file=Nothing
Set fso=Nothing
'Dim strComputer,objWMIService,colNetAdapters,strIPAddress,strSubnetMask,strGateway,errEnable,errGateways,objNetAdapter
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where (MACAddress ='"+ routmac+"' )")
strIPAddress = Array(routip)
strSubnetMask = Array(routmask)
strGateway = Array(routgw)
strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
next
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where (MACAddress = '"+rinmac+"')")
strIPAddress = Array(rinip)
strSubnetMask = Array(rinmask)
strGateway = Array(ringw)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
next
在同一个目录下,建立一个文件,名叫nicinfo.ini,内容如下:
outmac:00:15:AF:C8:5D:E3
outip:192.168.56.2
outmask:255.255.255.0
outgw:
inmac:00:AC:D9:00:8A:16
inip:172.16.254.6
inmask:255.255.255.0
ingw:none
根据自己的需要,修改配置文件内容,然后运行vbs脚本,就可以修改ip了。
'Option Explicit
Const ForReading= 1,ForWriting = 2,ForAppending = 8
Dim fso,file,msg
Dim WshShell,Path_init_file
Dim routmac,routip,routmask,routgw
Dim rinmac,rinip,rinmask,ringw
Dim k,m
Set WshShell=WScript.CreateObject( "WScript.Shell")
Path_init_file=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))& "nicinfo.ini"
'msgbox(path_init_file)
Set fso=CreateObject("Scripting.FileSystemObject")
Set file=fso.OpenTextFile(Path_init_file,ForReading)
While (Not file.AtEndOfLine)
msg=file.ReadLine
msg=trim(msg)
if instr(msg,"outmac:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routmac = Right(msg, m - k)
'msgbox(routmac)
else
end if
if instr(msg,"outip:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routip = Right(msg, m - k)
'msgbox(routip)
else
end if
if instr(msg,"outmask:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routmask = Right(msg, m - k)
'msgbox(routmask)
else
end if
if instr(msg,"outgw:")>0 then
k = InStr(msg, ":")
m = Len(msg)
routgw = Right(msg, m - k)
'msgbox(routgw)
else
end if
'+++++++++++++++++++++++++++++++++++++++++++++
if instr(msg,"inmac:")>0 then
k = InStr(msg, ":")
m = Len(msg)
rinmac = Right(msg, m - k)
'msgbox(rinmac)
else
end if
if instr(msg,"inip:")>0 then
k = InStr(msg, ":")
m = Len(msg)
rinip = Right(msg, m - k)
'msgbox(rinip)
else
end if
if instr(msg,"inmask:")>0 then
k = InStr(msg, ":")
m = Len(msg)
rinmask = Right(msg, m - k)
'msgbox(rinmask)
else
end if
if instr(msg,"ingw:")>0 then
k = InStr(msg, ":")
m = Len(msg)
ringw = Right(msg, m - k)
if ringw="none" then
ringw=""
else
end if
'msgbox(ringw)
else
end if
Wend
file.Close
Set file=Nothing
Set fso=Nothing
'Dim strComputer,objWMIService,colNetAdapters,strIPAddress,strSubnetMask,strGateway,errEnable,errGateways,objNetAdapter
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where (MACAddress ='"+ routmac+"' )")
strIPAddress = Array(routip)
strSubnetMask = Array(routmask)
strGateway = Array(routgw)
strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
next
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where (MACAddress = '"+rinmac+"')")
strIPAddress = Array(rinip)
strSubnetMask = Array(rinmask)
strGateway = Array(ringw)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
next
在同一个目录下,建立一个文件,名叫nicinfo.ini,内容如下:
outmac:00:15:AF:C8:5D:E3
outip:192.168.56.2
outmask:255.255.255.0
outgw:
inmac:00:AC:D9:00:8A:16
inip:172.16.254.6
inmask:255.255.255.0
ingw:none
根据自己的需要,修改配置文件内容,然后运行vbs脚本,就可以修改ip了。
相关文章推荐
- 根据MAC地址修改固定IP(附带IP&MAC扫描脚本)
- 用VBS方法获得双网卡或多网卡的IP,MAC,计算机名方法
- linux下根据ip调用命令获取mac
- shell脚本-根据连接数封IP
- linux下ip配置脚本
- shell脚本实现从master节点批量配置salve节点(主机名有瑕疵,IP映射,ssh服务)
- Linux下获取IP、MAC、网关、掩码的shell脚本
- XP系统下快速切换ip的bat脚本配置
- Cisco 2811 DHCP服务器 配置IP-MAC绑定
- 根据IP和MAC查端口
- 脚本-根据ip和mask获取网络地址
- java根据本地IP获取mac地址的方法
- java根据本地Ip获取mac地址
- vbs 脚本轻松搞定JDK的环境变量配置
- Linux下获取系统的IP,子网掩码,网关,MAC和配置文件的修改
- SystemCenter2012SP1实践(35)CentOS下用脚本快速配置IP和主机名
- 网卡IP配置脚本
- mac下通过虚拟机安装minimal centos和固定ip配置
- Hibernate根据配置文件生成表结构和创建表的脚本
- 根据hibernate配置文件生成数据库建表脚本