Tuesday, 23 May 2023

Some Mikrotik Script Trics that can make your boring work easy

Some Mikrotik Script Trics that can make your boring work easy 

>>Change Multiple pppoe profile at once:

{
/ppp secret
:foreach id in=[find where profile="Profile-A"] do={
	set $id profile="Profile-B"
}
}

if the profile shows unknown then use the profile name = "*8"

>>Move Multiple vlan to nother interface:

foreach i in=[/interface vlan find where interface=ether2] do={interface vlan set interface=ether3 $i}


>>Create Multiple vlan in a interface:

:for i from 1 to 50 do={/ int vlan add name=("vlan" . $i) vlan-id=$i interface=ether1}


>>Creating multiple queue in mikrotik at once:

:for x from 1 to 254 do={/queue simple add name="queue-$x" max-limit=2M/2M target="192.168.1.$x"}


>>Creating multiple PPPOE user at once:

:for i from 1 to 50 do={/ppp secret add name=("user" . $i) password=("pass" . $i) service=pppoe profile=1MB}


>>> Mikrotik firewall filter rule moving to the desired sequence number

ip firewall filter move [ /ip firewall filter find src-address-list="80_Mbps" ] "3"


>>Mikrotik Login LOG to Telegram

:local DeviceName [/system identity get name];

:local Time [/system clock get time];

:local Date [/system clock get date];

:local TelegramMessageText;

:global ParseLogAccountEndArrayID;

:local IDsEventsAccount [/log find where topics ~ "account"];

:local LenArrayIDs [:len $IDsEventsAccount];

:local StartArrayID [:find $IDsEventsAccount $ParseLogAccountEndArrayID];

:local EndArrayID ($IDsEventsAccount -> ($LenArrayIDs-1));

:if ($EndArrayID != $ParseLogAccountEndArrayID and [:tobool $ParseLogAccountEndArrayID] ) do={

:local StartArray [:find $IDsEventsAccount $ParseLogAccountLastRunID];

:for KeyArray from=($StartArrayID+1) to=($LenArrayIDs-1) do={

:local IDMessage ($IDsEventsAccount ->$KeyArray );

:set TelegramMessageText "$TelegramMessageText %0D%0A $[/log get number=$IDMessage time] - $[/log get number=$IDMessage message];";

}

# START SEND TELEGRAM MESSAGE

:local BotToken "6120514855:AAFIkwye7DKQ0w92O5bkT2YkR-r3vMnHcCk";

:local ChatID "-905535349";

:local ParseMode "html";

:local DisableWebPagePreview True;

:local SendText "\F0\9F\94\93 <b>$DeviceName: AUTH</b> $TelegramMessageText";

:local tgUrl "https://api.telegram.org/bot$BotToken/sendMessage?chat_id=$ChatID&text=$SendText&parse_mode=$ParseMode&disable_web_page_preview=$DisableWebPagePreview";

/tool fetch http-method=get url=$tgUrl keep-result=no;

# END SEND TELEGRAM MESSAGE

}

:set ParseLogAccountEndArrayID $EndArrayID;



>>Doube NAT Restrict

/ip firewall filter

 add action=add-src-to-address-list address-list=double_router \

    address-list-timeout=15m chain=forward connection-state=established \

    in-interface=all-ppp src-address=10.70.0.0/16 ttl=less-than:62

/ip firewall filter

add action=drop chain=forward connection-state=new src-address-list=\

    double_router ttl=less-than:62



>>Mikrotik Backup send to EMAIl:

/system scheduler
add interval=1d name=autobackup on-event=Mikrotik-autobackup policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=mar/22/2023 start-time=22:35:00
/system script
add dont-require-permissions=no name=Mikrotik-autobackup owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\
    log info \"Mikrotik Backup is started if old file found then it will delet\
    e\";\r\
    \n/export file=backup;\r\
    \n/system backup save name=backup;\r\
    \n:delay 5s\r\
    \n/tool e-mail send to=\" shhrlslm@gmail.com\" subject=\"\$[/system identi\
    ty get name] [\$[/system clock get date] - \$[/system clock get time]] Bac\
    kup file\" \\  body=\"\r\
    \nDear Team,\r\
    \n\r\
    \nBellow attached file is your Mikrotik Configuration Backup File. At the \
    Date: \$[/system clock get date] \r\
    \n\r\
    \nThanks\r\
    \nShaharul\r\
    \n\" file=backup.rsc,backup.backup;\r\
    \n:log info \"Mikrotik Backup is Emailed to  shhrlslm@gmail.com\";\r\
    \n:delay 5s\r\
    \n:log info \"OLD file removing..\";\r\
    \n/file remove backup.rsc;\r\
    \n/file remove backup.backup;\r\
    \n:log info \"Removed Thanks For Using this script shaharul Islam\";"
/tool e-mail
set address=74.125.68.109 from=automikrotikbackup247@gmail.com password=\
    wvaguvzkadqevgem port=587 start-tls=yes user=\
    automikrotikbackup247@gmail.com

>>OR


:local email "shaharul884@gmail.com"

:local rscfn "backup"

:local bakfn "bin"

/system

:local date [clock get date]

:local time [clock get time]

:local info [identity get name]

:local board ([resource get board-name])

:local files "$rscfn.rsc,$bakfn.backup"

:local Version [resource get version]

/system

:local serial "undefined"

:if (!($board~"(x86|CHR)")) do={

    :global testrbsn "NO RouterBOARD"

    :execute ":global testrbsn; :set testrbsn [/system routerboard get serial-number]"

    :delay 1s

    :set serial $testrbsn

    :set testrbsn

} else={

    :if ($board="x86") do={:set serial [license get software-id]}

    :if ($board="CHR") do={:set serial [license get system-id]}

}

:if ($Version~"^7") do={

[:parse "/export show-sensitive file=$rscfn.rsc"]

} else={

/export file="$rscfn.rsc"

}

:delay 2s

/system backup save name="$bakfn"

:delay 2s

/tool e-mail send to="$email" subject="Mikrotik: Backup $info $Version $serial" file="$files" body="Automatic Backup of $info $serial at $date $time"

:delay 20s

:execute "/file remove $files"

:log info "Backup router=$info serial=$serial ok"


Router OS-7 BGP community filter rule configure


/routing filter rule
add chain=Baisan-IN disabled=no rule="if (dst in 0.0.0.0/0){append bgp-communities 64500:11; accept}"
add chain=BAISNA-OUT-Discard disabled=no rule="if (dst in 0.0.0.0/0 && bgp-communities equal 64500:11){reject} else {accept}"

A complete Mikrotik Script to configure

A complete Mikrotik Script to configure Vlan wise service, INT, GGC, FNA, BDIX. With SRC NAT, Queue, PCQ, IP poole, PPPOE server, PPPOE profile, Configure BGP, Static Route


/interface bridge

add name=Loopback


/interface vlan

add interface=ether1 name=REGO-BDIX vlan-id=1214

add interface=ether1 name=REGO-FNA vlan-id=1213

add interface=ether1 name=REGO-GGC vlan-id=1212

add interface=ether1 name=REGO-INT vlan-id=1211


/ip address

add address=172.20.6.10/30 interface=REGO-INT

add address=172.22.6.10/30 interface=REGO-GGC

add address=172.24.6.10/30 interface=REGO-FNA

add address=172.26.6.10/30 interface=REGO-BDIX

add address=103.130.16.160 interface=Loopback



/ip dns

set servers=103.167.15.150


/ip firewall nat

add action=src-nat chain=srcnat out-interface=REGO-INT to-addresses=\ 103.130.16.160

add action=src-nat chain=srcnat out-interface=REGO-GGC to-addresses=\ 103.130.16.160

add action=src-nat chain=srcnat out-interface=REGO-FNA to-addresses=\ 103.130.16.160

add action=src-nat chain=srcnat out-interface=REGO-BDIX to-addresses=\ 103.130.16.160



/ip route

add check-gateway=ping distance=1 gateway=172.20.6.9


add dst-address=103.123.171.0/27 gateway=172.22.6.9 comment=GGC check-gateway=ping

add dst-address=103.166.252.96/27 gateway=172.22.6.9 comment=GGC check-gateway=ping

add dst-address=103.166.252.128/25 gateway=172.22.6.9 comment=GGC check-gateway=ping


add dst-address=103.123.170.0/26 gateway=172.24.6.9 comment=FNA check-gateway=ping

add dst-address=103.167.14.0/26 gateway=172.24.6.9 comment=FNA check-gateway=ping



/routing bgp instance

add as=64500 name=REGO-BDIX router-id=172.26.6.10


/routing bgp network

add network=103.130.16.160 synchronize=no


/routing bgp peer

add instance=REGO-BDIX name=REGO-BDIX remote-address=172.26.6.9 remote-as=\ 138346



/ip pool

add name=1MB ranges=10.10.0.2-10.10.1.254

add name=2MB ranges=10.10.2.2-10.10.3.254

add name=3MB ranges=10.10.4.2-10.10.5.254

add name=5MB ranges=10.10.6.2-10.10.7.254

add name=8MB ranges=10.10.8.2-10.10.9.254

add name=10MB ranges=10.10.10.2-10.10.11.254

add name=12MB ranges=10.10.12.2-10.10.13.254

add name=15MB ranges=10.10.14.2-10.10.15.254

add name=20MB ranges=10.10.16.2-10.10.17.254


/ppp profile

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.0.1 name=1MB \  only-one=no remote-address=1MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.2.1 name=2MB \ only-one=no remote-address=2MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.4.1 name=3MB \ only-one=no remote-address=3MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.6.1 name=5MB \ only-one=no remote-address=5MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.8.1 name=8MB \ only-one=no remote-address=8MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.10.1 name=10MB \ only-one=no remote-address=10MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.12.1 name=12MB \ only-one=no remote-address=12MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.14.1 name=15MB \ only-one=no remote-address=15MB

add dns-server=103.167.15.150,8.8.8.8 local-address=10.10.16.1 name=20MB \ only-one=no remote-address=20MB



/queue type

add kind=pcq name=3MB pcq-classifier=dst-address pcq-rate=3M

add kind=pcq name=5MB pcq-classifier=dst-address pcq-rate=5M

add kind=pcq name=8MB pcq-classifier=dst-address pcq-rate=8M

add kind=pcq name=10MB pcq-classifier=dst-address pcq-rate=10M

add kind=pcq name=12MB pcq-classifier=dst-address pcq-rate=12M

add kind=pcq name=20MB pcq-classifier=dst-address pcq-rate=20M

add kind=pcq name=All-Upload pcq-classifier=src-address pcq-rate=50M

add kind=pcq name=1MB pcq-classifier=dst-address pcq-rate=1M

add kind=pcq name=2MB pcq-classifier=dst-address pcq-rate=2M

add kind=pcq name=15MB pcq-classifier=dst-address pcq-rate=15M

add kind=pcq name=GGC-50M pcq-classifier=src-address,dst-address pcq-limit=\ 500KiB pcq-rate=50M pcq-total-limit=200000KiB

add kind=pcq name=FNA-50M pcq-classifier=src-address,dst-address pcq-limit=\ 500KiB pcq-rate=50M pcq-total-limit=200000KiB

add kind=pcq name=BDIX-50M pcq-classifier=src-address,dst-address pcq-limit=\ 500KiB pcq-rate=50M pcq-total-limit=200000KiB



/queue simple

add dst=REGO-BDIX max-limit=1G/1G name=BDIX queue=BDIX-50M/BDIX-50M target=""

add dst=REGO-GGC max-limit=1G/1G name=GGC queue=GGC-50M/GGC-50M target=""

add dst=REGO-FNA max-limit=1G/1G name=FNA queue=FNA-50M/FNA-50M target=""

add max-limit=1G/1G name=1MB-Package queue=All-Upload/1MB target=10.10.0.0/23

add max-limit=1G/1G name=2MB-Package queue=All-Upload/2MB target=10.10.2.0/23

add max-limit=1G/1G name=3MB-Package queue=All-Upload/3MB target=10.10.4.0/23

add max-limit=1G/1G name=5MB-Package queue=All-Upload/5MB target=10.10.6.0/23

add max-limit=1G/1G name=8MB-Package queue=All-Upload/5MB target=10.10.8.0/23

add max-limit=1G/1G name=10MB-Package queue=All-Upload/10MB target=\ 10.10.10.0/23

add max-limit=1G/1G name=12MB-Package queue=All-Upload/12MB target=\ 10.10.12.0/23

add max-limit=1G/1G name=15MB-Package queue=All-Upload/15MB target=\ 10.10.14.0/23

add max-limit=1G/1G name=20MB-Package queue=All-Upload/20MB target=\ 10.10.16.0/23

Comprehensive IP Calculator: Supporting Both IPv4 and IPv6

Download:  Download Whether you're a network engineer, IT professional, or simply a tech enthusiast, understanding IP addresses is cruci...