Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
RBScript 14.63 KB | None | 0 0
  1. # jul/26/2017 22:14:10 by RouterOS 6.40
  2. # software id = XXX-XXXXX
  3. #
  4.             script: :global ssid;
  5.                     #| RouterMode:
  6.                     #|  * WAN port is protected by firewall and enabled DHCP client
  7.                     #|  * Wireless and Ethernet interfaces (except WAN port ether1)
  8.                     #|    are part of LAN bridge
  9.                     #| wlan1 Configuration:
  10.                     #|     mode:          ap-bridge;
  11.                     #|     band:          2ghz-b/g/n;
  12.                     #|     ht-chains:     0,1;
  13.                     #|     ht-extension:  20/40mhz-Ce;
  14.                     #| LAN Configuration:
  15.                     #|     IP address 192.168.88.1/24 is set on bridge (LAN port)
  16.                     #|     DHCP Server: enabled;
  17.                     #|     DNS: enabled;
  18.                     #| WAN (gateway) Configuration:
  19.                     #|     gateway:  ether1 ;
  20.                     #|     ip4 firewall:  enabled;
  21.                     #|     NAT:   enabled;
  22.                    
  23.                     :log info Starting_defconf_script_;
  24.                     :global action;
  25.                     #-------------------------------------------------------------------------------
  26.                     # Apply configuration.
  27.                     # these commands are executed after installation or configuration reset
  28.                     #-------------------------------------------------------------------------------
  29.                     :if ($action = "apply") do={
  30.                     # wait for interfaces
  31.                     :local count 0;
  32.                     :while ([/interface ethernet find] = "") do={
  33.                     :if ($count = 30) do={
  34.                     :log warning "DefConf: Unable to find ethernet interfaces";
  35.                     /quit;
  36.                     }
  37.                     :delay 1s; :set count ($count +1);
  38.                     };
  39.                    
  40.                       :local count 0;
  41.                       :while ([/interface wireless print count-only] < 1) do={
  42.                         :set count ($count +1);
  43.                         :if ($count = 30) do={
  44.                           :log warning "DefConf: Unable to find wireless interface(s)";
  45.                           /ip address add address=192.168.88.1/24 interface=ether1 comment="defconf";
  46.                           /quit
  47.                         }
  48.                         :delay 1s;
  49.                       };
  50.                       /interface wireless {
  51.                         set wlan1 mode=ap-bridge band=2ghz-b/g/n tx-chains=0,1 rx-chains=0,1 \
  52.                           disabled=no wireless-protocol=802.11 distance=indoors
  53.                         :local wlanMac  [/interface wireless get wlan1 mac-address];
  54.                         :set ssid "MikroTik-$[:pick $wlanMac 9 11]$[:pick $wlanMac 12 14]$[:pick $wlanMac 15 17]"
  55.                         set wlan1 ssid=$ssid
  56.                         set wlan1 frequency=auto
  57.                         set wlan1 channel-width=20/40mhz-Ce ;
  58.                       }
  59.                      /interface bridge
  60.                        add name=bridge disabled=no auto-mac=yes protocol-mode=rstp comment=defconf;
  61.                      :local bMACIsSet 0;
  62.                      :foreach k in=[/interface find where !(slave=yes  || name~"ether1" || name~"bridge")] do={
  63.                        :local tmpPortName [/interface get $k name];
  64.                        :log info "port: $tmpPortName"
  65.                        :if ($bMACIsSet = 0) do={
  66.                          :if ([/interface get $k type] = "ether") do={
  67.                            /interface bridge set "bridge" auto-mac=no admin-mac=[/interface ethernet get $tmpPortName mac-address];
  68.                            :set bMACIsSet 1;
  69.                          }
  70.                        }
  71.                        /interface bridge port
  72.                          add bridge=bridge interface=$tmpPortName comment=defconf;
  73.                      }
  74.                        /ip pool add name="default-dhcp" ranges=192.168.88.10-192.168.88.254;
  75.                        /ip dhcp-server
  76.                          add name=defconf address-pool="default-dhcp" interface=bridge lease-time=10m disabled=no;
  77.                        /ip dhcp-server network
  78.                          add address=192.168.88.0/24 gateway=192.168.88.1 comment="defconf";
  79.                       /ip address add address=192.168.88.1/24 interface=bridge comment="defconf";
  80.                      /ip dns {
  81.                          set allow-remote-requests=yes
  82.                          static add name=router.lan address=192.168.88.1
  83.                      }
  84.                    
  85.                        /ip dhcp-client add interface=ether1 disabled=no comment="defconf";
  86.                      /interface list add name=WAN comment="defconf"
  87.                      /interface list add name=LAN comment="defconf"
  88.                      /interface list member add list=LAN interface=bridge comment="defconf"
  89.                      /interface list member add list=WAN interface=ether1 comment="defconf"
  90.                      /ip firewall nat add chain=srcnat out-interface-list=WAN ipsec-policy=out,none action=masquerade comment="defconf: masquerade"
  91.                      /ip firewall {
  92.                        filter add chain=input action=accept connection-state=established,related,untracked comment="defconf: accept established,related,untracked"
  93.                        filter add chain=input action=drop connection-state=invalid comment="defconf: drop invalid"
  94.                        filter add chain=input action=accept protocol=icmp comment="defconf: accept ICMP"
  95.                        filter add chain=input action=drop in-interface-list=!LAN comment="defconf: drop all not coming from LAN"
  96.                        filter add chain=forward action=accept ipsec-policy=in,ipsec comment="defconf: accept in ipsec policy"
  97.                        filter add chain=forward action=accept ipsec-policy=out,ipsec comment="defconf: accept out ipsec policy"
  98.                        filter add chain=forward action=fasttrack-connection connection-state=established,related comment="defconf: fasttrack"
  99.                        filter add chain=forward action=accept connection-state=established,related,untracked comment="defconf: accept established,related, untracked"
  100.                        filter add chain=forward action=drop connection-state=invalid comment="defconf: drop invalid"
  101.                        filter add chain=forward action=drop connection-state=new connection-nat-state=!dstnat in-interface-list=WAN comment="defconf:  drop all from WAN not DSTNATed"
  102.                      }
  103.                      /ip neighbor discovery set [find name="ether1"] discover=no
  104.                      /tool mac-server disable [find];
  105.                      /tool mac-server mac-winbox disable [find];
  106.                      :foreach k in=[/interface find where !(slave=yes  || name~"ether1")] do={
  107.                        :local tmpName [/interface get $k name];
  108.                        /tool mac-server add interface=$tmpName disabled=no;
  109.                        /tool mac-server mac-winbox add interface=$tmpName disabled=no;
  110.                      }
  111.                     }
  112.                     #-------------------------------------------------------------------------------
  113.                     # Revert configuration.
  114.                     # these commands are executed if user requests to remove default configuration
  115.                     #-------------------------------------------------------------------------------
  116.                     :if ($action = "revert") do={
  117.                     /user set admin password=""
  118.                      /ip firewall filter remove [find comment~"defconf"]
  119.                      /ip firewall nat remove [find comment~"defconf"]
  120.                      /interface list member remove [find comment~"defconf"]
  121.                      /interface list remove [find comment~"defconf"]
  122.                      /tool mac-server remove [find interface!=all]
  123.                      /tool mac-server set [find] disabled=no
  124.                      /tool mac-server mac-winbox remove [find interface!=all]
  125.                      /tool mac-server mac-winbox set [find] disabled=no
  126.                      /ip neighbor discovery set [find ] discover=yes
  127.                        :local o [/ip dhcp-server network find comment="defconf"]
  128.                        :if ([:len $o] != 0) do={ /ip dhcp-server network remove $o }
  129.                        :local o [/ip dhcp-server find name="defconf" !disabled]
  130.                        :if ([:len $o] != 0) do={ /ip dhcp-server remove $o }
  131.                        /ip pool {
  132.                          :local o [find name="default-dhcp" ranges=192.168.88.10-192.168.88.254]
  133.                          :if ([:len $o] != 0) do={ remove $o }
  134.                        }
  135.                        :local o [/ip dhcp-client find comment="defconf"]
  136.                        :if ([:len $o] != 0) do={ /ip dhcp-client remove $o }
  137.                      /ip dns {
  138.                        set allow-remote-requests=no
  139.                        :local o [static find name=router address=192.168.88.1]
  140.                        :if ([:len $o] != 0) do={ static remove $o }
  141.                      }
  142.                      /ip address {
  143.                        :local o [find comment="defconf"]
  144.                        :if ([:len $o] != 0) do={ remove $o }
  145.                      }
  146.                      :foreach iface in=[/interface ethernet find] do={
  147.                        /interface ethernet set $iface name=[get $iface default-name]
  148.                      }
  149.                      /interface bridge port remove [find comment="defconf"]
  150.                      /interface bridge remove [find comment="defconf"]
  151.                      /interface wireless reset-configuration wlan1
  152.                     }
  153.                     :log info Defconf_script_finished;
  154.                    
  155.   caps-mode-script: #-------------------------------------------------------------------------------
  156.                     # Note: script will not execute at all (will throw a syntax error) if
  157.                     #       dhcp or wireless-fp packages are not installed
  158.                     #-------------------------------------------------------------------------------
  159.                    
  160.                     #| CAP configuration
  161.                     #|
  162.                     #|   Wireless interfaces are set to be managed by CAPsMAN.
  163.                     #|   All ethernet interfaces and CAPsMAN managed interfaces are bridged.
  164.                     #|   DHCP client is set on bridge interface.
  165.                    
  166.                     # bridge port name
  167.                     :global brName  "bridgeLocal";
  168.                     :global logPref "defconf:";
  169.                    
  170.                    
  171.                     :global action;
  172.                    
  173.                     :log info $action
  174.                    
  175.                     :if ($action = "apply") do={
  176.                    
  177.                       # wait for ethernet interfaces
  178.                       :local count 0;
  179.                       :while ([/interface ethernet find] = "") do={
  180.                         :if ($count = 30) do={
  181.                           :log warning "DefConf: Unable to find ethernet interfaces";
  182.                           /quit;
  183.                         }
  184.                         :delay 1s; :set count ($count + 1);
  185.                       }
  186.                    
  187.                       :local macSet 0;
  188.                       :local tmpMac "";
  189.                    
  190.                       :foreach k in=[/interface ethernet find] do={
  191.                         # first ethernet is found; add bridge and set mac address of the ethernet port
  192.                         :if ($macSet = 0) do={
  193.                           :set tmpMac [/interface ethernet get $k mac-address];
  194.                           /interface bridge add name=$brName auto-mac=no admin-mac=$tmpMac comment="defconf";
  195.                           :set macSet 1;
  196.                         }
  197.                         # add bridge ports
  198.                         /interface bridge port add bridge=$brName interface=$k comment="defconf"
  199.                       }
  200.                    
  201.                       # try to add dhcp client on bridge interface (may fail if already exist)
  202.                       :do {
  203.                         /ip dhcp-client add interface=$brName disabled=no comment="defconf"
  204.                       } on-error={ :log warning "$logPref unable to add dhcp client";}
  205.                    
  206.                    
  207.                       # try to configure caps (may fail if for example specified interfaces are missing)
  208.                       :local interfacesList "";
  209.                       :local bFirst 1;
  210.                    
  211.                       # wait for wireless interfaces
  212.                       :while ([/interface wireless find] = "") do={
  213.                         :if ($count = 30) do={
  214.                           :log warning "DefConf: Unable to find wireless interfaces";
  215.                           /quit;
  216.                         }
  217.                         :delay 1s; :set count ($count + 1);
  218.                       }
  219.                    
  220.                       # delay just to make sure that all wireless interfaces are loaded
  221.                       :delay 5s;
  222.                       :foreach i in=[/interface wireless find] do={
  223.                         if ($bFirst = 1) do={
  224.                           :set interfacesList [/interface wireless get $i name];
  225.                           :set bFirst 0;
  226.                         } else={
  227.                           :set interfacesList "$interfacesList,$[/interface wireless get $i name]";
  228.                         }
  229.                       }
  230.                       :do {
  231.                         /interface wireless cap
  232.                           set enabled=yes interfaces=$interfacesList discovery-interfaces=$brName bridge=$brName
  233.                       } on-error={ :log warning "$logPref unable to configure caps";}
  234.                    
  235.                     }
  236.                    
  237.                     :if ($action = "revert") do={
  238.                       :do {
  239.                         /interface wireless cap
  240.                           set enabled=no interfaces="" discovery-interfaces="" bridge=none
  241.                       } on-error={ :log warning "$logPref unable to unset caps";}
  242.                    
  243.                       :local o [/ip dhcp-client find comment="defconf"]
  244.                       :if ([:len $o] != 0) do={ /ip dhcp-client remove $o }
  245.                    
  246.                       /interface bridge port remove [find comment="defconf"]
  247.                       /interface bridge remove [find comment="defconf"]
  248.                    
  249.                     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement