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}"

No comments:

Post a Comment

Thanks for your valuable feedback.

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...