Return IP Octet Function
Posted: Tue May 20, 2014 1:02 am
ros code
# Usage: [$returnOctet <ip> <octet number (0-3)>] # Input an IP Address and 0-3 argument to return a specific octet number :global returnOctet do={ :if ($1="") do={ :error "You did not specify an IP Address."; } :if ($2="") do={ :error "You did not specify an octet to return."; } :if (($2>3) || ($2<0)) do={ :error "Octet argument out of range."; } :local decimalPos "0"; :local octet1; :local octet2; :local octet3; :local octet4; :set decimalPos [:find $1 "."]; :set octet1 [:pick $1 0 $decimalPos]; :set decimalPos ($decimalPos+1); :set octet2 [:pick $1 $decimalPos [:find $1 "." $decimalPos]]; :set decimalPos ([:find $1 "." $decimalPos]+1); :set octet3 [:pick $1 $decimalPos [:find $1 "." $decimalPos]]; :set decimalPos ([:find $1 "." $decimalPos]+1); :set octet4 [:pick $1 $decimalPos [:len $1]]; :if (($octet1<0 || $octet1>255) || ($octet2<0 || $octet2>255) || ($octet3<0 || $octet3>255) || ($octet4<0 || $octet4>255)) do={ :error "Octet out of range."; } :if ($2=0) do={ :return $octet1; } :if ($2=1) do={ :return $octet2; } :if ($2=2) do={ :return $octet3; } :if ($2=3) do={ :return $octet4; } }I'm on RouterOS 6.7. When I created this code everything works fine but since I converted it to a function, I'm having trouble figuring out why when I insert a :put directly after I set octet4 it shows the correct value but then the return value somehow gives me the value for $octet3. Arguments 0-2 all produce the expected results.