Jump to content

Help with adding array


grimmlock
 Share

Recommended Posts

I can get most of this to work, however I am at the last few steps of finalizing my code and need some help. I have a mysql database with tons of data that is refreshed every night. The things I need help with is to get the data from mysql to the list box, seperating each name to a new line. Then what I am trying to accomplish is, I want to be able to highlight the computer name and have the buttons perform their action.

Example: If I choose branch 1 from the drop down list, and it pulls all the computers (8 of them) from mysql and displays them in the list box one pc per line. Then if I click Ping it scrolls through the list and returns the output of the ping into the read-only display boxes. Then if I stop the ping and select computers 2, 4, and 7 from the list box, I want to be able to get the IP addresses of those PC's (using mysql) and send the IP

to a seperate program. Lastly I would like to be able to select computers 1, 3, and 8 (if they are not reponding) and get thier IPs and their MAC addresses (from mysql) and send that data to the Wake On Lan Function.

Here is my code:

#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <WinAPI.au3>
#include <GUIComboBox.au3>
#Include <String.au3>
#include <mysql.au3>


; Declare a flag
Global $fPing = False
Global $Input1 = False
Global $Input2 = False


GUICreate("Am I Asleep?", 315, 850)


GUISetState()

;~ IP Addresses
$List1 = FileRead("c:\dhcp2.txt") ; Branch1
$List2 = FileRead("\server.test1.local\Installers\dhcp2.txt") ; Branch2
$List3 = ("") ; Branch3
$List4 = ("") ; Branch4
$List5 = ("") ; Branch5
$List6 = ("") ; Branch6
$List7 = ("") ; Branch7
$List8 = ("") ; Branch8
$List9 = ("") ; Branch9
$List10 = ("") ;Branch10
$List11 = ("") ; Branch11
$List12 = ("") ; Branch12
$List13 = ("") ; Branch13
$List14 = ("") ; Branch14
$List15 = ("") ; Branch15
$List16 = ("") ; Branch16
$List17 = ("") ; Branch17
$List18 = FileRead("C:\SC_PC.txt") ; Branch18
$List19 = ("") Branch Servers
$List20 = ("") ; Core Servers

Local $Combo1 = GUICtrlCreateCombo("", 10, 20, 125, 10)
GUICtrlSetData(-1, "Branch1|Branch2|Branch3|Branch4|Branch5|Branch6|Branch7|Branch8|Branch9|Branch10|Branch11|Branch12|Branch13|Branch14|Branch15|Branch16|Branch17|Branch18|Branch Servers|Core Servers")

; Set Cuebanner rather than add an extra element to the combo <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Get handle of combo edit
$tInfo = $tagCOMBOBOXINFO
_GUICtrlComboBox_GetComboBoxInfo($Combo1, $tInfo)
$hComboEdit = DllStructGetData($tInfo, "hEdit")
; Write CueBanner
$tText = _WinAPI_MultiByteToWideChar("Select one")
_SendMessage($hComboEdit, $EM_SETCUEBANNER, False, $tText, 0, "wparam", "struct*")

Local $Input1 = GUICtrlCreateList("", 150, 20, 150, 815, BitOR($ES_READONLY, $WS_BORDER, $WS_VSCROLL))
GUICtrlSetLimit(-1, 200) ; to limit horizontal scrolling
Local $Input2 = GUICtrlCreateList("", 440, 20, 150, 815, BitOR($ES_READONLY, $WS_BORDER, $WS_VSCROLL))
GUICtrlSetState(-1, $GUI_HIDE)

;~ Controls
Local $Button_1 = GUICtrlCreateButton("Ping", 10, 340, 115)
Local $Button_2 = GUICtrlCreateButton("Wake On Lan", 10, 380, 115)
Local $Button_3 = GUICtrlCreateButton("Netsupport Manager", 10, 440, 115)
Local $Button_4 = GUICtrlCreateButton("Remote Desktop", 10, 475, 115)
Local $Button_5 = GUICtrlCreateButton("VNC", 10, 510, 115)
Local $Button_6 = GUICtrlCreateButton("Start WHD", 10, 750, 115)
Local $Button_7 = GUICtrlCreateButton("Stop WHD", 10, 780, 115)
Local $Display1 = GUICtrlCreateEdit("", 10, 70, 125, 30, BitOR($ES_READONLY, $ES_CENTER)) ;, 0)
Local $Display2 = GUICtrlCreateEdit("", 10, 110, 125, 30, BitOR($ES_READONLY, $ES_CENTER)) ;, 0)


$iIndex = 0
_GUICtrlListBox_ClickItem($Input1, $iIndex)
$iCount = _GUICtrlListBox_GetCount($Input1)

; _AddHorzSep(10, 40, 290)
_AddHorzSep(10, 325, 125)
_AddHorzSep(10, 420, 125)
;~ _AddHorzSep(10, 690, 125)

; Get a timestamp
$iBegin = TimerInit()

While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit


Case $Button_1
Switch GUICtrlRead($Button_1)
; What does the button tell us we are going to do?
Case "Ping"
; We need to ping
$fPing = True
; And set the button text accordingly
GUICtrlSetData($Button_1, "Stop")
; Disable the combo <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GUICtrlSetState($Combo1, $GUI_DISABLE)
Case Else
; Now we need to stop pinging
$fPing = False
; And again change the button text
GUICtrlSetData($Button_1, "Ping")
; Enable the combo <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GUICtrlSetState($Combo1, $GUI_ENABLE)
EndSwitch


Case $Combo1
Switch GUICtrlRead($Combo1)
Local $ip = mysql("PC1")
Local $mac = mysql("PC1")
Case "Branch1"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch1
GUICtrlSetData($Input1, "|" & $List1)
;~ GUICtrlSetData($Input2, "|" & $List21)
Case "Branch2"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch2
GUICtrlSetData($Input1, "|" & $List2)
;~ GUICtrlSetData($Input2, "|" & $List22)
Case "Branch3"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch3
GUICtrlSetData($Input1, "|" & $List3)
;~ GUICtrlSetData($Input2, "|" & $List23)
Case "Branch4"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch4
GUICtrlSetData($Input1, "|" & $List4)
;~ GUICtrlSetData($Input2, "|" & $List24)
Case "Branch5"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch5
GUICtrlSetData($Input1, "|" & $List5)
;~ GUICtrlSetData($Input2, "|" & $List25)
Case "Branch6"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch6
GUICtrlSetData($Input1, "|" & $List6)
;~ GUICtrlSetData($Input2, "|" & $List26)
Case "Branch7"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch7
GUICtrlSetData($Input1, "|" & $List7)
;~ GUICtrlSetData($Input2, "|" & $List27)
Case "Branch8"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch8
GUICtrlSetData($Input1, "|" & $List8)
;~ GUICtrlSetData($Input2, "|" & $List28)
Case "Branch9"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch9
GUICtrlSetData($Input1, "|" & $List9)
;~ GUICtrlSetData($Input2, "|" & $List29)
Case "Branch10|"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch10
GUICtrlSetData($Input1, "|" & $List10)
;~ GUICtrlSetData($Input2, "|" & $List30)
Case "Branch11"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch11
GUICtrlSetData($Input1, "|" & $List11)
;~ GUICtrlSetData($Input2, "|" & $List31)
Case "Branch12"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch12
GUICtrlSetData($Input1, "|" & $List12)
;~ GUICtrlSetData($Input2, "|" & $List32)
Case "Branch13"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch13
GUICtrlSetData($Input1, "|" & $List13)
;~ GUICtrlSetData($Input2, "|" & $List33)
Case "Branch14"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch14
GUICtrlSetData($Input1, "|" & $List14)
;~ GUICtrlSetData($Input2, "|" & $List34)
Case "Branch15"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch15
GUICtrlSetData($Input1, "|" & $List15)
;~ GUICtrlSetData($Input2, "|" & $List35)
Case "Branch16"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch16
GUICtrlSetData($Input1, "|" & $List16)
;~ GUICtrlSetData($Input2, "|" & $List36)
Case "Branch17"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch17
GUICtrlSetData($Input1, "|" & $List17)
;~ GUICtrlSetData($Input2, "|" & $List37)
Case "Branch18"
ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch18
GUICtrlSetData($Input1, "|" & $List18)
;~ GUICtrlSetData($Input2, "|" & $List38)
Case "Branch Servers"
;~ ConsoleWrite("Hit" & @CRLF) ; <<<<<<<<<<<<< Branch Servers
GUICtrlSetData($Input1, "|" & $List19)
Case "Core Servers"
MsgBox(0, "", GuiCtrlRead($ip[1]))

EndSwitch

; Get new count <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$iCount = _GUICtrlListBox_GetCount($Input1)


Case $Button_2
WakeOnLan()


Case $Button_3
If ProcessExists("PCICTLUI.EXE") Then
Netsupport()
Else
Run("C:\Program Files (x86)\NetSupport\NetSupport Manager\PCICTLUI.EXE", "")
Netsupport()
EndIf


Case $Button_4
RDP()


Case $Button_5
VNC()


Case $Button_6
WHD_Start()


Case $Button_7
WHD_Stop()


EndSwitch

If $fPing Then
; Have we waited long enough since the last ping?
If TimerDiff($iBegin) > 1000 Then ; 1 sec delay
; Click the current item
_GUICtrlListBox_ClickItem($Input1, $iIndex)
; Read the current item
$sItem = GUICtrlRead($Input1)
$var = Ping(GUICtrlRead($Input1), 999)
If $var <> 0 Then
GUICtrlSetData($Display1, $sItem) ; & " - " & $var & " ms")
GUICtrlSetBkColor($Display1, 0x00FF00)
GUICtrlSetColor($Display1, 0x000000)
GUICtrlSetData($Display2, " " & $var & " ms")
GUICtrlSetBkColor($Display2, 0x00FF00)
GUICtrlSetColor($Display2, 0x000000)
Else
GUICtrlSetData($Display1, $sItem)
GUICtrlSetBkColor($Display1, 0)
GUICtrlSetColor($Display1, 0xFFFFFF)
GUICtrlSetData($Display2, "Request Timed Out ")
GUICtrlSetBkColor($Display2, 0)
GUICtrlSetColor($Display2, 0xFFFFFF)
EndIf
; Reset the timestamp for the next ping
$iBegin = TimerInit()
; Increase the index to select the next item
$iIndex = Mod($iIndex + 1, $iCount)
EndIf
EndIf

WEnd

Func _AddHorzSep($iX, $iY, $iW)

GUICtrlCreateLabel("", $iX, $iY, $iW, 1)
GUICtrlSetBkColor(-1, 0x000000)

EndFunc ;==>_AddHorzSep

; MySQL Function
Func sql($branch)
; establish an array with too many elements
Local $output[80]

; establish a counter starting at zero
$count = 0


; put query together
$sql = _MySQLConnect("DHCP","Yvl1234","test","192.168.26.18")
$var = _Query($sql,"SELECT * FROM Leases WHERE Branch='" & $branch & "'")

With $var


;loop through the query result
While NOT .EOF

;put data into array
$output[$count]=.Fields("IP").value


;increment the counter
$count += 1

; end of the loop
wend

; resize the array to the number of elements
ReDim $output[$count]
EndWith
; return the proper sized array
return $output
EndFunc

Func WHD_Start()
Run("cmd.exe")
Sleep(1000)
Send("cd /{Enter}")
Sleep(1000)
Send("cd pstools{Enter}")
Sleep(1000)
Send("psexec \\192.168.26.17\cmd.exe{Enter}")
Sleep(5000)
Send("cd /{Enter}")
Sleep(1000)
Send("cd Program Files\WebHelpDesk{Enter}")
Sleep(1000)
Send("whd_start.bat{Enter}")
EndFunc

Func WHD_Stop()
Run("cmd.exe")
Sleep(1000)
Send("cd /{Enter}")
Sleep(1000)
Send("cd pstools{Enter}")
Sleep(1000)
Send("psexec \\192.168.26.17\ cmd.exe{Enter}")
Sleep(5000)
Send("cd /{Enter}")
Sleep(1000)
Send("cd Program Files\WebHelpDesk{Enter}")
Sleep(1000)
Send("whd_stop.bat{Enter}")
EndFunc

Func VNC()
Run("C:\Program Files\RealVNC\VNC Viewer\vncviewer.exe")
WinWaitActive("VNC Viewer")
Send(GUICtrlRead($Input1))
Sleep(1000)
Send("{ENTER}")
EndFunc

Func RDP()
Run("mstsc.exe /console")
WinWaitActive("Remote Desktop Connection")
Send(GUICtrlRead($Input1))
Sleep(1000)
Send("{ENTER}")
EndFunc

Func Netsupport()
Local $Input3 = GUICtrlRead($Input1)
Local $Input4 = StringTrimRight(GUICtrlRead($Input1), 12)
WinActivate("NetSupport : ")
Send("!C{Down}")
Send("Q")
Sleep(1000)
Send("^A")
Sleep(1000)
;~ sql()
;~ Send(GuiCtrlRead($fields2))
Send("{ENTER}")
Sleep(1000)
If WinActive("Security") Then
Send("{Tab}")
Sleep(1000)
Send("ceeWee6o")
Sleep(2000)
Send("{Enter}")
Sleep(1000)
Else
Send("!C{Down}")
Send("w{Enter}")

EndIf

EndFunc

Func WakeOnLan() ; need to fix this so that it removes all that funky stuff I need it to
$IPAddress = GuiCtrlRead() ; I want to be able to have this use the IP address associated with the computer name in $Input1
$MACAddress = GUICtrlRead() ; I want to be able to have this use the MAC address associated with the computer name in $Input1


UDPStartUp()

$connexion = UDPOpen($IPAddress, 7)
$res = UDPSend($connexion, GenerateMagicPacket($MACAddress))
MsgBox(0, "", $res)

UDPCloseSocket($connexion)
UDPShutdown()

EndFunc

; ===================================================================
; Functions
; ===================================================================


; This function convert a MAC Address Byte (e.g. "1f") to a char
Func HexToChar($strHex)

Return Chr(Dec($strHex))

EndFunc

; This function generate the "Magic Packet"
Func GenerateMagicPacket($strMACAddress)

$MagicPacket = ""
$MACData = ""

For $p = 1 To 11 Step 2
$MACData = $MACData & HexToChar(StringMid($strMACAddress, $p, 2))
Next

For $p = 1 To 6
$MagicPacket = HexToChar("ff") & $MagicPacket
Next

For $p = 1 To 16
$MagicPacket = $MagicPacket & $MACData
Next

Return $MagicPacket

EndFunc
Edited by grimmlock

Thanks

Grimm

Link to comment
Share on other sites

grimmlock,

Two things:

1 - You've defined what you want to do but not what is failing

2 - Your code cannot be run as posted to duplicate the error, if any.

If you are seeking advice on technique (as you seem to be doing) then post a small, runnable reproducer.

kylomas

edit: Is this question related to thread?

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...