您的位置:首页 > 编程语言 > VB

根据mac来配置ip的vbs脚本

2011-07-09 23:44 155 查看

 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了。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: