LithiumLi Posted March 23, 2009 Share Posted March 23, 2009 Hello. I am having a problem can't think of a way to do this, my script uses the "netstat -n" command to retrieve tcp connections and that works great but I am looking for certain ports so I need to some how strip everything except the line that has the port the it would erase all the lines except for the ones with 555 ports. #include <Constants.au3> $Port = 555 _CmdRead(_CmdReturn("netstat -n") , $Port) Func _CmdRead($CmdRead , $ReadPort) ;Strip everything read except for lines that have the port number so return value would look something like this ;TCP 127.0.0.1:555 127.0.0.1:555 Established 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...
jvanegmond Posted March 23, 2009 Share Posted March 23, 2009 Here you go. More basic string operations. #include <Constants.au3> $Port = 555 ConsoleWrite( _CmdRead(_CmdReturn("netstat -n") , $Port) ) Func _CmdRead($CmdRead , $ReadPort) ;Strip everything read except for lines that have the port number so return value would look something like this ;TCP 127.0.0.1:555 127.0.0.1:555 Established Local $sReturn = "", $sLine $sLine = StringSplit(StringStripCR($CmdRead), @LF) For $i = 1 to UBound($sLine)-1 If ( StringInStr($sLine[$i], ":" & String($ReadPort) ) ) Then $sReturn &= $sLine[$i] & @CRLF EndIf Next Return $sReturn 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 github.com/jvanegmond Link to comment Share on other sites More sharing options...
LithiumLi Posted March 23, 2009 Author Share Posted March 23, 2009 Here you go. More basic string operations. #include <Constants.au3> $Port = 555 ConsoleWrite( _CmdRead(_CmdReturn("netstat -n") , $Port) ) Func _CmdRead($CmdRead , $ReadPort) ;Strip everything read except for lines that have the port number so return value would look something like this ;TCP 127.0.0.1:555 127.0.0.1:555 Established Local $sReturn = "", $sLine $sLine = StringSplit(StringStripCR($CmdRead), @LF) For $i = 1 to UBound($sLine)-1 If ( StringInStr($sLine[$i], ":" & String($ReadPort) ) ) Then $sReturn &= $sLine[$i] & @CRLF EndIf Next Return $sReturn 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 Thanks Perfect! 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