Jump to content



Photo

Help with my Script and netstat Check by For Loop


  • Please log in to reply
6 replies to this topic

#1 alientorress

alientorress

    Seeker

  • Active Members
  • 11 posts

Posted 23 February 2012 - 06:24 PM

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.

Plain Text         
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






#2 willichan

willichan

    Old Geek

  • Active Members
  • PipPipPipPipPipPip
  • 387 posts

Posted 23 February 2012 - 07:46 PM

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, 23 February 2012 - 07:48 PM.


#3 alientorress

alientorress

    Seeker

  • Active Members
  • 11 posts

Posted 24 February 2012 - 03:49 AM

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...

#4 alientorress

alientorress

    Seeker

  • Active Members
  • 11 posts

Posted 28 February 2012 - 01:40 AM

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...

#5 Aipion

Aipion

    Prodigy

  • Active Members
  • PipPipPip
  • 191 posts

Posted 28 February 2012 - 01:59 AM

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.

In Development - Setup Fusion

Library - Page Menu Control

Antonio Do Rosario

#6 alientorress

alientorress

    Seeker

  • Active Members
  • 11 posts

Posted 28 February 2012 - 02:11 AM

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...

#7 ripdad

ripdad

    Member

  • Active Members
  • PipPipPipPipPipPip
  • 537 posts

Posted 28 February 2012 - 02:42 PM

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

AutoIt         
#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 ;

I'm pretty sure this script has "some flaws" (somewhere). Welcome to programming!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users