Community discussions

MikroTik App
 
anassar26
just joined
Topic Author
Posts: 10
Joined: Sun Aug 02, 2020 3:20 pm

help to solve issue in script " dns to address lists scripts "

Sat Aug 29, 2020 5:35 am

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
# NAME TYPE DATA TTL
4 S www.youtube.com A 216.239.38.120 1d
10 www.facebook.com CNAME star.c10r.facebook.com 44m31s
11 graph.facebook.com CNAME api.facebook.com 44m31s

# Script :
: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;
}}
 
anassar26
just joined
Topic Author
Posts: 10
Joined: Sun Aug 02, 2020 3:20 pm

Re: help to solve issue in script " dns to address lists scripts "

Sun Aug 30, 2020 4:14 pm

any help please
 
msatter
Forum Guru
Forum Guru
Posts: 2319
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: help to solve issue in script " dns to address lists scripts "

Sun Aug 30, 2020 5:25 pm

Try this:
:foreach i in=[/ip dns cache find name~("facebook"|"youtube") type="A"] 
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet, no Amazon/Cloudfront/AWS. However, 'happy' with giving money to Italy.

Running:
RouterOS 7RC4 and 6.49RC / Winbox 3.31 64bits
 
anassar26
just joined
Topic Author
Posts: 10
Joined: Sun Aug 02, 2020 3:20 pm

Re: help to solve issue in script " dns to address lists scripts "

Sun Aug 30, 2020 9:51 pm

Try this:
:foreach i in=[/ip dns cache find name~("facebook"|"youtube") type="A"] 
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;
}} 
[/quote]
You do not have the required permissions to view the files attached to this post.
 
msatter
Forum Guru
Forum Guru
Posts: 2319
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: help to solve issue in script " dns to address lists scripts "

Mon Aug 31, 2020 10:52 am

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"
	}
}
viewtopic.php?f=9&t=160442&p=788842&hil ... he#p788879

Yours with data instead of address and matching is now correctly name~"(facebook|youtube)", to try:
:foreach i in=[/ip dns cache find name~"(facebook|youtube)" ] do={
:local tmpAddress [/ip dns cache get $i data];
: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;
}} 
I assume that something changed since we can use also other types of DNS entries since a few versions.
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet, no Amazon/Cloudfront/AWS. However, 'happy' with giving money to Italy.

Running:
RouterOS 7RC4 and 6.49RC / Winbox 3.31 64bits
 
msatter
Forum Guru
Forum Guru
Posts: 2319
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: help to solve issue in script " dns to address lists scripts "  [SOLVED]

Tue Sep 01, 2020 10:49 pm

I assume it is working now?
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet, no Amazon/Cloudfront/AWS. However, 'happy' with giving money to Italy.

Running:
RouterOS 7RC4 and 6.49RC / Winbox 3.31 64bits
 
anassar26
just joined
Topic Author
Posts: 10
Joined: Sun Aug 02, 2020 3:20 pm

Re: help to solve issue in script " dns to address lists scripts "

Wed Sep 02, 2020 7:41 am

I assume it is working now?
Its working awesome
Thanks 👍😊
 
User avatar
Wangz
newbie
Posts: 30
Joined: Wed Apr 15, 2020 4:41 pm

Re: help to solve issue in script " dns to address lists scripts "

Fri Sep 11, 2020 10:37 pm

Tried this script and it crumbles my cpu lol. Is this normal?

Edit: I'm using Hap Ac2. Getting 30% cpu usage spike just by running a single script.
-Home NA
 
msatter
Forum Guru
Forum Guru
Posts: 2319
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: help to solve issue in script " dns to address lists scripts "

Mon Sep 14, 2020 10:40 am

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={};
}
NOT TESTED! So please confirm if this is better.
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet, no Amazon/Cloudfront/AWS. However, 'happy' with giving money to Italy.

Running:
RouterOS 7RC4 and 6.49RC / Winbox 3.31 64bits
 
elico
Frequent Visitor
Frequent Visitor
Posts: 51
Joined: Mon Nov 07, 2016 3:23 am

Re: help to solve issue in script " dns to address lists scripts "

Mon Dec 28, 2020 5:42 pm

I tried the script from the wiki at:
https://wiki.mikrotik.com/wiki/Manual:S ... c_websites

But it just didn't ran..
So I came up with:
:foreach i in=[/ip dns cache find name~"(youtube-ui.l.google.com|youtube.com|googlevideo.com)\$" && $type ~ "^(A|CNAME)\$" ] do={
    :local tmpAddress [/ip dns cache get $i data];
    :local cacheName [/ip dns cache get $i name] ;
    delay delay-time=10ms
    
    :if ( [/ip firewall address-list find where address=$tmpAddress list=YOUTUBE_DOMAINS ] = "") do={         
        :log info ("added entry: $cacheName  $tmpAddress");
        /ip firewall address-list add address=$tmpAddress list=YOUTUBE_DOMAINS comment=$cacheName;
    }
} 
And it works pretty nice.
My basic difference was the "$" which will only match for suffix and not the whole domain.
* Updated for Address type A and CNAME
 
anassar26
just joined
Topic Author
Posts: 10
Joined: Sun Aug 02, 2020 3:20 pm

Re: help to solve issue in script " dns to address lists scripts "

Wed Oct 13, 2021 9:21 am

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={};
}
NOT TESTED! So please confirm if this is better.
Best , Nice Job
 
msatter
Forum Guru
Forum Guru
Posts: 2319
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: help to solve issue in script " dns to address lists scripts "

Wed Oct 13, 2021 1:02 pm

Thank, it took a while I noticed. ;-)
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet, no Amazon/Cloudfront/AWS. However, 'happy' with giving money to Italy.

Running:
RouterOS 7RC4 and 6.49RC / Winbox 3.31 64bits

Who is online

Users browsing this forum: No registered users and 12 guests