#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <FTPEx.au3>
Opt("GUIOnEventMode", 1)
Const $user = "root", $passwd = ""
Global $host = "", $socket = "", $port = 23 ;Default port for Telnet
Global $filename = "", $isRunning = 0
Dim $width = 398, $height = 486
#Region ### START Koda GUI section ###
$Form1 = GUICreate("Firmware Script Proxy", $width, $height, (@DesktopWidth - $width) / 2, (@DesktopHeight - $height) / 2, $WS_CAPTION)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 377, 385, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER))
GUICtrlSetData(-1, '1. Make sure that device is connected and powered on.' & @CRLF _
& '2. Select a script file on local computer.' & @CRLF _
& '3. Click "Execute" button to upload it to device and run remotely.')
$Edit2 = GUICtrlCreateEdit("", 16, 407, 281, 24, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL))
$Button1 = GUICtrlCreateButton("Browse...", 312, 405, 65, 28)
GUICtrlSetOnEvent(-1, "OpenFile")
$Button2 = GUICtrlCreateButton("Execute", 16, 442, 161, 33, $WS_GROUP)
GUICtrlSetOnEvent(-1, "ExecuteScript")
$Button3 = GUICtrlCreateButton("Terminate", 209, 442, 73, 33, $WS_GROUP)
GUICtrlSetOnEvent(-1, "TerminateScript")
GUICtrlSetState(-1, $GUI_DISABLE)
$Button4 = GUICtrlCreateButton("Exit", 307, 442, 73, 33)
GUICtrlSetOnEvent(-1, "ExitEvent")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
Sleep(10)
WEnd
Func OpenFile()
$var = FileOpenDialog("Open", "", "Shell Scripts (*.*)", 1, '', $Form1)
If @error Then
;MsgBox(4096, "Warning", "No File is chosen!")
Else
;MsgBox(4096, "", $var)
GUICtrlSetData($Edit2, $var)
EndIf
EndFunc ;==>OpenFile
Func ExecuteScript()
control_state_running_script()
;Check if a script file is selected
$filepath = GUICtrlRead($Edit2)
If $filepath = "" Then
MsgBox(48, "Error", "Please select a file first.", Default, $Form1)
control_state_not_running_script()
Return 1
Else
$filename = StringTrimLeft($filepath, StringInStr($filepath, "", 1, -1))
EndIf
;Check device connection
GUICtrlSetData($Edit1, "Checking connection ...... ")
$host = get_ip()
If $host = "" Then
MsgBox(48, "Error", "device connection not detected!", Default, $Form1)
GUICtrlSetData($Edit1, "Not detected!" & @CRLF, -1)
Return 1
EndIf
GUICtrlSetData($Edit1, "OK" & @CRLF, -1)
;Upload the selected script file to device
GUICtrlSetData($Edit1, "Uploading file to device ...... ", -1)
If ftp_put_file($filepath, "/taurus/" & $filename) Then
MsgBox(48, "Error", "Upload file to device failed!" & @CRLF)
GUICtrlSetData($Edit1, "Fail!" & @CRLF, -1)
control_state_not_running_script()
Return 1
EndIf
GUICtrlSetData($Edit1, "OK" & @CRLF & @CRLF, -1)
GUICtrlSetData($Edit1, "===================== Start Telnet Session =====================" & @CRLF & @CRLF, -1)
TCPStartup()
$socket = TCPConnect($host, $port)
If $socket = -1 Then
MsgBox(48, "Error", "Connection fail!")
TCPShutdown()
control_state_not_running_script()
Return 1
EndIf
$isRunning = 1
$ret = exec_script()
TCPCloseSocket($socket)
TCPShutdown()
$isRunning = 0
control_state_not_running_script()
Return 0
EndFunc ;==>ExecuteFile
Func TerminateScript()
;~ ;Open another telnet session
;~ $sk = TCPConnect($host, $port)
;~ If $sk = -1 Then
;~ MsgBox(48, "Error", "Connection fail!", Default, $Form1)
;~ Return 1
;~ EndIf
;~ TCPSend($sk, 'kill -9 "/bin/sh /taurus/' & $filename & '"' & @CRLF)
;~ ;TCPSend($sk, 'kill -9 $(ps|grep "' & $filename & '"' & "|grep -v grep|awk '{print $1}')" & @CRLF)
;~ wait_cmd_prompt($sk, "#")
;~ TCPCloseSocket($socket)
;~ TCPCloseSocket($sk)
;~ TCPShutdown()
;Terminate the session and the running script will also be terminated
TCPCloseSocket($socket)
TCPShutdown()
$isRunning = 0
control_state_not_running_script()
Return 0
EndFunc ;==>ExecuteFile
Func ExitEvent()
If $isRunning Then
TerminateScript()
EndIf
Exit 0
EndFunc ;==>ExitEvent
;==================================================================
; Description: Give executable property to the script and execute.
; Return value: Result of the execution.
; 0 - Successful
; 1 - Failed
;==================================================================
Func exec_script()
$delay = 300 ; compensates network/internet delays
Sleep($delay)
wait_cmd_prompt($socket, "login")
TCPSend($socket, $user & @CRLF)
Sleep($delay)
If wait_cmd_prompt($socket, "#") Then Return 1
TCPSend($socket, "chmod a+x /taurus/" & $filename & @CRLF)
wait_cmd_prompt($socket, "#")
TCPSend($socket, "/taurus/" & $filename & @CRLF)
wait_cmd_prompt($socket, "#")
;WinSetState($Form1, "", @SW_HIDE)
Return 0
EndFunc
;==================================================================
; Description: Get IP address of device.
; Return value: A string of IP address.
;==================================================================
Func get_ip()
Local $oIP = ""
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL")
For $objItem In $colItems
If StringInStr($objItem.Description, "Taurus") > 0 And $objItem.IPAddress <> "" Then
$oIP = $objItem.IPAddress(0)
ExitLoop
EndIf
Next
If $oIP <> "" Then
$oIP = StringTrimRight($oIP, 2) & "11" ; Convert PC IP to device IP
EndIf
;MsgBox(0, "IP Address", $oIP)
Return $oIP
EndFunc
;==================================================================
; Description: Wait for desired output before sending.
; Return value: 0 - Successful
; 1 - Failed
;==================================================================
Func wait_cmd_prompt($sk, $waitStr)
Local $ack, $count=0
If $waitStr = "#" Then
Do
$ack = TCPRecv($sk, 300)
GUICtrlSetData($Edit1, $ack, -1)
$ack = StringStripWS($ack, 2)
$count += 1
Sleep(300)
Until StringRight($ack, 1) = "#"
ElseIf $waitStr = "login" Then
Do
$ack = TCPRecv($sk, 300)
GUICtrlSetData($Edit1, $ack, -1)
$count += 1
If $count = 5 Then
MsgBox(48, "Error", 'Wait for prompt "' & $waitStr & '" timeout!' & @CRLF & "Please retry!")
Return 1
EndIf
Sleep(300)
Until StringInStr($ack, $waitStr) > 0
EndIf
Return 0
EndFunc
;==================================================================
; Description: Upload the selected shell script to device.
; Return value: 0 - Successful
; 1 - Failed
;==================================================================
Func ftp_put_file($srcFilePath, $DesFilePath)
$Open = _FTP_Open('myFTP')
$ftpConn = _FTP_Connect($Open, $host, $user, $passwd)
_FTP_FilePut($ftpConn, $srcFilePath, $DesFilePath)
If @error <> 0 Then
_FTP_Close($Open)
Return 1
EndIf
_FTP_Close($Open)
Return 0
EndFunc
Func control_state_running_script()
GUICtrlSetState($Edit2, $GUI_DISABLE)
GUICtrlSetState($Button1, $GUI_DISABLE)
GUICtrlSetState($Button2, $GUI_DISABLE)
GUICtrlSetState($Button3, $GUI_ENABLE)
EndFunc
Func control_state_not_running_script()
GUICtrlSetState($Edit2, $GUI_ENABLE)
GUICtrlSetState($Button1, $GUI_ENABLE)
GUICtrlSetState($Button2, $GUI_ENABLE)
If BitAND(GUICtrlGetState($Button3), $GUI_ENABLE) = $GUI_ENABLE Then GUICtrlSetState($Button3, $GUI_DISABLE)
EndFunc
1)如何让脚本正在运行的时候,该工具也能响应“Terminate”与“Exit”按钮?