首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

VBScript 設定 IE Proxy 伺服器腳本範例

     編輯:Java 相關知識

功能說明

此 VBScript 腳本從預定義代理列表中逐一測試連通性(Ping),發現可用代理後彈出確認對話框,用戶確認即寫入註冊表啟用 IE 代理設置。

完整腳本代碼

' ieproxy.vbs
' 用法:命令提示符執行 cscript ieproxy.vbs

Option Explicit

Dim strMachines, aMachines, machine2, machinearr, machine
Dim objPing, objStatus, objReg, strComputer, strKeyPath, strEntryName, dwValue

' 代理列表(IP:Port),實際使用請替換為有效代理
strMachines = "24.11.246.125:2406;61.252.60.30:68660;64.161.10.2:3128;65.23.157.55:80;66.229.103.146:5089"

aMachines = Split(strMachines, ";")

For Each machine2 In aMachines
    machinearr = Split(machine2, ":")
    machine = machinearr(0)
    
    ' WMI Ping 測試
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
        ExecQuery("select * from Win32_PingStatus where address = '" & machine & "'")
    
    For Each objStatus In objPing
        If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
            WScript.Echo machine2 & " is not reachable"
        Else
            WScript.Echo machine2 & " is OK"
            If Confirm("設定代理為 " & machine2 & "?") Then
                MsgBox SetIEProxy(1, machine2)
            End If
        End If
    Next
Next

' 確認對話框函數
Function Confirm(prompt)
    Confirm = (MsgBox(prompt, vbYesNo, prompt) = vbYes)
End Function

' 修改註冊表設置 IE Proxy
Function SetIEProxy(ProxyEnable, ProxyIP)
    On Error Resume Next
    Const HKEY_CURRENT_USER = &H80000001
    strComputer = "."
    
    Set objReg = GetObject("winmgmts:" & _
        "{impersonationLevel=impersonate}\\" & strComputer & "\root\default:StdRegProv")
    
    strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\"
    
    ' 啟用/禁用代理
    strEntryName = "ProxyEnable"
    dwValue = ProxyEnable
    objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strEntryName, dwValue
    
    ' 設置代理地址
    strEntryName = "ProxyServer"
    dwValue = ProxyIP
    objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strEntryName, dwValue
    
    If Err.Number = 0 Then
        SetIEProxy = True
    Else
        SetIEProxy = False
    End If
End Function

MsgBox "完成"

關鍵技術點解析

1. WMI Ping 檢測

使用 Win32_PingStatus 類,比傳統 Shell.Run "ping" 更精準獲取狀態碼(0 表示成功)。

2. 註冊表操作

  • 根鍵:HKEY_CURRENT_USER (當前用戶)。
  • 路徑:Software\Microsoft\Windows\CurrentVersion\Internet Settings。
  • 值:ProxyEnable (DWORD, 1=啟用),ProxyServer (字符串, "ip:port")。

3. 錯誤處理

On Error Resume Next 配合 Err.Number 判斷註冊表寫入是否成功。

使用注意事項

  1. 需以管理員權限運行(修改 HKCU 通常不需,但 WMI 查詢可能需權限)。
  2. 代理列表需根據實際環境更新,示例 IP 可能已失效。
  3. 僅影響 IE/系統代理設置,現代瀏覽器(Chrome/Edge)可能使用獨立代理配置。

適用場景

  • 自動化測試環境快速切換代理。
  • 批量驗證代理池可用性並自動應用。
  • 學習 VBScript/WMI/註冊表操作範例。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved