mIRC Home    About    Download    Register    News    Help

Print Thread
#201268 23/06/08 09:41 PM
Joined: May 2008
Posts: 329
A
AWEstun Offline OP
Fjord artisan
OP Offline
Fjord artisan
A
Joined: May 2008
Posts: 329
What's wrong with this code?

Code:
alias purgeip {
  set %a 1
  set %b $hget(iptracker,0).item
  echo 4 -a %b Total items in iptracker table.
  while (%a <= %b) {
    set %c $hget(iptracker,%a).item
    set %d $hget(iptracker,%a).data
    echo -a %a : %c is %d
    set %e 1
    set %f $numtok(%d,32)
    while (%e <= %f) {
      set %g $gettok(%d,%e,32)
      if (!$hget(dns,%g)) { hadd -s iptracker %c $remtok(%d,%g,32) | set %total $calc(%total + 1) }
      inc %e
    }
    inc %a
  }
  echo 4 -a Finished processing %b items in iptracker table!  %total total data values purged!
  unset %a %b %c %d %e %f %g %total
}


This code searches each item's data (ip) in the iptracker has table and checks to see if there is a corresponding same ip (item) in the dns table. If there is not, the ip data value is deleted from the item in the iptracker table.

The problem is that it's not removing all of the ip data values in the iptracker table that don't have a matching ip (item) in the dns table. I'm having to run the alias multiple times and each time it's finding less and less total amount of matches to be removed.

iptracker ex.:

itemname 1.2.3.4 2.3.4.5 3.4.5.6

dns ex.:

2.3.4.5 machine57.hardware.cc
1.2.3.4 green7.newrun.jp

The code above should $remtok 3.4.5.6 from itemname


I registered; you should too.
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
First thing I noticed, is the fact that your $remtok is missing a parameter

Here's my recommendation for an alternative code, but please note that this has not been tested.
Code:
alias purgeip {
  var %a = 1, %b = $hget(iptracker,0).item
  while %a <= %b {
    var %ip.item = $hget(iptracker,%a).item, %ip.data = $hget(iptracker,%a), %c = $numtok(%ip.data,32)
    while %c {
      var %dns = $gettok(%ip.data,%c,32)
      if !$hfind(dns,%dns,0) {        %ip.data = $remtok(%ip.data,%dns,1,32)      }
      dec %c
    }
    if !%ip.data {
      .hdel iptracker %ip.item
    }
    else {
       .hadd -m iptracker %ip.item %ip.data
    }
    inc %a
  }
}

Last edited by RusselB; 23/06/08 11:59 PM.
Joined: May 2008
Posts: 329
A
AWEstun Offline OP
Fjord artisan
OP Offline
Fjord artisan
A
Joined: May 2008
Posts: 329
In your version, wouldn't inc %c be dec %c? Since you want the while loop to process $numtok times and if you keep inc %c, it would go on for ever?

So I should use $hfind instead of $hget, so the table is accessed more efficiently, right?

Wouldn't it be better to only do the hadd if with the %remtok, if no item match is found in the dns table? The way you have it, hadd is always processed, even if an item match is found in the dns table, right?

Also, thanks for reminding me about removing the item if there are no data values left for it.


I registered; you should too.
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Good catch on the inc/dec error. In fact, with it starting at the maximum, then increasing, it would only run once, not forever.

I recommended using $hfind rather than $hget to save you from having to loop through the dns hash table each time. In this case, $hfind is more efficient than $hget since it incorporates the search rather than having to get the item then compare it.

The hadd (before altering the code) was always processed so that any remaining dns values that were in the match string, but not the dns table would be maintained.

Joined: May 2008
Posts: 329
A
AWEstun Offline OP
Fjord artisan
OP Offline
Fjord artisan
A
Joined: May 2008
Posts: 329
Originally Posted By: RusselB
Good catch on the inc/dec error. In fact, with it starting at the maximum, then increasing, it would only run once, not forever.

I recommended using $hfind rather than $hget to save you from having to loop through the dns hash table each time. In this case, $hfind is more efficient than $hget since it incorporates the search rather than having to get the item then compare it.

The hadd (before altering the code) was always processed so that any remaining dns values that were in the match string, but not the dns table would be maintained.


Yup, with dec and counting down it would only run once, but with inc it was going to run forever.

In my code it only did hadd if the item name didn't also exist in the dns table. If it didn't then and only then it would remove the orphan ip data value and profess the hadd, all in one line. (I see you changed your code to only hadd when the data was changed, instead of always hadd.)

Also, shouldn't %ip.data = $hget(iptracker,%a) be

%ip.data = $hget(iptracker,%a).data

or

%ip.data = $hget(iptracker,%ip.item)

???


I registered; you should too.
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Once again you have caught a mistake in my code.
You're right that it should be %ip.data = $hget(iptracker,%ip.item)

I can't edit my earlier code, so here's the re-write.. hopefully this has everything correct.
Code:
alias purgeip {
  var %a = 1, %b = $hget(iptracker,0).item
  while %a <= %b {
    var %ip.item = $hget(iptracker,%a).item, %ip.data = $hget(iptracker,%ip.item), %c = $numtok(%ip.data,32)
    while %c {
      var %dns = $gettok(%ip.data,%c,32)
      if !$hfind(dns,%dns,0) {        %ip.data = $remtok(%ip.data,%dns,1,32)      }
      dec %c
    }
    if !%ip.data {
      .hdel iptracker %ip.item
    }
    else {
       .hadd -m iptracker %ip.item %ip.data
    }
    inc %a
  }
}


As to the loop, counting down, it will run once for each IP address that is stored in the data section of the IPTracker hash table.

eg:
ip.data = 12.45.67.89 45.67.89.45 123.456.789.123 222.222.222.222
loop would count from 4 to 0 then end, with the dec, however, with the inc, it would've counted from 4 to 5 then stopped, since there isn't a 5th item in the data.

Joined: May 2008
Posts: 329
A
AWEstun Offline OP
Fjord artisan
OP Offline
Fjord artisan
A
Joined: May 2008
Posts: 329
Here is what I currently have. In order to properly check the iptracker data values for items in the dns table, I had to change the data value in $hfind to a wild card seach.

I am comparing the original data string for the item to the result string, if it changed, then hadd the item with the new data string. Although this doesn't really have to be done, since the hadd is inside $hfind.

When an item is hdel from the iptracker table, the item count for iptable needs to be dec, else at the end the loop will run an additional number of times items were hdel, for items that don't exist, as they were hdel. Likewise for the $numtok count for the number of tokens in the data, when one is removed. Else the loop will process more time then the number of tokens in the data for the item.

Code:
alias purgeip {
  set %total 1
  set %a 1
  set %b $hget(iptracker,0).item
  echo 4 -a %b Total items in iptracker table.
  while (%a <= %b) {
    set %c $hget(iptracker,%a).item
    set %d $hget(iptracker,%c)
    echo -a %a : %c is %d
    set %e 1
    set %f $numtok(%d,32)
    while (%e <= %f) {
      set %g $gettok(%d,%e,32)
      if (!$hfind(dns,%g,0)) {
        set %d $remtok(%d,%g,1,32)
        dec %f
        if (!%d) { hdel -s iptracker %c | dec %b }
        elseif (%d) { hadd -s iptracker %c %d }
        inc %total
      }
      inc %e
    }
    inc %a
  }
  echo 4 -a Finished processing %b items in iptracker table!  %total total data values purged!
  unset %a %b %c %d %e %f %g %h %total
}


I registered; you should too.
Joined: May 2008
Posts: 329
A
AWEstun Offline OP
Fjord artisan
OP Offline
Fjord artisan
A
Joined: May 2008
Posts: 329
This 'bitch' still isn't getting all the 'stale' data ip's in just one run. Why is that!?


I registered; you should too.
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Sorry, but I can't see why your code isn't getting it all in one run, but as it isn't, here's another suggestion, although this one sort of works in a backwards method, by getting the ip from the dns hash, then searching for it in the iptracker hash, and if found, deletes it. Hopefully this will work to give you the results you're wanting, even if it's not in the same method that you were trying.
Code:
alias ippurge {
  var %a = 1, %b = $hget(dns,0).item
  while %a <= %b {
    var %ip = $hget(dns,%a).item, %ip.item = $hfind(iptracker,%ip,0,w).data
    if %ip.item {
      var %ip.data = $remtok($hget(iptracker,%ip.item),%ip,1,32)
      if %ip.data {        .hadd -m iptracker %ip.item %ip.data      }
      else {        .hdel iptracker %ip.item      }
    }
    inc %a
  }
}

Joined: May 2008
Posts: 329
A
AWEstun Offline OP
Fjord artisan
OP Offline
Fjord artisan
A
Joined: May 2008
Posts: 329
Originally Posted By: RusselB
Sorry, but I can't see why your code isn't getting it all in one run, but as it isn't, here's another suggestion, although this one sort of works in a backwards method, by getting the ip from the dns hash, then searching for it in the iptracker hash, and if found, deletes it. Hopefully this will work to give you the results you're wanting, even if it's not in the same method that you were trying.


I appreciate the suggestion, but the reason that I'm pulling IP's from the item in the iptracker table is to check if there is a dns item match for the IP. If not, remove that IP from the item in the iptracker table and save the resulting data back to the item in the iptracker table. If after removing a non-matching data value from an item in the iptracker table and the resulting data is $null then delete the item from the iptracker table. If there is a match, go to the next IP for the item in the iptracker table.


I registered; you should too.
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
The way my latest code works, is that it pulls the address from the DNS table, then checks if the address is in the IPTRACKER table, and if it is, then it removes that address from the IPTRACKER table. This means that the maximum number of searches matches the number of items in the DNS table, rather than having to do a search for each tokenized item that is in the IPTRACKER table.

Realistically it should work and provide the results you're looking for, even though it does it in a different way than what you were thinking.


Link Copied to Clipboard