Community discussions

MikroTik App
 
User avatar
tplecko
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Mon Jun 11, 2007 12:18 pm
Location: Croatia
Contact:

why do i get an error here?

Wed Apr 27, 2011 11:40 am

:set hostname $[ip dhcp-server lease get [find address="192.168.1.$i"] host-name ];
I get an error here (red bold)

The script works if i remove the line completely (so there are no other errors and looping works fine. I just wanted to add hostnames in the report


Thanks
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7186
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 11:45 am

remove $ that is before [
 
User avatar
tplecko
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Mon Jun 11, 2007 12:18 pm
Location: Croatia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 12:07 pm

Same error
The line:
:set hname [ip dhcp-server lease get [ find address="192.168.1.$i" ] host-name ] ;

Error:
bad command name ip (line 13 column 25)



(cursor location indicator in the script editor would be very useful right about now :shock: )

BTW. version 5.1
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7186
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 12:13 pm

:set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name ];
 
User avatar
tplecko
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Mon Jun 11, 2007 12:18 pm
Location: Croatia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 12:28 pm

tryed it before ...
:set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] ;

interrupted
no such item

so i changed it to:
:if ([/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] = nil) do={ :set hname "none" } else={ :set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] }

and still the same error (no such item)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7186
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 12:37 pm

if "find" returns more than one item then you have to use :foreach to go through array of returned items.
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: why do i get an error here?

Wed Apr 27, 2011 12:45 pm

How is the variable $i assigned a value? Can you post the rest of the code in the loop?

ADD: I tried this and got no errors:
local hname;

:for i from=1 to=10 do={
    :set hname [/ip dhcp-server lease get [/ip dhcp-server lease find address="192.168.1.$i"] host-name ] ;
}
 
User avatar
tplecko
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Mon Jun 11, 2007 12:18 pm
Location: Croatia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 2:08 pm

Here is the code:
      :global traf;
       :global megsu "0";
       :global megsd "0";
       :global resulter "";
       :local logmsg;
       :local hname "";
       /queue tree
           :for i from=1 to=9 do={
                   :set logmsg "";
                   :set megsd "0";
                   :set megsu "0";
                   :if ([/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] = nil) do={ :set hname "none" } else={ :set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] }
                   :set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] ;
                   :if ([/queue tree find name="192.168.1.00$i_D"]!="") do={:set traf [get [find name="192.168.1.00$i_D"] bytes]; :set megsd ($traf / 1000);}
                   :if ([/queue tree find name="192.168.1.00$i_U"]!="") do={:set traf [get [find name="192.168.1.00$i_U"] bytes]; :set megsu ($traf / 1000);}
                   :if ($megsd > 0) do={
                       :if ($megsd > 1000) do={ :set megsd ($megsd / 1000); :set megsd "$megsd Mb" } else={ :set megsd "$megsd Kb"}
                       :set logmsg ("192.168.1.00$i_D: $megsd");
                       :set resulter ("$resulter \n User: $hname 192.168.1.00$i Download: $megsd")
                   }
                   :if ($megsu > 0) do={
                       :if ($megsu > 1000) do={ :set megsu ($megsu / 1000); :set megsu "$megsu Mb" } else={ :set megsu "$megsu Kb"}
                       :if ($logmsg!="") do={:set resulter ("$resulter Upload: $megsu")} else={:set resulter ("$resulter \n User: $hname 192.168.1.00$i Upload: $megsu")}
                       :if ($logmsg!="") do={ :set logmsg ("$logmsg 192.168.1.00$i_U: $megsu"); } else={ :set logmsg ("192.168.1.00$i_U: $megsu"); }
                   }
                   :if ($logmsg!="") do={ :log info ($logmsg) }
               }
All queues exist (they are static) but not all IP's are assigned via DHCP (wich is the mikrotik itself), some are static (and i figure, i'll get an empty string for those)
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: why do i get an error here?

Wed Apr 27, 2011 2:12 pm

You are not including the full path on the "find" requests.
Don't use [find address="192.168.1.$i"]
Use [/ip dhcp-server lease find address="192.168.1.$i"]
Same applies to the rest of the "find" requests in the code.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7186
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 2:18 pm

full path is not required there, find will use current path.
For example in
[/ip dhcp-server lease get [find address="192.168.1.$i"] host-name]
current path is set to "/ip dhcp-server lease" and find will also use it.

Regarding tplecko problem:
Does entries with addresses "192.168.1.1-192.168.1.9" exist in dhcp lease menu?
 
User avatar
tplecko
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Mon Jun 11, 2007 12:18 pm
Location: Croatia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 2:26 pm

Regarding tplecko problem:
Does entries with addresses "192.168.1.1-192.168.1.9" exist in dhcp lease menu?
Not all IP addresses will exist. 1-5 are static, 6-9 are dynamic. currently 1,2,5,7,8 and 9 exist (but in dhcp lease list, i can see 1,2 and 5 as busy and with no hostname)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7186
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 2:36 pm

then you have an answer
if [find address="192.168.1.$i"] is unable to find anything then getis trying to get hostname from nothingand you get "no such item"

Try
:if (/ip dhcp-server lease find address="192.168.1.$i" != "") do={
:set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name]
}
 
User avatar
tplecko
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Mon Jun 11, 2007 12:18 pm
Location: Croatia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 2:39 pm

As i submited the post, i realized just that!
But i have just upgraded my ubuntu to 11.04 and crossover stopped working so testing this will wait until i fix my current problem!

Thanks for helping!
 
User avatar
tplecko
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Mon Jun 11, 2007 12:18 pm
Location: Croatia
Contact:

Re: why do i get an error here?

Wed Apr 27, 2011 3:37 pm

If anyone is interested:

Queue tree is quite simple. For sorting purposes, names of items are IP addresses ending with 3 digit number (meaning 192.168.1.1 rule has name 192.168.1.001)
This results in 3 for-loops since i check the name of queue items (i have an idea to fix this but am lazy to do it)
I have one for the complete /24 range for upload and for download.
This script reports bandwidth usage per IP address. It reports the hostname too if the DHCP is on the same device.
Unfortunatley i don'tt know how and if mikrotik can get hostnames from IP addresses any other way. (note for another feature request)

Here is the script:
:log error "Queue Stats Start"
:local traf;
:local megsu "0";
:local megsd "0";
:local resulter "";
:local logmsg;
:local hname "";
/queue tree
    :for i from=1 to=9 do={
        :set logmsg "";
        :if ( [/ip dhcp-server lease find address="192.168.1.$i"] != "" ) do={ 
            :if ( [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] != "") do={
                :set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] 
            } else={ 
                :set hname "unknown"
            }
        } else={ 
            :set hname "n/a" 
        }
        :if ([/queue tree find name="192.168.1.00$i_D"]!="") do={:set traf [get [find name="192.168.1.00$i_D"] bytes]; :set megsd ($traf / 1000);}
        :if ([/queue tree find name="192.168.1.00$i_U"]!="") do={:set traf [get [find name="192.168.1.00$i_U"] bytes]; :set megsu ($traf / 1000);}
        :if ($megsd > 0) do={
            :if ($megsd > 1000) do={ :set megsd ($megsd / 1000); :set megsd "$megsd Mb" } else={ :set megsd "$megsd Kb"}
            :set logmsg ("192.168.1.$i Down: $megsd");
            :set resulter ("$resulter \n IP: 192.168.1.00$i Host: $hname Down: $megsd")
        }
        :if ($megsu > 0) do={
            :if ($megsu > 1000) do={ :set megsu ($megsu / 1000); :set megsu "$megsu Mb" } else={ :set megsu "$megsu Kb"}
            :if ($logmsg!="") do={ 
                :set resulter ("$resulter Up: $megsu") 
                :set logmsg ("$logmsg Up: $megsu")
            } else={ 
                :set resulter ("$resulter \n Ip: 192.168.1.00$i Host: $hname Up: $megsu")
                :set logmsg ("192.168.1.$i Up: $megsu"); 
            }
        }
        :if ($logmsg!="") do={ :log info ($logmsg) }
    }

    :for i from=10 to=99 do={
        :set logmsg "";
        :if ( [/ip dhcp-server lease find address="192.168.1.$i"] != "" ) do={ 
            :if ( [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] != "") do={
                :set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] 
            } else={ 
                :set hname "unknown"
            }
        } else={ 
            :set hname "n/a" 
        }
        :if ([/queue tree find name="192.168.1.0$i_D"]!="") do={:set traf [get [find name="192.168.1.0$i_D"] bytes]; :set megsd ($traf / 1000);}
        :if ([/queue tree find name="192.168.1.0$i_U"]!="") do={:set traf [get [find name="192.168.1.0$i_U"] bytes]; :set megsu ($traf / 1000);}
        :if ($megsd > 0) do={
            :if ($megsd > 1000) do={ :set megsd ($megsd / 1000); :set megsd "$megsd Mb" } else={ :set megsd "$megsd Kb"}
            :set logmsg ("192.168.1.$i Down: $megsd");
            :set resulter ("$resulter \n IP: 192.168.1.0$i Host: $hname Down: $megsd")
        }
        :if ($megsu > 0) do={
            :if ($megsu > 1000) do={ :set megsu ($megsu / 1000); :set megsu "$megsu Mb" } else={ :set megsu "$megsu Kb"}
            :if ($logmsg!="") do={ 
                :set resulter ("$resulter Up: $megsu") 
                :set logmsg ("$logmsg Up: $megsu")
            } else={ 
                :set resulter ("$resulter \n Ip: 192.168.1.0$i Host: $hname Up: $megsu")
                :set logmsg ("192.168.1.$i Up: $megsu"); 
            }
        }
        :if ($logmsg!="") do={ :log info ($logmsg) }
    }

    :for i from=100 to=254 do={
        :set logmsg "";
        :if ( [/ip dhcp-server lease find address="192.168.1.$i"] != "" ) do={ 
            :if ( [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] != "") do={
                :set hname [/ip dhcp-server lease get [find address="192.168.1.$i"] host-name] 
            } else={ 
                :set hname "unknown"
            }
        } else={ 
            :set hname "n/a" 
        }
        :if ([/queue tree find name="192.168.1.$i_D"]!="") do={:set traf [get [find name="192.168.1.$i_D"] bytes]; :set megsd ($traf / 1000);}
        :if ([/queue tree find name="192.168.1.$i_U"]!="") do={:set traf [get [find name="192.168.1.$i_U"] bytes]; :set megsu ($traf / 1000);}
        :if ($megsd > 0) do={
            :if ($megsd > 1000) do={ :set megsd ($megsd / 1000); :set megsd "$megsd Mb" } else={ :set megsd "$megsd Kb"}
            :set logmsg ("192.168.1.$i Down: $megsd");
            :set resulter ("$resulter \n IP: 192.168.1.$i Host: $hname Down: $megsd")
        }
        :if ($megsu > 0) do={
            :if ($megsu > 1000) do={ :set megsu ($megsu / 1000); :set megsu "$megsu Mb" } else={ :set megsu "$megsu Kb"}
            :if ($logmsg!="") do={ 
                :set resulter ("$resulter Up: $megsu") 
                :set logmsg ("$logmsg Up: $megsu")
            } else={ 
                :set resulter ("$resulter \n Ip: 192.168.1.$i Host: $hname Up: $megsu")
                :set logmsg ("192.168.1.$i Up: $megsu"); 
            }
        }
        :if ($logmsg!="") do={ :log info ($logmsg) }
    }

/tool e-mail send body="$[/system identity get name] queue stats report on: $[/system clock get date]@$[/system clock get time]\n\n$resulter" subject="Queue stats" to="me@myserver.com" from="$[/system identity get name]@myserver.com" ;
/queue tree reset-counters-all ;
:log error "Queue Stats End"
Scheduler runs this script every midnight. The script, as is, takes about 1 minute to execute on a pentium computer so be careful if putting on a routerboard (especially since it requires 500 mangle rules and 500 queue tree rules)

It will end up on the wiki once i removed the excess code 8-)

Who is online

Users browsing this forum: No registered users and 3 guests