Jump to content

Global variable problem


Recommended Posts

I have this script and I declare $bAlive to be global but it is not acting like a global variable. When it goes into selection() it sets $bAlive to false and that function then eventually will call the PacketX_OnPacket() which does some checking. If it is true then it sets $bAlive to true. I have confirmed that it does this eventually in that function but it doesn't update the variable globally! When it comes back to selection() $bAlive is still false even though it was updated to true in PacketX_OnPacket() and was assigned to be global! What do I do?!

#NoTrayIcon
#AutoIt3Wrapper_Change2CUI=y

#Region packetx declares
Const $PktXPacketTypePromiscuous = 0x0020
Const $PktXLinkType802_3 = 1
Const $PktXLinkType802_5 = 2
Const $PktXLinkTypeFddi = 3
Const $PktXLinkTypeWan = 4
Const $PktXLinkTypeLocalTalk = 5
Const $PktXLinkTypeDix = 6
Const $PktXLinkTypeArcnetRaw = 7
Const $PktXLinkTypeArcnet878_2 = 8
Const $PktXLinkTypeAtm = 9
Const $PktXLinkTypeWirelessWan = 10
Const $PktXModeCapture = 1

; Protocol types
Const $PktXProtocolTypeEthernet = 1
Const $PktXProtocolTypeIp = 2
Const $PktXProtocolTypeUdp = 3
Const $PktXProtocolTypeTcp = 4
#EndRegion

Global $bAlive

Global $oPktX = ObjCreate("PktX.PacketX")
If Not IsObj($oPktX) Then 
    MsgBox(0, "ERROR", "No Object")
    Exit
EndIf

ObjEvent($oPktX, "PacketX_")
If @error Then
    MsgBox(0, "ERROR", "No Object")
    Exit
EndIf

For $i = 1 to $oPktX.Adapters.Count
    If $oPktX.Adapters($i).Isgood Then
        ConsoleWrite($i & ". " & $oPktX.Adapters($i).Description & @CRLF)
    EndIf
Next
ConsoleWrite("Choose Adapter: ")
$oPktX.Adapter = $oPktX.Adapters(Number(cmdRead()))

$oPktX.Adapter.BuffSize = 256 * 1024 ; 256 KB
$oPktX.Adapter.BuffMinToCopy = 0
$oPktX.Adapter.HWFilter = $PktXPacketTypePromiscuous
$oPktX.Adapter.Mode = $PktXModeCapture
$sHWAddr = $oPktX.Adapter.HWAddress
$sIPAddr = $oPktX.Adapter.NetIP
$aIPAddr = StringSplit($sIPAddr, ".")

Dim $aPacket[60] = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, _
      Number(Dec(StringMid($sHWAddr, 1, 2))), _
      Number(Dec(StringMid($sHWAddr, 4, 2))), _
      Number(Dec(StringMid($sHWAddr, 7, 2))), _
      Number(Dec(StringMid($sHWAddr, 10, 2))), _
      Number(Dec(StringMid($sHWAddr, 13, 2))), _
      Number(Dec(StringMid($sHWAddr, 16, 2))), _
      0x08, 0x06, 0x00, 0x01, _
      0x08, 0x00, 0x06, 0x04, 0x00, 0x01, _
      Number(Dec(StringMid($sHWAddr, 1, 2))), _
      Number(Dec(StringMid($sHWAddr, 4, 2))), _
      Number(Dec(StringMid($sHWAddr, 7, 2))), _
      Number(Dec(StringMid($sHWAddr, 10, 2))), _
      Number(Dec(StringMid($sHWAddr, 13, 2))), _
      Number(Dec(StringMid($sHWAddr, 16, 2))), _   
      Number($aIPAddr[1]), _
      Number($aIPAddr[2]), _
      Number($aIPAddr[3]), _
      Number($aIPAddr[4]), _
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
      0, _
      0, _
      0, _
      0, _
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, _
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00]

ConsoleWrite("Type in IP: ")
$sIPReso = cmdRead()

$iNum = 0
If StringInStr($sIPReso, "-") Then
    $aIP = StringSplit($sIPReso, "-")
    If Not IsIPAddress($aIP[1]) Then usage()
    If Not IsIPAddress($aIP[2]) Then usage()
    $aIP1 = StringSplit($aIP[1], ".")
    $aIP2 = StringSplit($aIP[2], ".")
        
    For $i = 1 to 4
        If Number($aIP1[$i]) <> Number($aIP2[$i]) Then ExitLoop
    Next
        
    $time = TimerInit()
    Select
        Case $i = 1
            For $iIP = Number($aIP1[$i]) to Number($aIP2[$i])
                For $i1 = 1 to 255
                    For $i2 = 1 to 255
                        For $i3 = 1 to 255
                            $sIPReso = (String($iIP & "." & $i1 & "." & $i2 & "." & $i3))
                            selection($sIPReso, $aPacket)
                            $iNum += 1
                            If $i3 = Number($aIP2[4]) AND $i2 = Number($aIP2[3]) Then ExitLoop
                        Next
                        $iNum += 1
                        If $i2 = Number($aIP2[3]) AND $i1 = Number($aIP2[2]) Then ExitLoop
                    Next
                    $iNum += 1
                    If $i1 = Number($aIP2[2]) AND $iIP = Number($aIP2[$i]) Then ExitLoop
                Next
                $iNum += 1
            Next
            $iNum -= 4
        Case $i = 2
            For $iIP = Number($aIP1[$i]) to Number($aIP2[$i])
                For $i1 = 1 to 255
                    For $i2 = 1 to 255
                        $sIPReso = (String($aIP2[1] & "." & $iIP & "." & $i1 & "." & $i2))
                        selection($sIPReso, $aPacket)
                        $iNum += 1
                        If $i2 = Number($aIP2[4]) AND $i1 = Number($aIP2[3]) Then ExitLoop
                    Next
                    $iNum += 1
                    If $i1 = Number($aIP2[3]) AND $iIP = Number($aIP2[$i]) Then ExitLoop
                Next
                $iNum += 1
            Next
            $iNum -= 3
        Case $i = 3
            For $iIP = Number($aIP1[$i]) to Number($aIP2[$i])
                For $i1 = 1 to 255
                    $sIPReso = (String($aIP2[1] & "." & $aIP2[2] & "." & $iIP & "." & $i1))
                    selection($sIPReso, $aPacket)
                    $iNum += 1
                    If $i1 = Number($aIP2[4]) AND $iIP = Number($aIP2[$i]) Then ExitLoop
                Next
                $iNum += 1
            Next
            $iNum -= 2
        Case $i = 4
            For $iIP = Number($aIP1[$i]) to Number($aIP2[$i])
                $sIPReso = (String($aIP2[1] & "." & $aIP2[2] & "." & $aIP2[3] & "." & $iIP))
                selection($sIPReso, $aPacket)
                $iNum += 1
            Next
            $iNum -= 1
    EndSelect
    ConsoleWrite("Scanned " & $iNum & " addresses in " & round(TimerDiff($time) / 1000, 2) & " seconds" & @CRLF)    
Else
    If Not IsIPAddress($sIPReso) Then usage()
    selection($sIPReso, $aPacket)
EndIf

Func selection($sIPReso, $aPacket)
    $aIPReso = StringSplit($sIPReso, ".")

    $aPacket[38] = Number($aIPReso[1])
    $aPacket[39] = Number($aIPReso[2])
    $aPacket[40] = Number($aIPReso[3])
    $aPacket[41] = Number($aIPReso[4])

    $oPktX.Start
    $oPktX.Adapter.SendPacket($aPacket, 1) ;send one packet 

    $bAlive = False
    $begin = TimerInit()
    While TimerDiff($begin) < 5000
        Sleep(20)
        If $bAlive = True Then 
            ConsoleWrite($sIPReso & " is online" & @CRLF)
            ExitLoop
        EndIf
    WEnd
    $oPktX.Stop

    If $bAlive = False Then ConsoleWrite($sIPReso & " is offline" & @CRLF)
EndFunc

Func PacketX_OnPacket($oPacket)
    $aData = $oPacket.Data
    $iHWType = $aData[14] & $aData[15]
    $iProtocolType = $aData[16] & $aData[17]
    $iOPCode = $aData[20] & $aData[21]
    $sSenderIP = $aData[28] & "." & $aData[29] & "." & $aData[30] & "." & $aData[31]
    $sDestMac = ""
    For $i = 32 to 37
        $sDestMac &= Hex($aData[$i], 2) & ":"
    Next
    $sDestMac = StringTrimRight($sDestMac, 1)
    $sDestIP = $aData[38] & "." & $aData[39] & "." & $aData[40] & "." & $aData[41]
    
    If $iHWType = 01 And $iProtocolType = 80 And $iOPCode = 02 And _
    StringCompare($sSenderIP, $sIPReso) = 0 And StringCompare($sDestMac, $sHWAddr) = 0 And _
    StringCompare($sDestIP, $sIPAddr) = 0 Then $bAlive = True
EndFunc   ;==>PacketX_OnPacket

Func cmdRead()
    Local $input = ""
    $file = FileOpen("con", 4)
    While 1
        $chr = FileRead($file, 1)
        If $chr = @LF Then ExitLoop
        $input &= BinaryToString($chr)
        Sleep(50)
    WEnd
    FileClose($file)
    $input = StringReplace($input, @CR, "")
    Return $input
EndFunc

Func IsIPAddress($text)
    Return StringRegExp($text, "(((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))")
EndFunc

Func usage()
    ConsoleWrite("Usage: Enter single ip or range divided by - Example 192.168.1.1-192.168.1.10")
    Exit
EndFunc
Edited by SoulA
Link to comment
Share on other sites

I would suggest this is a logic problem issue.

First thing I would do is assign $bAlive false upon decleration then add "#AutoIt3Wrapper_Run_Debug_Mode=Y" to the top of your script.

I think somewhere $bAlive is being assigned where you don't know - trace the code and you will find it.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...