Sign in to follow this  
Followers 0
LithiumLi

Cmd Read Help

3 posts in this topic

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

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

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!

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  
Followers 0