自動修復網絡腳本(Auto IT)

適用於網絡棧出現故障,不能正常顯示IP地址。

#cs ---------------------------------------------------------------------------------------
FileName:
	NetworkDiagnosis.au3
Description:
	This tool is used to disable and enable network device to recover IP address.
	
	This scripts use a external program: devcon.exe, which is a Windows Device manange
	tookit. More information about this tool, you can retrieve from web.
	
	Use below commands to implement function of network diagnosis:
	devcon status *
		Display all device info, find out device instance by device name.
	devcon disable @device instance
		Disable this device
	devcon enable @device instance
		Enable this device
		
	The device name is the name of device which not has a IP address, maybe cause of
	network stack issues. You can use windows command: ipconfig to get all network
	device. And compare to output of command devcon status *. If you can find a device
	in the latter command output, but can not find in former command out, that is the
	device need to disable and enable.	
Log:
	 2011-07-14		sulliy		Initiate
#ce ---------------------------------------------------------------------------------------

Local Const $Work_Path = "D:\Tools\"
Local Const $Devcon = "devcon.exe"
Local Const $Device_List_File = "devicelist.txt"
Local Const $Ipconfig_File = "ipconfig.txt"

Local Const $Name_Pattern = "";Regular expression for network device name
Local Const $Device_Instance_Pattern = "USB\\\w*"

Main()

Func Main()
	EmunNetworkDevice()
	Ipconfig()
	Dim $Device_Array = AnalyseIpconfigFile()
	Dim $Device_Instance_Array = AnalyseDeviceListFile()
	
	Dim $Ops = True
	
	For $i = 0 To UBound($Device_Array) - 1 Step 1
		If($Device_Instance_Array[$i][0] == "")Then ExitLoop
		For $j = 0 To UBound($Device_Array) - 1 Step 1
			If($Device_Instance_Array[$i][0] == $Device_Array[$j])Then $Ops = False
		Next
		If($Ops)Then DiagnosisDeviceByInstance($Device_Instance_Array[$i][1])
	Next
EndFunc

Func AnalyseDeviceListFile()
	Dim $handle = FileOpen($Device_List_File, 0)
	Dim $line1 = ""
	Dim $line2 = ""
	Dim $Device_Instance_Array[6][2]
	Dim $Array_Index = 0
	Dim $File_Line = 1
	While True
		$line1 = FileReadLine($handle, $File_Line)
		If(-1 == @error) Then ExitLoop
		$line2 = FileReadLine($handle, $File_Line + 1)
		If(-1 == @error) Then ExitLoop
		
		Dim $ret1 = StringRegExp($line1, $Device_Instance_Pattern, 1)
		Dim $ret2 = StringRegExp($line2, $Name_Pattern, 1)
		If(IsArray($ret1) And IsArray($ret2))Then
			$Device_Instance_Array[$Array_Index][0] = $ret2[0]
			$Device_Instance_Array[$Array_Index][1] = $line1
			$Array_Index += 1
		EndIf
		$File_Line += 1
	WEnd
	For $i = 0 To UBound($Device_Instance_Array) - 1 Step 1
		ConsoleWrite("NetworkDiagnosis.au3" & @TAB & "AnalyseDeviceListFile" & @TAB & "Device: " & $Device_Instance_Array[$i][0] & @TAB & "Line:" & @ScriptLineNumber & @CRLF)
		ConsoleWrite("NetworkDiagnosis.au3" & @TAB & "AnalyseDeviceListFile" & @TAB & "Instance: " & $Device_Instance_Array[$i][1] & @TAB & "Line:" & @ScriptLineNumber & @CRLF)
	Next
	FileClose($handle)
	Return $Device_Instance_Array
EndFunc

Func AnalyseIpconfigFile()
	Dim $handle = FileOpen($Ipconfig_File, 0)
	Dim $line = ""
	Dim $Device_Array[6]
	Dim $Array_Index = 0
	While True
		$line = FileReadLine($handle)
		If(-1 == @error) Then ExitLoop
		Dim $ret = StringRegExp($line, $Name_Pattern, 1)
		If(IsArray($ret))Then
			$Device_Array[$Array_Index] = $ret[0]
			$Array_Index += 1
		EndIf
	WEnd
	For $i = 0 To UBound($Device_Array) - 1 Step 1
		ConsoleWrite("NetworkDiagnosis.au3" & @TAB & "AnalyseIpconfigFile" & @TAB & "Device: " & $Device_Array[$i] & @TAB & "Line:" & @ScriptLineNumber & @CRLF)
	Next
	FileClose($handle)
	Return $Device_Array
EndFunc

Func DiagnosisDeviceByInstance($Instance)
	ConsoleWrite($Instance & @CRLF)
	
	RunWait(@ComSpec & ' /c ' & $Work_Path & $Devcon & ' disable "@' & $Instance & '"', $Work_Path)
	ConsoleWrite("NetworkDiagnosis.au3" & @TAB & "DiagnosisDeviceByInstance" & @TAB & "disable!" & $Instance & @TAB & "Line:" & @ScriptLineNumber & @CRLF)
	RunWait(@ComSpec & ' /c ' & $Work_Path & $Devcon & ' enable "@' & $Instance & '"', $Work_Path)
	ConsoleWrite("NetworkDiagnosis.au3" & @TAB & "DiagnosisDeviceByInstance" & @TAB & "enable!" & $Instance & @TAB & "Line:" & @ScriptLineNumber & @CRLF)
	
EndFunc

Func EmunNetworkDevice()
	RunWait(@ComSpec & ' /c ' & $Work_Path & $Devcon & " status * > " & $Device_List_File, $Work_Path)
	ConsoleWrite("NetworkDiagnosis.au3" & @TAB & "EmunNetworkDevice" & @TAB & "finish!" & @TAB & "Line:" & @ScriptLineNumber & @CRLF)
EndFunc

Func Ipconfig()
	RunWait(@ComSpec & ' /c ipconfig /all > ' & $Ipconfig_File, $Work_Path)
	ConsoleWrite("NetworkDiagnosis.au3" & @TAB & "Ipconfig" & @TAB & "finish!" & @TAB & "Line:" & @ScriptLineNumber & @CRLF)
EndFunc


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章