通過 WMI 改變網卡的IP地址 ChangeIP.vbs

來源:互聯網
上載者:User

這是第一個版本,就是實現換IP功能,只換第一個發現的,如果只有一個網卡的話可以用這個
view plaincopy to clipboardprint?

    '/*=========================================================================  
    ' * Intro       通過 WMI 改變網卡的IP地址 vbs版  
    ' * FileName    ChangeIP.vbs  
    ' * Author      yongfa365  
    ' * Version     v1.1  
    ' * WEB         http://www.yongfa365.com  
    ' * Email       yongfa365@qq.com  
    ' * FirstWrite  http://www.yongfa365.com/item/Use-WMi-Change-IP-VBS-yongfa365.html  
    ' * LastModify  2007-12-29 13:57:50  
    ' *==========================================================================*/  
      
    strIPAddress = Array("192.168.0.100","192.168.0.106") '修改後的ip,多個IP可以以","格開,可以寫多個  
    strSubnetMask = Array("255.255.255.0","255.255.255.0") '子網路遮罩,配置同IP  
    strGateway = Array("192.168.0.1") '網關  
    arrDNSServers = Array("192.168.0.1", "221.12.1.228")'DNS,可以寫多個  
      
    strComputer = "."  
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")  
    Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")  
    For Each objNetAdapter in colNetAdapters  
        'msgbox colnetadapters.count'看下有幾塊網卡  
        'sip = Join(objNetAdapter.IPAddress, ",") '得到原來的ip  
        'strIPAddress = sip '保持原來的ip  
        strGatewayMetric = Array(1)  
        errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)  
        errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)  
        errDNS = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)  
        If errEnable = 0 Then  
            WScript.Echo "IP修改成功"  
        Else  
            WScript.Echo "IP修改失敗"  
        End If  
      
        Exit For '只修改第一個網卡的設定  
    Next  

這是第二個版本,遍曆所有網卡,如果這個網卡的IP裡包含"116.90.185"的話,就換這個IP,比較智能化,當初設計出來是為了在移客戶機器時,重啟後會自動執行“啟動”裡的這個指令碼,後來證明不成,因為客戶沒法登入,“啟動”裡的東西沒法運行所以也就沒法改IP了。
view plaincopy to clipboardprint?

    '/*=========================================================================  
    ' * Intro       通過 WMI 改變網卡的IP地址 vbs版  
    ' * FileName    ChangeIP.vbs  
    ' * Author      yongfa365  
    ' * Version     v2.0  
    ' * WEB         http://www.yongfa365.com  
    ' * Email       yongfa365@qq.com  
    ' * FirstWrite  http://www.yongfa365.com/item/Use-WMi-Change-IP-VBS-yongfa365.html  
    ' * LastModify  2007-12-31 14:57:50  
    ' *==========================================================================*/  
      
    strIPAddress = Array("66.66.66.7") '修改後的ip,多個IP可以以","格開,可以寫多個  
    strSubnetMask = Array("255.255.255.224") '子網路遮罩,配置同IP  
    strGateway = Array("66.66.66.1") '網關  
    arrDNSServers = Array("66.233.9.9")'DNS,可以寫多個  
      
    strComputer = "."  
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")  
    Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")  
    For Each objNetAdapter in colNetAdapters  
        sip = Join(objNetAdapter.IPAddress, ",")  
        if instr(sip,"116.90.185")<>0 then  
            strGatewayMetric = Array(1)  
            errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)  
            errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)  
            errDNS = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)  
        end if  
    Next  

這是第三個版本,遍曆所有網卡,如果這個網卡的IP裡包含"116.90.185" 並且 時間到了預先設定好的時間,就換這個IP,測試成功,唯一不足的地方是,在運行這個指令碼後不能重啟機器,重啟後這個指令碼就運行不了了。但總的來說這個是最適用的。
view plaincopy to clipboardprint?

    '/*=========================================================================     
    ' * Intro       通過 WMI 改變網卡的IP地址 vbs版     
    ' * FileName    ChangeIP.vbs     
    ' * Author      yongfa365     
    ' * Version     v1.0     
    ' * WEB         http://www.yongfa365.com     
    ' * Email       yongfa365@qq.com     
    ' * FirstWrite  http://www.yongfa365.com/item/Use-WMi-Change-IP-VBS-yongfa365.html     
    ' * LastModify  2007-1-3 14:57:50     
    ' *==========================================================================*/     
        
    strIPAddress = Array("66.66.66.7") '修改後的ip,多個IP可以以","格開,可以寫多個  
    strSubnetMask = Array("255.255.255.224") '子網路遮罩,配置同IP  
    strGateway = Array("66.66.66.1") '網關  
    arrDNSServers = Array("66.233.9.9")'DNS,可以寫多個  
      
    strComputer = "."    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")     
    Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")     
    For Each objNetAdapter in colNetAdapters    
        sip = Join(objNetAdapter.IPAddress, ",")  
        if instr(sip,"116.90.185")<>0 then  
            do while 1  
                if (now-#2008-1-3 23:00:00#) >0 then '關機時間  
                    strGatewayMetric = Array(1)     
                    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)     
                    errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)     
                    errDNS = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)     
                    exit do  
                else  
                    Wscript.Sleep 60*1000  
                end if  
             loop  
        end if  
    Next 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.