Board logo

标题: 求一个切换网卡的脚本 [打印本页]

作者: lfplft     时间: 2008-10-13 13:10    标题: 求一个切换网卡的脚本

我有两块网卡,在网络连接里显示为“本地连接1”和“本地连接2“,平时只启用一个,现在想用一个脚本,实现以下功能:判断现在启用的是哪一个网卡,把它停用,并且启用另外一个,然后作出提示。BAT或VBS均可。恳请高手赐教!
作者: 406317577     时间: 2008-10-13 16:36
手动禁止一个不就行了
作者: hackate     时间: 2008-10-13 18:17
这有一个VBS批处理文件,会停用一个网卡,然后启用一个网卡,你可以用这个
里面前两行定义网络的名称,前一个sEnableConnectionName是要启用的网络名,第二行那个是停用的网络名称,这个可以从控制面板-网络连接里面看到;
你只要将这个下面的内容复制下来,在记事本中粘贴出来,保存为VBS文件(如:切换网卡.vbs),就可以执行了

下面是VBS文件内容:
sEnableConnectionName = "无线网络连接"
sDisableConnectionName = "本地连接"

Const ssfCONTROLS = 3
sEnableVerb = "启用(&A)"
sDisableVerb = "停用(&B)"

set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)

set oNetConnections = nothing
for each folderitem in oControlPanel.items
  If folderitem.name = "网络连接" Then
    Set oNetConnections = folderitem.getfolder: exit for
  end if
next

if oNetConnections is nothing then
  msgbox "未找到网络和拨号连接文件夹"
  wscript.quit
end if

'-----------------停用连接--------------------------
set oLanConnection = nothing
for each folderitem in oNetConnections.items
  if lcase(folderitem.name) = lcase(sDisableConnectionName) then
    set oLanConnection = folderitem: exit for
  end if
next

if oLanConnection is nothing then
  msgbox "未找到 '" & sDisableConnectionName & "' item"
Else
  Set oEnableVerb = nothing
  set oDisableVerb = nothing
  For each verb in oLanConnection.verbs
    if verb.name = sDisableVerb then
      set oDisableVerb = verb
      Exit For
    end if
  next
   
  If not (oDisableVerb is nothing) then
    oDisableVerb.DoIt
  end if
end If

'---------------------------启用连接---------------------------------
set oLanConnection = nothing
for each folderitem in oNetConnections.items
  if lcase(folderitem.name) = lcase(sEnableConnectionName) then
    set oLanConnection = folderitem: exit for
  end if
next

if oLanConnection is nothing then
  msgbox "未找到 '" & sEnableConnectionName & "' item"
Else
  Set oEnableVerb = nothing
  set oDisableVerb = nothing
  For each verb in oLanConnection.verbs
    If verb.name = sEnableVerb then
      set oEnableVerb = verb
      Exit For
    End if
  next
   
  If not (oEnableVerb is nothing) then
    oEnableVerb.DoIt
  end if
end If
wscript.sleep 800