hi all
i am using this script to collect YouTube & Facebook IP addresses in DNS cache ( for block or separate route )
but it is not working and get result “input does not match any value of value-name”
please help me to solve issue
i print dns cash to find Facebook and YouTube in cach and get result below but when i run script get “input does not match any value of value-name” :
[admin@MikroTik] /ip dns cache> print
Flags: S - static
:foreach i in=[/ip dns cache all find where (name~“facebook” || name~“youtube”) && (type=“A”) ] do={
:local tmpAddress [/ip dns cache get $i address];
delay delay-time=10ms #prevent script from using all cpu time
:if ( [/ip firewall address-list find where address=$tmpAddress] = “”) do={
:local cacheName [/ip dns cache get $i name] ;
:log info (“added entry: $cacheName $tmpAddress”);
/ip firewall address-list add address=$tmpAddress list=restricted comment=$cacheName;
}}
first many thanks
i try script after your modification but i got no result in address list
please help to get correct result “create list in address list for youtube and facebook” :
:foreach i in=[/ip dns cache find name~("facebook"|"youtube") ] do={
:local tmpAddress [/ip dns cache get $i address];
:local cacheName [/ip dns cache get $i name] ;
delay delay-time=10ms
:if ( [/ip firewall address-list find where address=$tmpAddress] = "") do={
:log info ("added entry: $cacheName $tmpAddress");
/ip firewall address-list add address=$tmpAddress list=restricted comment=$cacheName;
}}
An example and it contains the assumption that the IP address is labeled address but it data in real:
:foreach i in=[/ip dns cache all find where name~"tiktok" && static=no] do={
:local tmpIP [/ip dns cache get $i data]
if ([:len [/ip firewall address-list find where address=$tmpIP list=tiktok-hosts]]=0) do {
/ip firewall address-list add address=$tmpIP list=tiktok-hosts
:log info "added entry: $tmpIP"
}
}
You can optimize it a bit if you leave out the check and logging and then I can compress the write to one line:
:foreach i in=[/ip dns cache find name~"(facebook|youtube)" ] do={
:do {/ip firewall address-list add address=[/ip dns cache get $i data] list=restricted comment=[/ip dns cache get $i name]} on-error={};
}