Jump to content
Sign in to follow this  
Ajan

Splitting string, a little bit more complicated

Recommended Posts

Hello.

I'm working on a small program atm which at some place uses _RUNDos function to run netstat - n > file.txt in the background. As you see netstat writes its content to a file called file.txt. The file usuallu looks like this

TCP 83.22.50.162:1406     80.210.110.50:80      ESTABLISHED
  TCP   127.0.0.1:1042       127.0.0.1:1043      ESTABLISHED
  TCP   127.0.0.1:1043       127.0.0.1:1042      ESTABLISHED

Now, I would like to find a host where I'm connected on specified port (let's say here 80) and take only the last octet of the host (here it would be 50). Seems like I should use StringSplit but doesn't it split strings according to only one delimiter? Here I should use a tabulation delimiter shouldn't I?

Anyway I tried another option, I split the whole line using space as delimiter. It gave me an array with about 19 elements where one was "80.210.110.50:80". I had to split it again this time using dot "." as a delimiter which gave me an array where one of ements was "50:80" Then I did the same thing but this time with ":" and I got the thing I wanted. But that is a little bit complicated I guess, and for unknown reason sometimes doesn't work on other computers (error with elements of arrays).

So is there any less complicated way to do it?

I'd appreciate any help.

Share this post


Link to post
Share on other sites

I use stringSplit all the time, and it has it's limit to only use one character as a delimiter, but you can work around it. You could replace all the spaces with a single one, or a few other things.

You can split by the : first and search for your port that way, but most things I am thinking you will be using a few stringsplits.

One version to show what address a port is using.

$x=ClipGet()
for $i=1 to 5
$x=StringReplace($x,"  "," "); replace more than one space with a single one
Next
msgbox(1,"Clipboard shown",$x)
$data=Stringsplit($x,":")

$question=InputBox("What port?","Enter the port number","80")

for $i=1 to $data[0]
   if StringInstr($data[$i],$question & " ")>0 then 
      $address=StringTrimleft($data[$i-1],StringInStr($data[$i-1]," "))
      msgbox(1,"IP address", $address)
   EndIf
Next

I thought if I show you a different approach it might help.

here is one really different approach:

$x=ClipGet()
for $i=1 to 5
$x=StringReplace($x,"       ESTABLISHED","@"); replace more than one space with a single one
Next
msgbox(1,"Clipboard shown",$x); showing split
$data=Stringsplit($x,"@")

$question=":" & InputBox("What port?","Enter the port number","80")
for $i=1 to $data[0]
   if Stringright($data[$i],Stringlen($question))=$question Then
      $data[$i]=StringTrimright($data[$i],Stringlen($question))
      $t=Stringtrimleft($data[$i],Stringinstr($data[$i],".",0,-1))
      msgbox(1,"Last part of IP",$t)
   EndIf
Next

This one uses stringinstring with a few trims.

The main thing you need in any of the approches is to have consistant data. If using on more than one machine, get the netstat from each machine and make sure that the format is always the same.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

Thanks a lot. This should really help, I've got to thing about it and understand it though :lmao: . Anyway thanks again.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...