LithiumLi Posted March 28, 2009 Share Posted March 28, 2009 Hello ok I am really bad at strings #include <Constants.au3> $Port = 80 ConsoleWrite(_CmdRead(_CmdReturn("netstat -n") , $Port)) Func _CmdRead($CmdRead , $ReadPort) Local $Return = "" , $String $String = StringSplit(StringStripCR($CmdRead), @LF) For $i = 1 to UBound($String) -1 If StringInStr($String[$i], ":" & String($ReadPort)) Then $Return &= $String[$i] & @CRLF EndIf Next Return $Return EndFunc Func _CmdReturn($CmdCommand) $CmdReturn = "" $Stream = Run(@ComSpec & " /c " & $CmdCommand , @SystemDir , @SW_HIDE , $STDOUT_CHILD + $STDIN_CHILD) While 1 $Line = StdoutRead($Stream) If @error Then ExitLoop $CmdReturn &= $Line WEnd Return $CmdReturn EndFunc Example Console Return Value looks like this TCP 127.0.0.1:4043 127.0.0.1:4044 ESTABLISHED The information I would like returned is this 127.0.0.1:4043 so I need to strip the "tcp" and all the white spaces and 127.0.0.1:4043 but keep one I am not sure if the "local" or "foreign" address would be better to keep and also I want to remove the ESTABLISHED text but sometimes it doesn't say ESTABLISHED but it says something else such as Closed/Wait etc... Link to comment Share on other sites More sharing options...
Authenticity Posted March 28, 2009 Share Posted March 28, 2009 Maybe like this: ... If StringInStr($String[$i], ":" & String($ReadPort)) Then $String[$i] = StringTrimLeft($String[$i], StringLen(' TCP ')) $String[$i] = StringLeft($String[$i], StringInStr($String[$i], ':')+StringLen($Port)) $Return &= $String[$i] & @CRLF EndIf ... Link to comment Share on other sites More sharing options...
LithiumLi Posted March 29, 2009 Author Share Posted March 29, 2009 Maybe like this: ... If StringInStr($String[$i], ":" & String($ReadPort)) Then $String[$i] = StringTrimLeft($String[$i], StringLen(' TCP ')) $String[$i] = StringLeft($String[$i], StringInStr($String[$i], ':')+StringLen($Port)) $Return &= $String[$i] & @CRLF EndIf ...oÝ÷ Ûú®¢×+0¢¹,nëH¶§l°k)¢[y¦â¶Æz´ázåÊènW¦Â+a)ÞÈën®{-ÂßÛ0éÈ¢yrÁ©í¶¨ì"¶ayÊ+ç-¦íjëh×6Func _CmdRead($CmdRead , $ReadPort) Local $Return = "" , $String $String = StringSplit(StringStripCR($CmdRead), @LF) For $i = 1 to UBound($String) -1 If StringInStr($String[$i], ":" & String($ReadPort)) Then $String[$i] = StringReplace($String[$i] , "TCP" , "") $String[$i] = StringStripWS($String[$i] , 1) ;3 ;ClipPut($String[$i]) $Split = StringInStr($String[$i] , ":") $String[$i] = StringTrimLeft($String[$i] , $Split + StringLen($Port)) $String[$i] = StringStripWS($String[$i] , 1) $String[$i] = StringLeft($String[$i] , $Split + StringLen($Port)) ;;;;; ;;;;; $Return &= $String[$i] & @CRLF EndIf Next Return $Return EndFunc Link to comment Share on other sites More sharing options...
Authenticity Posted March 29, 2009 Share Posted March 29, 2009 Well, it's depended on the exact match that you need.I mean, for this:TCP 127.0.0.1:4043 127.0.0.1:4044 ESTABLISHEDor this:TCP 127.0.0.1:4043 127.0.0.1:4044 ESTABLISHEDAnd what about this:TCP 127.0.0.1:4044 127.0.0.1:4043 ESTABLISHEDor this:TCP 127.0.0.1:4044 127.0.0.1:4043 ESTABLISHEDCheck this one, tell me what you think:#include <Constants.au3> $Port = 80 ConsoleWrite(_CmdRead(_CmdReturn("netstat -n") , $Port)) Func _CmdRead($CmdRead , $ReadPort) Local $Return = "" , $String Local $sPattern = "\b(?>(?:\d|[01]?\d\d|25[0-5]|2[0-4]\d)\.){3}(?:\d|[01]?\d\d|25[0-5]|2[0-4]\d):\Q" & $Port & '\E\b' $String = StringSplit(StringStripCR($CmdRead), @LF) For $i = 1 to UBound($String) -1 If StringInStr($String[$i], ":" & String($ReadPort)) Then Local $aTmp = StringRegExp($CmdRead, $sPattern, 1) If IsArray($aTmp) Then ConsoleWrite($aTmp[0] & @LF) $Return &= $String[$i] & @CRLF EndIf Next Return $Return EndFunc Func _CmdReturn($CmdCommand) $CmdReturn = "" $Stream = Run(@ComSpec & " /c " & $CmdCommand , @SystemDir , @SW_HIDE , $STDOUT_CHILD + $STDIN_CHILD) While 1 $Line = StdoutRead($Stream) If @error Then ExitLoop $CmdReturn &= $Line WEnd Return $CmdReturn EndFunc Link to comment Share on other sites More sharing options...
LithiumLi Posted March 29, 2009 Author Share Posted March 29, 2009 (edited) Well, it's depended on the exact match that you need. I mean, for this: TCP 127.0.0.1:4043 127.0.0.1:4044 ESTABLISHED or this: TCP 127.0.0.1:4043 127.0.0.1:4044 ESTABLISHED And what about this: TCP 127.0.0.1:4044 127.0.0.1:4043 ESTABLISHED or this: TCP 127.0.0.1:4044 127.0.0.1:4043 ESTABLISHED Check this one, tell me what you think: #include <Constants.au3> $Port = 80 ConsoleWrite(_CmdRead(_CmdReturn("netstat -n") , $Port)) Func _CmdRead($CmdRead , $ReadPort) Local $Return = "" , $String Local $sPattern = "\b(?>(?:\d|[01]?\d\d|25[0-5]|2[0-4]\d)\.){3}(?:\d|[01]?\d\d|25[0-5]|2[0-4]\d):\Q" & $Port & '\E\b' $String = StringSplit(StringStripCR($CmdRead), @LF) For $i = 1 to UBound($String) -1 If StringInStr($String[$i], ":" & String($ReadPort)) Then Local $aTmp = StringRegExp($CmdRead, $sPattern, 1) If IsArray($aTmp) Then ConsoleWrite($aTmp[0] & @LF) $Return &= $String[$i] & @CRLF EndIf Next Return $Return EndFunc Func _CmdReturn($CmdCommand) $CmdReturn = "" $Stream = Run(@ComSpec & " /c " & $CmdCommand , @SystemDir , @SW_HIDE , $STDOUT_CHILD + $STDIN_CHILD) While 1 $Line = StdoutRead($Stream) If @error Then ExitLoop $CmdReturn &= $Line WEnd Return $CmdReturn EndFunc Thats very good but I just learned something new today, did you know you can do this. netstat -n | find "80" This is a lot faster and doesn't require a lot of strings and makes return much faster in a loop. Edited March 29, 2009 by LithiumLi 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