Sign in to follow this  
Followers 0
tater

Locating a string within a string.

4 posts in this topic

Hi everyone. I've been creating/deleting/modifying(mainly out of confusion and anger mind you) this program for the past 3 hours and I simply cannot find an efficient way to really get it to work.

What I'm trying to do is basically a 'netstat' (or netstat -n/B) and pick out any IP(s) containing the port :4000 and have it write to an INI file on my desktop for sort of like an archive. What's giving me great difficulty is the fact that I don't really know if what I'm doing would be the best choice. I've tried using the PID to find specific ports on the output text file but the PID always changes making it complicated. I've also tried finding the .EXE and going from there but they aren't in the same line via text output.

Can someone please help and/or guide me through what I have done and if there's any way to make it more efficient?

This is the output text file from which I'm trying to pick out any IP containing port 4000.

Active Connections

  Proto  Local Address        Foreign Address       State
  TCP   name-pc:1110           localhost:1119        ESTABLISHED
  TCP   name-pc:1119           localhost:1110        ESTABLISHED
  TCP   name-pc:2776           localhost:2777        ESTABLISHED
  TCP   name-pc:2777           localhost:2776        ESTABLISHED
  TCP   name-pc:2778           localhost:2779        ESTABLISHED
  TCP   name-pc:2779           localhost:2778        ESTABLISHED
  TCP   name-pc:1121           205.188.7.152:5190    ESTABLISHED
  TCP   name-pc:1130           cs124.msg.mud.yahoo.com:5050  ESTABLISHED
  TCP   name-pc:1205           gamesurge.com:6668  ESTABLISHED
  TCP   name-pc:1206           216.226.155.237:6668   ESTABLISHED
  TCP   name-pc:2867           63.240.202.131:6112  ESTABLISHED
  TCP   name-pc:2872           63.240.202.143:4000  ESTABLISHED

And this is the program I've written

RunWait(@ComSpec & " /c netstat > " & @TempDir & "\netstat.txt", "", @SW_HIDE)

If FileExists(@TempDir & "\netstat.txt") Then

$ipfile = FileOpen(@TempDir & "\netstat.txt", 0)

While 1
    $ipline = FileReadLine($ipfile)
    If @error = -1 Then ExitLoop
    $ipresult = StringInStr($ipline, ":4000")
    If $ipresult <> "0" Then
        $ipline = StringStripWS($ipline,8)
        IniWrite(@DesktopDir&"\test.ini","section1","key",$ipline)
        $ApplePie=StringTrimRight($ipline, 11)
        IniWrite(@DesktopDir&"\test.ini","section2","key",$ApplePie)
        $Coffee=StringTrimLeft($blah, 15)
        IniWrite(@DesktopDir&"\test.ini","section3","key",$Coffee)
        ExitLoop
    EndIf
Wend
EndIf

It's very hard to pick out things from the netstat commands because it's like a giant blob of numbers and no real significant naming values like that of a net config workstation (for finding a MAC address for example). This is why I'm rather lost when thinking of efficiency because it seems like something can always go wrong with my method.

As always, any help would be greatly appreciated!

Many thanks.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi everyone. I've been creating/deleting/modifying(mainly out of confusion and anger mind you) this program for the past 3 hours and I simply cannot find an efficient way to really get it to work.

What I'm trying to do is basically a 'netstat' (or netstat -n/B) and pick out any IP(s) containing the port :4000 and have it write to an INI file on my desktop for sort of like an archive. What's giving me great difficulty is the fact that I don't really know if what I'm doing would be the best choice. I've tried using the PID to find specific ports on the output text file but the PID always changes making it complicated. I've also tried finding the .EXE and going from there but they aren't in the same line via text output.

Can someone please help and/or guide me through what I have done and if there's any way to make it more efficient?

This is the output text file from which I'm trying to pick out any IP containing port 4000.

Active Connections

  Proto  Local Address        Foreign Address       State
  TCP   name-pc:1110           localhost:1119        ESTABLISHED
  TCP   name-pc:1119           localhost:1110        ESTABLISHED
  TCP   name-pc:2776           localhost:2777        ESTABLISHED
  TCP   name-pc:2777           localhost:2776        ESTABLISHED
  TCP   name-pc:2778           localhost:2779        ESTABLISHED
  TCP   name-pc:2779           localhost:2778        ESTABLISHED
  TCP   name-pc:1121           205.188.7.152:5190    ESTABLISHED
  TCP   name-pc:1130           cs124.msg.mud.yahoo.com:5050  ESTABLISHED
  TCP   name-pc:1205           gamesurge.com:6668  ESTABLISHED
  TCP   name-pc:1206           216.226.155.237:6668   ESTABLISHED
  TCP   name-pc:2867           63.240.202.131:6112  ESTABLISHED
  TCP   name-pc:2872           63.240.202.143:4000  ESTABLISHED

And this is the program I've written

RunWait(@ComSpec & " /c netstat > " & @TempDir & "\netstat.txt", "", @SW_HIDE)

If FileExists(@TempDir & "\netstat.txt") Then

$ipfile = FileOpen(@TempDir & "\netstat.txt", 0)

While 1
    $ipline = FileReadLine($ipfile)
    If @error = -1 Then ExitLoop
    $ipresult = StringInStr($ipline, ":4000")
    If $ipresult <> "0" Then
        $ipline = StringStripWS($ipline,8)
        IniWrite(@DesktopDir&"\test.ini","section1","key",$ipline)
        $ApplePie=StringTrimRight($ipline, 11)
        IniWrite(@DesktopDir&"\test.ini","section2","key",$ApplePie)
        $Coffee=StringTrimLeft($blah, 15)
        IniWrite(@DesktopDir&"\test.ini","section3","key",$Coffee)
        ExitLoop
    EndIf
Wend
EndIf

It's very hard to pick out things from the netstat commands because it's like a giant blob of numbers and no real significant naming values like that of a net config workstation (for finding a MAC address for example). This is why I'm rather lost when thinking of efficiency because it seems like something can always go wrong with my method.

As always, any help would be greatly appreciated!

Many thanks.

Try this:

#include <File.au3>
$LINES = _FileCountLines(@TempDir & "\netstat.txt")
$FILE = FileOpen(@TempDir & "\netstat.txt",0)
For $INDEX = 1 To $LINES
    $DATA = FileReadLine($FILE,$INDEX)
    If StringInStr($DATA,":4000") Then
        $STRING = StringStripWS($DATA,4)
        $IP = StringSplit($STRING," ")
        MsgBox(-1,"",$IP[4])
    EndIf
Next
FileClose($FILE)

Hope that helps!

Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Hi everyone. I've been creating/deleting/modifying(mainly out of confusion and anger mind you) this program for the past 3 hours and I simply cannot find an efficient way to really get it to work.

What I'm trying to do is basically a 'netstat' (or netstat -n/B) and pick out any IP(s) containing the port :4000 and have it write to an INI file on my desktop for sort of like an archive. What's giving me great difficulty is the fact that I don't really know if what I'm doing would be the best choice. I've tried using the PID to find specific ports on the output text file but the PID always changes making it complicated. I've also tried finding the .EXE and going from there but they aren't in the same line via text output.

Can someone please help and/or guide me through what I have done and if there's any way to make it more efficient?

This is the output text file from which I'm trying to pick out any IP containing port 4000.

Active Connections

  Proto  Local Address        Foreign Address       State
  TCP   name-pc:1110           localhost:1119        ESTABLISHED
  TCP   name-pc:1119           localhost:1110        ESTABLISHED
  TCP   name-pc:2776           localhost:2777        ESTABLISHED
  TCP   name-pc:2777           localhost:2776        ESTABLISHED
  TCP   name-pc:2778           localhost:2779        ESTABLISHED
  TCP   name-pc:2779           localhost:2778        ESTABLISHED
  TCP   name-pc:1121           205.188.7.152:5190    ESTABLISHED
  TCP   name-pc:1130           cs124.msg.mud.yahoo.com:5050  ESTABLISHED
  TCP   name-pc:1205           gamesurge.com:6668  ESTABLISHED
  TCP   name-pc:1206           216.226.155.237:6668   ESTABLISHED
  TCP   name-pc:2867           63.240.202.131:6112  ESTABLISHED
  TCP   name-pc:2872           63.240.202.143:4000  ESTABLISHED

And this is the program I've written

RunWait(@ComSpec & " /c netstat > " & @TempDir & "\netstat.txt", "", @SW_HIDE)

If FileExists(@TempDir & "\netstat.txt") Then

$ipfile = FileOpen(@TempDir & "\netstat.txt", 0)

While 1
    $ipline = FileReadLine($ipfile)
    If @error = -1 Then ExitLoop
    $ipresult = StringInStr($ipline, ":4000")
    If $ipresult <> "0" Then
        $ipline = StringStripWS($ipline,8)
        IniWrite(@DesktopDir&"\test.ini","section1","key",$ipline)
        $ApplePie=StringTrimRight($ipline, 11)
        IniWrite(@DesktopDir&"\test.ini","section2","key",$ApplePie)
        $Coffee=StringTrimLeft($blah, 15)
        IniWrite(@DesktopDir&"\test.ini","section3","key",$Coffee)
        ExitLoop
    EndIf
Wend
EndIf

It's very hard to pick out things from the netstat commands because it's like a giant blob of numbers and no real significant naming values like that of a net config workstation (for finding a MAC address for example). This is why I'm rather lost when thinking of efficiency because it seems like something can always go wrong with my method.

As always, any help would be greatly appreciated!

Many thanks.

I don't think there's anything wrong with what you've shown, except that I think you've used $blah where you meant $ApplePie I think.

I would have done something more like this (possibly)

#include <Constants.au3>

$foo = Run("cmd.exe", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
StdinWrite($foo, "netstat" & @CRLF)
StdinWrite($foo)

; Read from child's STDOUT 
Local $data
While True
    $data &= StdoutRead($foo)
    If @error Then ExitLoop
    Sleep(25)
WEnd
If $data <> '' Then
    $ipline = StringSplit(StringReplace($data, @LF, ''), @CR)

    For $n = 1 To $ipline[0]
    ;ConsoleWrite($ipline[$n] & @CRLF)
        $ipresult = StringInStr($ipline[$n], ":4000")
        If $ipresult <> "0" Then
            $ipline = StringStripWS($ipline[$n], 8)
            $ArrayLine = StringSplit(StringStripWS($ipline[$n], 4), ' ')
            If $ArrayLine[0] = 4 Then
                IniWrite(@DesktopDir & "\test.ini", "section1", "key", $ipline[$n])
                IniWrite(@DesktopDir & "\test.ini", "section2", "key", $ArrayLine[1] & $ArrayLine[2] & $ArrayLine[3])
                IniWrite(@DesktopDir & "\test.ini", "section3", "key", $ArrayLine[3])
            EndIf
        EndIf
    Next
EndIf

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This could be easily done with one line of regular expression:

#include <array.au3>

$i_port = 4000
; Substituted your file write for this
$i_pid = Run(@ComSpec & " /c netstat", "", @SW_HIDE, 6)
$s_text = ""
While Not @error
    $s_text &= StdoutRead($i_pid)
WEnd


$a_sre = StringRegExp($s_text, "(?s)(\d+\.\d+\.\d+\.\d+):" & $i_port, 3)
_ArrayDisplay($a_sre)

Edit:

Changed port number (didn't have anything running on port 4000 muttley )

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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