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.
I appreciate the response rextended and the time you took to rewrite the function but I’d still like to know why the code that I wrote is not returning the proper results. I’ve upgraded to Router OS 6.13 and the issue still persists.
Following should fix your problem
:if ($2=“0”) do={ :return $octet1; }
:if ($2=“1”) do={ :return $octet2; }
:if ($2=“2”) do={ :return $octet3; }
:if ($2=“3”) do={ :return $octet4; }Sometimes it is not possible to guess correct type, so unexpected results may appear.
Function variable is a string and you are comparing it to integer.
Obviously the response is a year late or so but, to answer your question, all you do is copy and paste the ROS code above into whatever script your working on and as indicated in the usage statement above, you can call on it like this from inside your script:
It would return the first octet of “10” and print it to the terminal window. Also as noted above, the second argument 0-3 returns what octet you’re looking for. So in the case above, if you put the number 0 through 3, it would return “10”, “1”, “1”, or “20” respectively. It is very helpful in situations where you need to manipulate a specific octet of a variable that contains an IP address.
Or you can directly input the variable and it would output the same thing like this:
:put [$returnOctet 10.1.1.20 0]
Once you’ve ran the script one time, since it’s a global variable, you could then call it directly from the terminal the same way or use it in any other script for that matter as long as you declare “:global returnOctet;” to initialize the variable before you use it as long as you haven’t unset the variable by calling “:set returnOctet;” or rebooted your router since you ran the script. Global variables aren’t kept after a reboot.
To actually run a script there are several ways that I know of (probably more than this), by opening a terminal and typing “/system script run nameOfScript” or in Winbox go to System > Scripts > Select the script and hit Run Script but you won’t see any output that way.
how do I really use this script after creating a firewall filter. I just ran it but I had a few snags, so i did not get to a point where its supposed to check the dns blacklist.