ohnoe Posted March 18, 2012 Share Posted March 18, 2012 (edited) Hello, so I'm trying to learn a bit with this language and am having an issue finding out what to do with stringinstr to get what I need done. The issue I have is I want to check if a certain set of strings in a row exist, so as of right now my $string can return one of two things: [Test.exe] TCP 0000 0000 ESTABLISHED or just [Test.exe] I need to be able to differentiate the results from each other, but I'm unsure how to do that due to I don't see a way I can stringinstr search for two lines to exist instead of one, as there will be a lot of similar results in my $string, but I only need to know if there is a TCP after the test.exe only, not the other .exes. Here is the code I have now to see if the [test.exe] exist, but I need to figure out how to also check if the TCP 000 0000 ESTABLISHED exist directly under the test.exe line, as there will be lots of others [random.exe] with TCP's under it as well. #include <Constants.au3> $cmd = "netstat -b" $string = _CMDreturn($cmd) Local $file = FileOpen("test.txt", 1) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf Local $result = StringInStr($string, "[test.exe] ") FileClose($file) MsgBox(0, $cmd, $result) Func _CMDreturn($sCommand) ; Returns a the output of a DOS command as a string $cmdreturn = "" $stream = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDIN_CHILD) While 1 ; loop through the return from the command until there is no more $line = StdoutRead($stream) If @error Then ExitLoop $cmdreturn &= $line WEnd Return $cmdreturn EndFunc ;==>_CMDreturn Edited March 18, 2012 by ohnoe Link to comment Share on other sites More sharing options...
Skitty Posted March 18, 2012 Share Posted March 18, 2012 (edited) Well, I really have no idea but I made you this..For $x = 1 To 9001 $Line = FileReadLine(@DesktopDir & "test.text",$x) If @error Then ExitLoop ToolTip($Line) $R = StringRegExp($Line,"[(.*)]",3) If @error Then ContinueLoop Else If StringInStr($R[0],"Test.exe",2) Then For $y = $x + 1 to 9001 $R = StringRegExp($Line,"[(.*)]",3) If @error Then ExitLoop $Line = FileReadLine(@DesktopDir & "test.text",$y) If Not @error Then If StringInStr($Line,"ESTABLISHED",2) Then MsgBox(64,$R[0],$Line & @CR & "line number:" & $y) ExitLoop 2 EndIf Else ExitLoop EndIf Next EndIf EndIf NextIs that more or less what you're talking about?It reads files like this..[chrome.exe] TCP 0000 0000 ESTABLISHED [utorrent.exe] TCP 0000 0000 ESTABLISHED [MyIrc.exe] [doomsdaydevice.exe] TCP 0000 0000 ESTABLISHED [moonprobe.exe] TCP 0000 0000 ESTABLISHED [Test.exe] TCP 0000 0000 ESTABLISHED [kilimanjaro.exe] [switch.exe] TCP 0000 0000 ESTABLISHEDEdit Reason: Shitty code... Edited March 18, 2012 by THAT1ANONYMOUSEDUDE Link to comment Share on other sites More sharing options...
ohnoe Posted March 18, 2012 Author Share Posted March 18, 2012 I don't think that really accomplishes what I'm referring to. Is there anyway for StringinStr or something similar to return the line the string is on in the file? I was thinking I could do something like this if StringinStr returned the line the text was on, at the very least this example code should give people a better idea of what I'm trying to accomplish. I'm trying to find out if the line under the "test.exe" line contains the text "TCP". $result = StringInStr($string, "[Test.exe] ") $check = FileReadLine("filehandle/filename" [, $result + 1) $result2 = StringinStr($check, "TCP") if $result2 > 0 Then do blah blah blah Link to comment Share on other sites More sharing options...
Skitty Posted March 18, 2012 Share Posted March 18, 2012 I don't think that really accomplishes what I'm referring to. Is there anyway for StringinStr or something similar to return the line the string is on in the file? I was thinking I could do something like this if StringinStr returned the line the text was on, at the very least this example code should give people a better idea of what I'm trying to accomplish. I'm trying to find out if the line under the "test.exe" line contains the text "TCP". $result = StringInStr($string, "[Test.exe] ") $check = FileReadLine("filehandle/filename" [, $result + 1) $result2 = StringinStr($check, "TCP") if $result2 > 0 Then do blah blah blah Then why not post an example of the actual file that you're parsing instead, that example really isn't helping much... $Log = @DesktopDir & "test.text" $Name = "Test.exe" $Str2Match = "TCP";"ESTABLISHED" For $x = 1 To 9001 $Line = FileReadLine($Log,$x) If @error Then ExitLoop If StringInStr($Line,$Name,2) Then For $y = $x + 1 to 9001 $Line = FileReadLine($Log,$y) If Not @error Then If StringInStr($Line,$Str2Match,2) Then MsgBox(64,$Name,$Line & @CR & "line number:" & $y) ExitLoop EndIf Else ExitLoop EndIf Next EndIf Next Link to comment Share on other sites More sharing options...
czardas Posted March 18, 2012 Share Posted March 18, 2012 (edited) Here's how I would approach this using regexp. This code would replace StringInString for this particular search. I don't know if it solves your problem, but I have created two versions. There isn't much difference between them. ; Version 1 $string = "[Test.exe]" & @LF & " TCP" ; This is a sample search string If StringRegExp($string, "([Test.exe]s+TCP)") Then MsgBox(0, "", "Found") ; Version 2 $sExe = "Test.exe" $string = "[" & $sExe & "]" & @LF & " TCP" ; This is a sample string If StringRegExp($string, "([" & $sExe & "]s+TCP)") Then MsgBox(0, "", "Found") Edit2 => added '+' after 's' Hopefully it's okay now. Dinner's getting cold. Edit3 Just found another error. Damn! $sExe = "Test.exe" $string = "[" & $sExe & "]" & @LF & " TCP 0000 0000 ESTABLISHED" ; This is a sample string If StringRegExp($string, "([" & $sExe & "]s+TCPh*0000h*0000h*ESTABLISHED)") Then MsgBox(0, "", "Found") Edited March 19, 2012 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
ohnoe Posted March 19, 2012 Author Share Posted March 19, 2012 (edited) Would something like this work? Note $string2 returns something like this: Active Connections Proto Local Address Foreign Address State TCP 000 000 ESTABLISHED [chrome.exe] TCP 000 000 ESTABLISHED [chrome.exe] TCP 000 000 CLOSE_WAIT [jusched.exe] TCP 1000 000 ESTABLISHED [WoW.exe] TCP 000 000 ESTABLISHED And I'm trying to see if it returns this: [WoW.exe]or this: [WoW.exe] TCP 000 000 ESTABLISHED The problem I am having is finding a way to check if [WoW.exe] exist some where in the big list of results, if also the TCP under it exist or not, and only for the WoW result. #include <Constants.au3> $cmd = "netstat -b" $string2 = _CMDreturn($cmd) If StringRegExp($string2, "([WoW.exe]s+[TCP])") Then MsgBox(0, "", "Found") ;MsgBox(0, $cmd, $result) Func _CMDreturn($sCommand) ; Returns a the output of a DOS command as a string $cmdreturn = "" $stream = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDIN_CHILD) While 1 ; loop through the return from the command until there is no more $line = StdoutRead($stream) If @error Then ExitLoop $cmdreturn &= $line WEnd Return $cmdreturn EndFunc ;==>_CMDreturn Edited March 19, 2012 by ohnoe Link to comment Share on other sites More sharing options...
czardas Posted March 19, 2012 Share Posted March 19, 2012 (edited) Here's a more complete version: StringRegExp($string, "([WoW.exe]s+TCPh*000h*000h*ESTABLISHED)") This should return True if your string contains exactly this: [WoW.exe] TCP 000 000 ESTABLISHED I notice you have changed the number of zeros. This is confusing. Also the spaces between have changed, however I accounted for that possibility. Edited March 19, 2012 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
ohnoe Posted March 19, 2012 Author Share Posted March 19, 2012 Here's a more complete version: StringRegExp($string, "([WoW.exe]s+TCPh*000h*000h*ESTABLISHED)") This should return True if your string contains exactly this: [WoW.exe] TCP 000 000 ESTABLISHED I notice you have changed the number of zeros. This is confusing. Also the spaces between have changed, however I accounted for that possibility. The reason I changed them is where the zero's are is always a unique number. Would changing: StringRegExp($string, "([WoW.exe]s+TCPh*000h*000h*ESTABLISHED)") to StringRegExp($string, "([WoW.exe]s+TCP)") work in order to check if it has the TCP part? the numbers after it are completely irrelevant, we only need to see if it has a TCP connection open, which if TCP is there it is open, if not it will be blank under the [WoW.exe]. I appreciate your help and hope you're not annoyed by a new user/programming attempting to learn some noob stuff. Link to comment Share on other sites More sharing options...
czardas Posted March 19, 2012 Share Posted March 19, 2012 Very close. This does it. StringRegExp($string, "(\[WoW.exe\]\s+TCP)") Providing you are not going to ask about automating WoW.exe, I won't get annoyed. Make sure you have read the forum rules. Sorry about my earlier code mistakes. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
ohnoe Posted March 19, 2012 Author Share Posted March 19, 2012 Very close. This does it. StringRegExp($string, "([WoW.exe]s+TCP)") Providing you are not going to ask about automating WoW.exe, I won't get annoyed. Make sure you have read the forum rules. Sorry about my earlier code mistakes. Checking to see of I'm connected to WoW or not is my simple goal. Not only am I not interested in automating WoW, if I can't understand something so simple like this I think it would be a bit out of my league . I'm only attempting to see if I'm connected as a first time coding project, sadly I resorted to being unable to figure it out on my own and referring to help here Link to comment Share on other sites More sharing options...
czardas Posted March 19, 2012 Share Posted March 19, 2012 Well that's alright then. I suppose. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Skitty Posted March 19, 2012 Share Posted March 19, 2012 Checking to see of I'm connected to WoW or not is my simple goal. Not only am I not interested in automating WoW, if I can't understand something so simple like this I think it would be a bit out of my league . I'm only attempting to see if I'm connected as a first time coding project, sadly I resorted to being unable to figure it out on my own and referring to help here FYI, if you were to write the string into a text file and parse it with the first script I posted, maybe you'd have noticed it did what you asked, as well as czardas code only difference is that with mine you have to write it to a text file as apposed to using regular expression to parse the string which might be a little confusing for starters. Link to comment Share on other sites More sharing options...
czardas Posted March 19, 2012 Share Posted March 19, 2012 I still find some regular expressions confusing, but they seem to make much more sense to me now than they did a year ago. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
ohnoe Posted March 19, 2012 Author Share Posted March 19, 2012 You're right I probably did not fully understand the code written. This ended up working for my goal though, thanks to the simplicity and continuous response's by czardas #include <Constants.au3> $cmd = "netstat -b" $string2 = _CMDreturn($cmd) $result = StringRegExp($string2, "([WoW.exe]s+TCP)") MsgBox(0,"",$result) ;MsgBox(0, $cmd, $result) Func _CMDreturn($sCommand) ; Returns a the output of a DOS command as a string $cmdreturn = "" $stream = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDIN_CHILD) While 1 ; loop through the return from the command until there is no more $line = StdoutRead($stream) If @error Then ExitLoop $cmdreturn &= $line WEnd Return $cmdreturn EndFunc ;==>_CMDreturn Link to comment Share on other sites More sharing options...
czardas Posted March 19, 2012 Share Posted March 19, 2012 It's a learning experience for me too. I'm glad it was useful to you in the end. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now