Community discussions

MikroTik App
 
lukkes
Member Candidate
Member Candidate
Topic Author
Posts: 177
Joined: Mon Jun 16, 2008 2:12 am
Location: Venezuela
Contact:

working, assign to array var the ip to compare later change

Mon Nov 12, 2012 8:13 pm

god morning,

in this script i scan all the interfaces that have name "WANxxx" like WAN1, WAN2, and then obtain it's ip address, then put that ip in an array, make it global and later run script again to check if any has changed, it works fine, but i have a problem, when some interface has no ip address, the script it´s broken and aborted in the line ":set currip [/ip address get [find interface="$ifname"] address];", the problem is when some interface has no ip, this happens frequently when the DSL goes down and the ISP dhcp server cant be reached.

somebody have any idea of how can bypass this problem? any other solution? thanks


:global currentifnames;
:global currentips;
:global arraycurrentips;
:local arraynewips;
:local arraynewifnames; 
:local newips;
:local newifnames;
:local iface;
:local ifname;
:local currip;

:log info "Checking  interfaces.";


:foreach iface in=[/interface  ethernet find name~"wan" disabled=no ] do={ 
   :set ifname [ /interface ethernet get $iface name ];
   :set newifnames ($newifnames, $ifname);
   :log info $newifnames;
   :set currip [/ip address get [find interface="$ifname"] address];
   :set newips ($newips, $currip);
   :log info $newips;

}
   :set arraynewifnames [:toarray $newifnames];
   :set arraynewips [:toarray $newips];
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: working, assign to array var the ip to compare later ch

Wed Nov 21, 2012 7:25 pm

If the interface does not show up at all in the [/ip address] list, you could try checking to see if an IP address exists for each interface using the length... Replace the problematic line with:
:if ([:len [/ip address find interface="$ifname"]] != 0) do={
   :set currip [/ip address get [find interface="$ifname"] address];
} else={
   :set currip "no ip";
}

Who is online

Users browsing this forum: No registered users and 35 guests