• 欢迎访问奇跡の海网站,本站不上传任何资源,所有资源均来自于网络,欢迎加入奇跡の海~!奇跡の海-WordPress QQ群
  • 本站下载资源为网络上收集整理而来,并且以计算机技术研究交流为目的,版权归原作者所有,仅供大家参考,学习,不存在任何商业目的与商业用途.
  • 本站系统镜像均来自于官方原版,ed2k可视为P2P下载链接。所有操作系统默认均为试用版,如有正版密钥可以有效激活,本站不提供任何激活和相关服务。

AU3源码-备份、恢复网卡静态IP地址工具

AutoIT3 奇跡の海 4个月前 (05-18) 160次浏览 已收录 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址