• 歡迎訪問奇跡の海網站,本站不上傳任何資源,所有資源均來自于網絡,歡迎加入奇跡の海~!奇跡の海-WordPress QQ群
  • 本站下載資源為網絡上收集整理而來,并且以計算機技術研究交流為目的,版權歸原作者所有,僅供大家參考,學習,不存在任何商業目的與商業用途.
  • 本站系統鏡像均來自于官方原版,ed2k可視為P2P下載鏈接。所有操作系統默認均為試用版,如有正版密鑰可以有效激活,本站不提供任何激活和相關服務。

AU3源碼-備份、恢復網卡靜態IP地址工具

AutoIT3 奇跡の海 5個月前 (05-18) 202次瀏覽 已收錄 0個評論 掃描二維碼

簡介

AU3源碼-備份、恢復網卡靜態IP地址工具
#PRE_Res_Comment=備份、恢復網卡靜態IP地址工具
#PRE_Res_Description=必須插網線,靜默參數 /B 備份,/R 恢復,通過MAC地址比對網卡,必須取消偽裝MAC地址

源碼

#Region ;**** 參數創建于 ACNWrapper_GUI ****
#PRE_Compression=3
#PRE_UseX64=n
#PRE_Icon=Network.ICO
#PRE_Res_Comment=備份、恢復網卡靜態IP地址工具
#PRE_Res_Description=必須插網線,靜默參數 /B 備份,/R 恢復,通過MAC地址比對網卡,必須取消偽裝MAC地址
#PRE_Res_Fileversion=1.2019.4.2
#[email protected]
#PRE_Res_SaveSource=y
#PRE_Res_requestedExecutionLevel=requireAdministrator
#PRE_Run_Obfuscator=y
#Obfuscator_Parameters=/cs=1 /cn=1 /cf=1 /sf=1 /sv=1 /striponlyincludes
#EndRegion ;**** 參數創建于 ACNWrapper_GUI ****
;#AutoIt3Wrapper_run_debug_mode=Y
Opt("GUICloseOnESC",0)
;Opt("TrayIconHide",1)
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $Form1

If $CmdLine[0] <> 0 and $CmdLine[1] = "/B" Then BackUP();備份網卡信息
If $CmdLine[0] <> 0 and $CmdLine[1] = "/R" Then ReStore();恢復網卡信息

If $CmdLine[0] = 0 Then

	#Region ### START Koda GUI section ### Form=
	$Form1 = GUICreate("備份、恢復網卡靜態IP地址", 312, 166)

	Select ;根據操作系統顯示不同字體
		Case StringInstr(@OSVersion, "WIN_2003") or StringInstr(@OSVersion, "WIN_XP")
			GUISetFont(8.5, 400, 0, "Tahoma")
		Case Else
			GUISetFont(9, 400, 0, "微軟雅黑")
	EndSelect

	$Radio_Backup = GUICtrlCreateRadio(" 備份  網卡配置的靜態IP地址", 48, 24, 233, 33) ;$Radio,GUICtrlRead選中1,未選中4
	$Radio_Restore = GUICtrlCreateRadio(" 恢復  網卡配置的靜態IP地址", 48, 56, 233, 33)
	If FileExists(@ScriptDir & "\BACKUP\Network") Then
		GUICtrlSetState($Radio_Restore,$GUI_CHECKED)
	Else
		GUICtrlSetState($Radio_Backup,$GUI_CHECKED)
	EndIf

	$Button_OK = GUICtrlCreateButton("確定", 115, 104, 70, 33)
	GUISetState(@SW_SHOW)
	#EndRegion ### END Koda GUI section ###

	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit
			Case $Button_OK
				GUICtrlSetState($Button_OK,$GUI_DISABLE)
				GUICtrlSetData($Button_OK, "操作中..." )
				If GUICtrlRead($Radio_Backup)=1 Then BackUP();備份網卡信息
				If GUICtrlRead($Radio_Restore)=1 Then ReStore();恢復網卡信息
				GUICtrlSetData($Button_OK, "退出中..." )
				Sleep(1000)
				ExitLoop
		EndSwitch
	WEnd

EndIf


Func BackUP();備份網卡信息

	DirRemove(@ScriptDir & "\BACKUP\Network", 1)

	Local $NetConnectionID, $NetworkAdapterName, $NetEnabled, $NetworkAdapterMac, $DHCPEnabled

	Local $NetworkAdapterCount=1 ;網卡計數

	$objWMIService = objget("winmgmts:\\127.0.0.1\root\CIMV2")

	$NetworkAdapter = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where PNPDeviceID like ""PCI\\%"" or PNPDeviceID like ""USB\\%""") ;查詢PNPDeviceID 以PCI\,USB\開頭的網卡,即查找物理網卡,返回值參看 https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-networkadapter
	For $object in $NetworkAdapter

		$NetConnectionID = $object.NetConnectionID ;網絡鏈接ID、名字,即以太網、WLAN、本地連接之類的網卡名字,返回字符串
		$NetworkAdapterName=$object.Name ;網卡描述、名字,返回字符串
		$NetworkAdapterIndex = $object.Index ;網卡索引號,返回無符號整型32位
		$NetEnabled=$object.NetEnabled ;網絡是否已連接,返回布爾值

		ConsoleWrite( @CRLF & "---------------- Win32_NetworkAdapter " & $NetworkAdapterCount & " info ----------------" & @CRLF & @CRLF & "$NetConnectionID=" & $NetConnectionID & @CRLF & "$NetworkAdapterName=" & $NetworkAdapterName & @CRLF & "$NetworkAdapterIndex=" & $NetworkAdapterIndex & @CRLF & "$NetEnabled=" & $NetEnabled & @CRLF)

		Local $IPAddressArray,$IPSubnetArray,$DefaultIPGatewayArray, $GatewayCostMetricArray, $DNSServerArray
		$NetworkAdapterConfiguration= $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where Index=" & $NetworkAdapterIndex)
		For $object in $NetworkAdapterConfiguration
			$NetworkAdapterMac=$object.MacAddress ;MAC地址,返回字符串
			$DHCPEnabled=$object.DHCPEnabled ;是否啟用DHCP,返回布爾值

			If $NetEnabled=False and $DHCPEnabled=False Then ;如果是靜態IP地址且沒有插網線
				$IfGoOn=MsgBox(4+32+262144, "問題", "本地網卡:" & $NetConnectionID & "  " & $NetworkAdapterName & @LF & "已配置為固定 IP,在斷網情況下無法獲得配置的IP信息!" & @LF & @LF & "請連接網絡后重試!是否要重試?",-1,$Form1)
				If $IfGoOn=6 Then ;如果選擇是
					Run(@ComSpec&" /c ping 127.0.0.1 -n 5&""" & @ScriptName & """ /B",@ScriptDir, @SW_HIDE) ;退出后再次運行
					Exit
				EndIf
			EndIf

			If $DHCPEnabled=False and $NetEnabled=True Then ;如果是非DHCP,即靜態IP地址,且網絡已經連接,則開始備份IP地址

				If not FileExists(@ScriptDir & "\BACKUP\Network") Then DirCreate(@ScriptDir & "\BACKUP\Network")

				IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","NetworkAdapterName",$NetworkAdapterName)
				IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","NetConnectionID",$NetConnectionID)
				IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","NetEnabled",$NetEnabled)
				IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","MacAddress",$NetworkAdapterMac)

				ConsoleWrite( @CRLF & "---------------- Win32_NetworkAdapter " & $NetworkAdapterCount & " Configuration ----------------" & @CRLF & @CRLF & "$NetworkAdapterName=" & $NetworkAdapterName & @CRLF & "$NetworkAdapterIndex=" & $NetworkAdapterIndex & @CRLF & "$NetEnabled=" & $NetEnabled & @CRLF & "$DHCPEnabled=" & $DHCPEnabled & @CRLF)

				$IPAddressArray=$object.IPAddress ;網卡IP地址,返回字符串數組
				If IsArray($IPAddressArray) Then
					$ArrayCount = UBound($IPAddressArray) -2
					If UBound($IPAddressArray) -2 < 0 Then $ArrayCount = UBound($IPAddressArray) -1
					For $i=0 to $ArrayCount
						ConsoleWrite( @CRLF & "---------------- Win32_NetworkAdapter " & $NetworkAdapterCount & " Configuration DefaultIPGateway ----------------" & @CRLF & @CRLF & "$NetworkAdapterName=" & $NetworkAdapterName & @CRLF & "IP"& $i+1 & "地址=" & $IPAddressArray[$i] & @CRLF)
						IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","IPAddress" & $i+1,$IPAddressArray[$i])
					Next
				EndIf

				$IPSubnetArray=$object.IPSubnet ;網卡IP掩碼,返回字符串數組
				If IsArray($IPSubnetArray) Then
					$ArrayCount = UBound($IPSubnetArray) -2
					If UBound($IPSubnetArray) -2 < 0 Then $ArrayCount = UBound($IPSubnetArray) -1
					For $i=0 to $ArrayCount
						ConsoleWrite( @CRLF & "---------------- Win32_NetworkAdapter " & $NetworkAdapterCount & " Configuration DefaultIPGateway ----------------" & @CRLF & @CRLF & "$NetworkAdapterName=" & $NetworkAdapterName & @CRLF & "IP"& $i+1 & "掩碼=" & $IPSubnetArray[$i] & @CRLF)
						IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","IPSubnet" & $i+1,$IPSubnetArray[$i])
					Next
				EndIf

				$DefaultIPGatewayArray=$object.DefaultIPGateway ;網卡IP默認網關,返回字符串數組
				If IsArray($DefaultIPGatewayArray) Then
					For $i=0 to UBound($DefaultIPGatewayArray) -1
						ConsoleWrite( @CRLF & "---------------- Win32_NetworkAdapter " & $NetworkAdapterCount & " Configuration DefaultIPGateway ----------------" & @CRLF & @CRLF & "$NetworkAdapterName=" & $NetworkAdapterName & @CRLF & "IP"& $i+1 & "默認網關=" & $DefaultIPGatewayArray[$i] & @CRLF)
						IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","DefaultIPGateway" & $i+1,$DefaultIPGatewayArray[$i])
					Next
				EndIf

				$GatewayCostMetricArray=$object.GatewayCostMetric ;網卡IP GatewayCostMetric,返回字符串數組
				If IsArray($GatewayCostMetricArray) Then
					For $i=0 to UBound($GatewayCostMetricArray) -1
						ConsoleWrite( @CRLF & "---------------- Win32_NetworkAdapter " & $NetworkAdapterCount & " Configuration GatewayCostMetric ----------------" & @CRLF & @CRLF & "$NetworkAdapterName=" & $NetworkAdapterName & @CRLF & "IP"& $i+1 & " GatewayCostMetric=" & $GatewayCostMetricArray[$i] & @CRLF)
						IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","GatewayCostMetric" & $i+1,$GatewayCostMetricArray[$i])
					Next
				EndIf

				$DNSServerArray=$object.DNSServerSearchOrder ;網卡DNS,返回字符串數組
				If IsArray($DNSServerArray) Then
					For $i=0 to UBound($DNSServerArray) -1
						ConsoleWrite( @CRLF & "---------------- Win32_NetworkAdapter " & $NetworkAdapterCount & " Configuration DNSServer ----------------" & @CRLF & @CRLF & "$NetworkAdapterName=" & $NetworkAdapterName & @CRLF & "DNSServer"& $i+1 & "=" & $DNSServerArray[$i] & @CRLF)
						IniWrite(@ScriptDir & "\BACKUP\Network\NetworkAdapter_" & $NetworkAdapterIndex & ".ini","Network","DNSServer" & $i+1,$DNSServerArray[$i])
					Next
				EndIf
			EndIf

			$NetworkAdapterCount+=1

		Next
	Next

EndFunc


Func ReStore();恢復網卡信息

	Local $hSearch = FileFindFirstFile(@ScriptDir & "\BACKUP\Network\*.ini")

	If $hSearch = -1 Then
		Exit
	EndIf

	Local $NetworkAdapterCount=1 ;網卡計數

	While 1
		Local $sFile = FileFindNextFile($hSearch)
		If @error Then ExitLoop
		[email protected] & "\BACKUP\Network\" & $sFile

		Local $NetworkAdapterMac=IniRead($sFileWithPath,"Network","MacAddress","")

		Local $NetConnectionID
		$objWMIService = objget("winmgmts:\\127.0.0.1\root\CIMV2")
		$NetworkAdapter = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where MACAddress=""" & $NetworkAdapterMac & """ and (PNPDeviceID like ""PCI\\%"" or PNPDeviceID like ""USB\\%"")")
		For $object in $NetworkAdapter
			$NetConnectionID = $object.NetConnectionID ;網絡鏈接ID、名字,即以太網、WLAN、本地連接之類的網卡名字,且PNPDeviceID 以PCI\,USB\開頭的物理網卡,返回字符串
		Next
		ConsoleWrite( @CRLF & "---------------- MAC 地址 " & $NetworkAdapterMac & " 的網卡" & $NetworkAdapterCount & " ----------------" & @CRLF & @CRLF & "$NetConnectionID=" & $NetConnectionID & @CRLF)

		$i=1 ;還原IP
		While 1
			If IniRead($sFileWithPath,"Network","IPAddress" & $i,"")="" Then ExitLoop
			Local $IPAddress=IniRead($sFileWithPath,"Network","IPAddress" & $i,"")
			Local $IPSubnet=IniRead($sFileWithPath,"Network","IPSubnet" & $i,"")
			Local $DefaultIPGateway=IniRead($sFileWithPath,"Network","DefaultIPGateway" & $i,"")
			Local $GatewayCostMetric=IniRead($sFileWithPath,"Network","GatewayCostMetric" & $i,"")
			Local $GatewayCostMetricCMD=""
			If $GatewayCostMetric<>"" Then $GatewayCostMetricCMD=" gwmetric=" & $GatewayCostMetric

			If $i=1 Then ;還原主IP

				If $IPAddress<>"" and $IPSubnet<>"" Then ;還原IP地址
					ConsoleWrite( @CRLF & "---------------- MAC 地址 " & $NetworkAdapterMac & " 的網卡" & $NetworkAdapterCount & " 執行還原 IP " & $i & " ----------------" & @CRLF & @CRLF & "netsh.exe interface ip set address name=""" & $NetConnectionID & """ source=static addr=" & $IPAddress & " mask=" & $IPSubnet & " gateway=" & $DefaultIPGateway & $GatewayCostMetricCMD & @CRLF)
					RunWait(@SystemDir & "\netsh.exe interface ip set address name=""" & $NetConnectionID & """ source=static addr=" & $IPAddress & " mask=" & $IPSubnet & " gateway=" & $DefaultIPGateway & $GatewayCostMetricCMD, @SystemDir, @SW_HIDE)
				EndIf

			Else ;不等于1就是還原次IP

				If $IPAddress<>"" and $IPSubnet<>"" Then ;還原次IP
					ConsoleWrite( @CRLF & "---------------- MAC 地址 " & $NetworkAdapterMac & " 的網卡" & $NetworkAdapterCount & " 執行還原次 IP " & $i & " ----------------" & @CRLF & @CRLF & "netsh.exe interface ip add address """ & $NetConnectionID & """ " & $IPAddress & " " & $IPSubnet & @CRLF)
					RunWait(@SystemDir & "\netsh.exe interface ip add address """ & $NetConnectionID & """ " & $IPAddress & " " & $IPSubnet, @SystemDir, @SW_HIDE)
				EndIf

				If $DefaultIPGateway<>"" Then ;還原次IP網關
					ConsoleWrite( @CRLF & "---------------- MAC 地址 " & $NetworkAdapterMac & " 的網卡" & $NetworkAdapterCount & " 執行還原次 IP " & $i & " 網關 ----------------" & @CRLF & @CRLF & "netsh.exe interface ip add address """ & $NetConnectionID & """ gateway=" & $DefaultIPGateway & $GatewayCostMetricCMD & @CRLF)
					RunWait(@SystemDir & "\netsh.exe interface ip add address """ & $NetConnectionID & """ gateway=" & $DefaultIPGateway & $GatewayCostMetricCMD, @SystemDir, @SW_HIDE)
				EndIf

			EndIf
			$i+=1
		WEnd

		$i=1 ;還原DNS
		While 1
			If IniRead($sFileWithPath,"Network","DNSServer" & $i,"")="" Then ExitLoop
			Local $DNSServer=IniRead($sFileWithPath,"Network","DNSServer" & $i,"")

			If $i=1 Then ;還原主DNS

				If $DNSServer<>"" Then ;還原主DNS
					ConsoleWrite( @CRLF & "---------------- MAC 地址 " & $NetworkAdapterMac & " 的網卡" & $NetworkAdapterCount & " 執行還原主DNS ----------------" & @CRLF & @CRLF & "netsh.exe interface ip set dns name=""" & $NetConnectionID & """ source=static addr=" & $DNSServer & " register=primary" & @CRLF)
					RunWait(@SystemDir & "\netsh.exe interface ip set dns name=""" & $NetConnectionID & """ source=static addr=" & $DNSServer & " register=primary", @SystemDir, @SW_HIDE)
				EndIf

			Else ;不等于1就是次DNS

				If $DNSServer<>"" Then ;還原次DNS
					ConsoleWrite( @CRLF & "---------------- MAC 地址 " & $NetworkAdapterMac & " 的網卡" & $NetworkAdapterCount & " 執行還原備用DNS ----------------" & @CRLF & @CRLF & "netsh.exe interface ip add dns name=""" & $NetConnectionID & """ addr=" & $DNSServer & @CRLF)
					RunWait(@SystemDir & "\netsh.exe interface ip add dns name=""" & $NetConnectionID & """ addr=" & $DNSServer, @SystemDir, @SW_HIDE)
				EndIf

			EndIf
			$i+=1
		WEnd

		$NetworkAdapterCount+=1

	WEnd

	FileClose($hSearch)

EndFunc

版權聲明:本站所有文章和資源使用CC BY-NC-SA 4.0協議授權發布 , 轉載應當以相同方式注明文章來自“SeaOMC.COM->AU3源碼-備份、恢復網卡靜態IP地址工具!在下邊可以分享本文哦!
喜歡 (0)
[]
分享 (0)
奇跡の海
關于作者:
一個WordPress菜鳥!
發表我的評論
取消評論

表情 貼圖 加粗 刪除線 居中 斜體 簽到

Hi,您需要填寫昵稱和郵箱!

  • 昵稱 (必填)
  • 郵箱 (必填)
  • 網址
中国福利彩票36选7开奖结果