#NoTrayIcon
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=D:\Архив\ICONVISTA\ip.ico
#AutoIt3Wrapper_Res_Comment=Sergey
#AutoIt3Wrapper_Res_Description=Barbayanov
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=007
#AutoIt3Wrapper_Res_Language=1049
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <GuiIPAddress.au3>
#include <GuiComboBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Process.au3>
#include <Encoding.au3>
#region ### START Koda GUI section ### Form=смена ip.kxf
$Form1_1 = GUICreate("Change IP-address ", 207, 470, 444, 32)
$Combo1 = GUICtrlCreateCombo("", 8, 8, 185, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
$Combo2 = GUICtrlCreateCombo("", 8, 40, 185, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
GUICtrlSetState(-1, $GUI_DISABLE)
$Radio1 = GUICtrlCreateRadio("Получить IP и DNS сервера авто", 8, 98, 193, 17)
$Radio2 = GUICtrlCreateRadio("Использовать свои настройки", 8, 122, 185, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio3 = GUICtrlCreateRadio("Закладки", 8, 72, 177, 17)
$IPAddress1 = _GUICtrlIpAddress_Create($Form1_1, 42, 166, 113, 17)
$IPAddress2 = _GUICtrlIpAddress_Create($Form1_1, 42, 216, 113, 17)
$IPAddress3 = _GUICtrlIpAddress_Create($Form1_1, 42, 265, 113, 17)
$IPAddress4 = _GUICtrlIpAddress_Create($Form1_1, 42, 339, 113, 17)
$IPAddress5 = _GUICtrlIpAddress_Create($Form1_1, 42, 387, 113, 17)
$Group1 = GUICtrlCreateGroup("", 16, 136, 169, 162)
$Label1 = GUICtrlCreateLabel("IP адрес:", 26, 149, 50, 17)
$Label2 = GUICtrlCreateLabel("Маска подсети:", 26, 199, 84, 17)
$Label3 = GUICtrlCreateLabel("Основной шлюз:", 26, 248, 88, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlSetState(-1, $GUI_DISABLE)
$Group2 = GUICtrlCreateGroup("DNS сервера", 16, 301, 169, 113)
$Label4 = GUICtrlCreateLabel("Предпочитаемый DNS сервер", 26, 322, 157, 17)
$Label5 = GUICtrlCreateLabel("Альтернативный DNS сервер", 26, 370, 154, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button1 = GUICtrlCreateButton("Пуск", 32, 426, 137, 33)
#endregion ### END Koda GUI section ###
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
Dim $aAdapters[1][6] ; массив для хранения параметров сетевых адаптеров
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
For $objItem In $colItems ;перебор всех адаптеров
If $objItem.NetConnectionID <> '' Then
$aAdapters[UBound($aAdapters) - 1][0] = $objItem.NetConnectionID ;сохраняем имя адаптера
ReDim $aAdapters[UBound($aAdapters) + 1][6]
EndIf
Next
For $i = 0 To UBound($aAdapters) - 2;поиск сетевых настроек по каждому адаптеру
$sOut = ''
$hRun = Run('netsh interface ip show address name="' & $aAdapters[$i][0] & '"', '', @SW_HIDE, 6) ;получение ip, mask & gateway
Do
$sOut &= StdoutRead($hRun)
Until @error
$sOut = _Encoding_OEM2ANSI($sOut)
$aIpParams = StringRegExp($sOut, '((?:\d+\.){3}\d+)', 3)
If IsArray($aIpParams) And UBound($aIpParams) = 3 Then ;если адрес выдается не через DHCP, то заносим их в массив
For $j = 0 To UBound($aIpParams) - 1
$aAdapters[$i][$j + 1] = $aIpParams[$j]
Next
Else ;DHCP
For $j = 0 To UBound($aIpParams) - 1
$aAdapters[$i][$j + 1] = '0.0.0.0'
Next
EndIf
$sOut = ''
$hRun = Run('netsh interface ip show dns name="' & $aAdapters[$i][0] & '"', '', @SW_HIDE, 6) ;получение адресов DNS
Do
$sOut &= StdoutRead($hRun)
Until @error
$sOut = _Encoding_OEM2ANSI($sOut)
$aDNS = StringRegExp($sOut, '((?:\d+\.){3}\d+)', 3)
If IsArray($aDNS) Then ;если есть хотя бы один DNS-адрес, то заносим превые два из них в массив
$aAdapters[$i][4] = $aDNS[0]
If UBound($aDNS) > 1 Then
$aAdapters[$i][5] = $aDNS[1]
Else
$aAdapters[$i][5] = '0.0.0.0'
EndIf
Else
$aAdapters[$i][4] = '0.0.0.0'
$aAdapters[$i][5] = '0.0.0.0'
EndIf
Next
$sComboText = ''
For $i = 0 To UBound($aAdapters) - 2
If $sComboText <> '' Then $sComboText &= '|'
$sComboText &= $aAdapters[$i][0]
Next
GUICtrlSetData($Combo1, $sComboText, $aAdapters[0][0])
_fill_from_array($aAdapters[0][0])
$sPathINI = 'c:\config.ini'
$var = IniReadSectionNames($sPathINI)
$sComboText = ''
If IsArray($var) Then
For $i = 1 To $var[0]
If $sComboText <> '' Then $sComboText &= '|'
$sAdName = IniRead($sPathINI, $var[$i], 'name', 'default')
$sComboText &= $sAdName
$aAdapters[UBound($aAdapters) - 1][0] = $sAdName
$aAdapters[UBound($aAdapters) - 1][1] = IniRead($sPathINI, $var[$i], 'ip', '0.0.0.0')
$aAdapters[UBound($aAdapters) - 1][2] = IniRead($sPathINI, $var[$i], 'mask', '0.0.0.0')
$aAdapters[UBound($aAdapters) - 1][3] = IniRead($sPathINI, $var[$i], 'gateway', '0.0.0.0')
$aAdapters[UBound($aAdapters) - 1][4] = IniRead($sPathINI, $var[$i], 'dns1', '0.0.0.0')
$aAdapters[UBound($aAdapters) - 1][5] = IniRead($sPathINI, $var[$i], 'dns2', '0.0.0.0')
ReDim $aAdapters[UBound($aAdapters) + 1][6]
Next
GUICtrlSetData($Combo2, $sComboText, IniRead($sPathINI, $var[1], 'name', 'default'))
Else
GUICtrlSetData($Combo2, 'default', 'default')
$aAdapters[UBound($aAdapters) - 1][0] = 'default'
$aAdapters[UBound($aAdapters) - 1][1] = '0.0.0.0'
$aAdapters[UBound($aAdapters) - 1][2] = '0.0.0.0'
$aAdapters[UBound($aAdapters) - 1][3] = '0.0.0.0'
$aAdapters[UBound($aAdapters) - 1][4] = '0.0.0.0'
$aAdapters[UBound($aAdapters) - 1][5] = '0.0.0.0'
ReDim $aAdapters[UBound($aAdapters) + 1][6]
EndIf
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $Radio1
WinSetState($IPAddress1, '', @SW_DISABLE)
WinSetState($IPAddress2, '', @SW_DISABLE)
WinSetState($IPAddress3, '', @SW_DISABLE)
WinSetState($IPAddress4, '', @SW_DISABLE)
WinSetState($IPAddress5, '', @SW_DISABLE)
GUICtrlSetState($Combo2, $GUI_DISABLE)
Case $Radio2
WinSetState($IPAddress1, '', @SW_ENABLE)
WinSetState($IPAddress2, '', @SW_ENABLE)
WinSetState($IPAddress3, '', @SW_ENABLE)
WinSetState($IPAddress4, '', @SW_ENABLE)
WinSetState($IPAddress5, '', @SW_ENABLE)
GUICtrlSetState($Combo2, $GUI_DISABLE)
_ip_config_1()
Case $Radio3
_ip_config_1()
GUICtrlSetState($Combo2, $GUI_ENABLE)
Case $Combo2
_ip_config_1()
Case $Button1
GUICtrlSetState($Button1, $GUI_DISABLE)
$name_a = GUICtrlRead($Combo1)
If GUICtrlRead($Radio2) = $GUI_CHECKED Then
$name_a = GUICtrlRead($Combo1)
$ip_a = _GUICtrlIpAddress_Get($IPAddress1)
$mask_a = _GUICtrlIpAddress_Get($IPAddress2)
$gatewey_a = _GUICtrlIpAddress_Get($IPAddress3)
$dns_a = _GUICtrlIpAddress_Get($IPAddress4)
$dns2_a = _GUICtrlIpAddress_Get($IPAddress5)
$ip = "netsh interface ip set address name=""" & $name_a & """ source=static addr=" & $ip_a & " mask=" & $mask_a & " gateway=" & $gatewey_a & " gwmetric=1"
$dns = "netsh interface ip set dns name=""" & $name_a & """ source=static addr=" & $dns_a
$dns2 = "netsh interface ip add dns """ & $name_a & """ " & $dns2_a
_RunDos($ip)
_RunDos($dns)
_RunDos($dns2)
EndIf
If GUICtrlRead($Radio1) = $GUI_CHECKED Then
$ip = "netsh interface ip set address name=""" & $name_a & """ source=dhcp"
$dns = "netsh interface ip set dns name=""" & $name_a & """ source=dhcp"
_RunDos($ip)
_RunDos($dns)
EndIf
If GUICtrlRead($Radio3) = $GUI_CHECKED Then
$name_a = GUICtrlRead($Combo1)
$name_c = GUICtrlRead($Combo2)
$ip_a = _GUICtrlIpAddress_Get($IPAddress1)
$mask_a = _GUICtrlIpAddress_Get($IPAddress2)
$gatewey_a = _GUICtrlIpAddress_Get($IPAddress3)
$dns_a = _GUICtrlIpAddress_Get($IPAddress4)
$dns2_a = _GUICtrlIpAddress_Get($IPAddress5)
$ip = "netsh interface ip set address name=""" & $name_a & """ source=static addr=" & $ip_a & " mask=" & $mask_a & " gateway=" & $gatewey_a & " gwmetric=1"
$dns = "netsh interface ip set dns name=""" & $name_a & """ source=static addr=" & $dns_a
$dns2 = "netsh interface ip add dns """ & $name_a & """ " & $dns2_a
_RunDos($ip)
_RunDos($dns)
_RunDos($dns2)
EndIf
GUICtrlSetState($Button1, $GUI_ENABLE)
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func _ip_config_1()
WinSetState($IPAddress1, '', @SW_ENABLE)
WinSetState($IPAddress2, '', @SW_ENABLE)
WinSetState($IPAddress3, '', @SW_ENABLE)
WinSetState($IPAddress4, '', @SW_ENABLE)
WinSetState($IPAddress5, '', @SW_ENABLE)
If GUICtrlRead($Radio3) = $GUI_CHECKED Then
$name_c = GUICtrlRead($Combo2)
Else
$name_c = GUICtrlRead($Combo1)
EndIf
_fill_from_array($name_c)
EndFunc ;==>_ip_config_1
Func _fill_from_array($sSelectedText)
For $i = 0 To UBound($aAdapters) - 2
If $aAdapters[$i][0] = $sSelectedText Then
_GUICtrlIpAddress_Set($IPAddress1, $aAdapters[$i][1])
If $aAdapters[$i][1] = '0.0.0.0' Then WinSetState($IPAddress1, '', @SW_DISABLE)
_GUICtrlIpAddress_Set($IPAddress2, $aAdapters[$i][2])
If $aAdapters[$i][2] = '0.0.0.0' Then WinSetState($IPAddress2, '', @SW_DISABLE)
_GUICtrlIpAddress_Set($IPAddress3, $aAdapters[$i][3])
If $aAdapters[$i][3] = '0.0.0.0' Then WinSetState($IPAddress3, '', @SW_DISABLE)
_GUICtrlIpAddress_Set($IPAddress4, $aAdapters[$i][4])
If $aAdapters[$i][4] = '0.0.0.0' Then WinSetState($IPAddress4, '', @SW_DISABLE)
_GUICtrlIpAddress_Set($IPAddress5, $aAdapters[$i][5])
If $aAdapters[$i][5] = '0.0.0.0' Then WinSetState($IPAddress5, '', @SW_DISABLE)
ExitLoop
EndIf
Next
EndFunc ;==>_fill_from_array