Jump to content
Sign in to follow this  
alientorress

Help with my Script and netstat Check by For Loop

Recommended Posts

alientorress

Hello to everyone.

My problem is my For Loop is not working in the way it should please help...

Why my script do is create an incoming connection(Dial-In) wait arround 30s to modem connection complete, then search on Systray tooltips to find if the connection is established and then run netstat to check if the incoming user have an established connection with my mail server, POP3 or SMTP, if not then delete connection in 2 minutes or so.

But if the user have an established connection with my mail then wait until a maximun of 16Min so so...

The problem is sometimes the user is downloading mails and the script go and delete the connection so for some reason my netstat loop is not working very well i will put here the functions involved but if someone need the entire code letme know...

Thanks in advance... Kind Regards Alien.

Func Conected()
If FileExists(@ScriptDir&"-Status.txt") Then
  FileDelete(@ScriptDir&"-Status.txt")
EndIf
_RunDOS('netstat -anop TCP > ' & @ScriptDir&"-Status.txt")
Local $aRecords
_FileReadToArray(@ScriptDir&"-Status.txt",$aRecords)
$pcname = @ComputerName
For $x = 1 to $aRecords[0]
  If  StringInStr($aRecords[$x], "192.168.0.1"&":25") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") OR _
   StringInStr($aRecords[$x], "192.168.0.1"&":110") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") _
  Then
   $conexyes = 1
  Else
   $conexyes = 0
  EndIf
Sleep(50)
Next
EndFunc
Func CheckConexMail()
If Get_ConexExist($conexname) = 0 AND $delcon = 0 Then
  DelConex()
EndIf
While Get_ConexExist($conexname) <> 0
  Conected()
  If $conexyes <> 0 Then
   Sleep(500)
   $conbreak = $conbreak + 1
   If $conbreak >= 1800 AND $delcon = 0 Then
    DelConex()
    ExitLoop
   Else
    If Get_ConexExist($conexname) <> 0 Then
     CheckConexMail()
    ElseIf $delcon = 0 Then
     DelConex()
    ExitLoop
    EndIf
   EndIf
  Else
   Sleep(500)
   $conectime = $conectime + 1
   If $conectime >= 60 AND $delcon = 0 Then
    DelConex()
    ExitLoop
   Else
    CheckConexMail()
   EndIf
  EndIf
WEnd
EndFunc

Share this post


Link to post
Share on other sites
willichan

I don't think it is your For loop. It is the If statement.

You have

If  StringInStr($aRecords[$x], "192.168.0.1"&":25") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") OR _
   StringInStr($aRecords[$x], "192.168.0.1"&":110") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") _
  Then

It should be

If ( StringInStr($aRecords[$x], "192.168.0.1"&":25") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") ) OR _
   ( StringInStr($aRecords[$x], "192.168.0.1"&":110") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") ) _
  Then

Unless I misunderstand, you want either ( conditionA AND conditionB ), OR you want ( conditionC AND conditionD ). Encasing the sets in parenths is essential to get the logic right.

Even simpler, since conditionB is the same as conditionD you could do this

If ( StringInStr($aRecords[$x], "192.168.0.1"&":25") OR _
     ( StringInStr($aRecords[$x], "192.168.0.1"&":110") ) AND _
     StringInStr($aRecords[$x], "ESTABLISHED") _
  Then
Edited by willichan

Share this post


Link to post
Share on other sites
alientorress

I don't think it is your For loop. It is the If statement.

You have

If  StringInStr($aRecords[$x], "192.168.0.1"&":25") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") OR _
   StringInStr($aRecords[$x], "192.168.0.1"&":110") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") _
  Then

It should be

If ( StringInStr($aRecords[$x], "192.168.0.1"&":25") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") ) OR _
   ( StringInStr($aRecords[$x], "192.168.0.1"&":110") AND _
   StringInStr($aRecords[$x], "ESTABLISHED") ) _
  Then

Unless I misunderstand, you want either ( conditionA AND conditionB ), OR you want ( conditionC AND conditionD ). Encasing the sets in parenths is essential to get the logic right.

Even simpler, since conditionB is the same as conditionD you could do this

If ( StringInStr($aRecords[$x], "192.168.0.1"&":25") OR _
     ( StringInStr($aRecords[$x], "192.168.0.1"&":110") ) AND _
     StringInStr($aRecords[$x], "ESTABLISHED") _
  Then

Thanks u so much, i try this and i get unbalance parentexis expresion to i add ) rigth after "ESTABLISHED") so will be like this: "ESTABLISHED")) _

Then all compile and working, anyways i need to wait an user connect and see if the code work properly i will let u know...

Kind Regards

Alien and THANKS u AGAIN...

Share this post


Link to post
Share on other sites
alientorress

Still Not Working:

If ( StringInStr($aRecords[$x], "192.168.0.1"&":25") AND _

StringInStr($aRecords[$x], "ESTABLISHED") ) OR _

( StringInStr($aRecords[$x], "192.168.0.1"&":110") AND _

StringInStr($aRecords[$x], "ESTABLISHED") ) _

Then

With this code is not working, i don't know what is wrong.

Please Help...

Share this post


Link to post
Share on other sites
Aipion

Still Not Working:

If ( StringInStr($aRecords[$x], "192.168.0.1"&":25") AND _

StringInStr($aRecords[$x], "ESTABLISHED") ) OR _

( StringInStr($aRecords[$x], "192.168.0.1"&":110") AND _

StringInStr($aRecords[$x], "ESTABLISHED") ) _

Then

With this code is not working, i don't know what is wrong.

Please Help...

What is the error that you are getting when you run it and show the code that you are running currently.

Share this post


Link to post
Share on other sites
alientorress

What is the error that you are getting when you run it and show the code that you are running currently.

Apreciate your help, please read the post from the begining... everything is there... the problem is my script is deleting de incoming connection even if the user is downloading mail from my server...

Kind Regards...

Share this post


Link to post
Share on other sites
ripdad

Play with this and see if you like it ...

#include 'array.au3'
;
;netstat example
Local $rtn = _GetActiveTCPTable()
If Not IsArray($rtn) Then
    MsgBox(8256, '_GetActiveTCPTable', $rtn)
Else
    _ArrayDisplay($rtn, '_GetActiveTCPTable')
    $rtn = _Conected($rtn)
    MsgBox(0, '', $rtn); <-- the return, (connection = 1) or (no connection = 0)
EndIf
;
Func _Conected($aRecords)
    If Not IsArray($aRecords) Then Return
    Local $conexyes = 0; <-- set to zero
    ;
    For $x = 1 To $aRecords[0][0]
        If $aRecords[$x][6] <> 'ESTABLISHED' Then ContinueLoop; status filter
        If $aRecords[$x][2] <> '192.168.0.1' Then ContinueLoop; ip filter
        ;
        Switch $aRecords[$x][3]
            Case '25', '110'; <-- if needed, more ports could be added (separate them with a comma)
                $conexyes = 1; set to 1 if found
                ExitLoop; <-- escape now, since we have the connection
            Case Else; <-- otherwise, we continue the loop until end of array
                $conexyes = 0
        EndSwitch
    Next
    Return $conexyes
EndFunc
;
;========================================
; Function: _GetActiveTCPTable
; Description: netstat tcp table parser
;
; $aParsed[x][0] = Program
; $aParsed[x][1] = PID
; $aParsed[x][2] = Local IP
; $aParsed[x][3] = Local Port
; $aParsed[x][4] = Foreign IP
; $aParsed[x][5] = Foreign Port
; $aParsed[x][6] = Status
;========================================
Func _GetActiveTCPTable()
    Local $aTemp, $bTemp, $str = ''
    Local $pid = Run(@WindowsDir & 'system32netstat.exe -bn', '', @SW_HIDE, 2)
    While ProcessExists($pid)
        $str &= StdoutRead($pid)
    WEnd
    If Not StringInStr($str, 'TCP') Then Return SetError(-1, 0, 'No_Active_TCP')
    $str = StringTrimLeft($str, StringInStr($str, 'TCP') + 2)
    $str = StringReplace(StringReplace($str, @CR, ''), @LF, '')
    $str = StringReplace(StringReplace($str, '[', ''), ']', '')
    $str = StringStripWS($str, 7)
    Local $aSemiParsed = StringSplit($str, 'TCP ', 1)
    Local $aParsed[$aSemiParsed[0] + 1][7] = [[$aSemiParsed[0], 'PID', 'Local IP', 'Local Port', 'Foreign IP', 'Foreign Port', 'Status']]
    For $i = 1 To $aSemiParsed[0]
        $aTemp = StringSplit(StringStripWS($aSemiParsed[$i], 3), ' ')
        If $aTemp[0] = 4 Then; check for valid string
            $aParsed[$i][0] = 'SYSTEM'
        ElseIf $aTemp[0] = 5 Then
            $aParsed[$i][0] = $aTemp[5]; Program
        Else
            ContinueLoop
        EndIf
        $aParsed[$i][1] = $aTemp[4]; PID
        $bTemp = StringSplit($aTemp[1], ':')
        $aParsed[$i][2] = $bTemp[1]; Local IP
        $aParsed[$i][3] = $bTemp[2]; Local Port
        $bTemp = StringSplit($aTemp[2], ':')
        $aParsed[$i][4] = $bTemp[1]; Foreign IP
        $aParsed[$i][5] = $bTemp[2]; Foreign Port
        $aParsed[$i][6] = $aTemp[3]; Status
    Next
    Return $aParsed; returns array
EndFunc
;

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
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
Sign in to follow this  

×