How to Make an Automated Configuration and Uninstall

From MikroTik Wiki
Revision as of 22:17, 13 November 2008 by Canniscam (talk | contribs) (New page: == The Explanation == === The Breakdown === ==== Section 1 ==== ==== Section 2 ==== ==== Section 3 ==== ==== Section 4 ==== <pre> #~ "Uninstall Flag (Default = no )" :local deconf no...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Explanation

The Breakdown

Section 1

Section 2

Section 3

Section 4

#~ "Uninstall Flag (Default = no )"
:local deconf no
#~#

#~ "Start of Main Sub"
:if ($deconf = no) do={

#~ "Calculate Script Runtime"
:put ("System Configuration Script Executed in " . [:time {
#~#

#########################################################
## An Automated Installation and Uninstall Script      ##
## This Script Prepared by: Casey Annis                 ##
##                                                     ##
## Wednesday, October 22nd, 2008                        ##
#########################################################

#~ These variables can be edited to make the script funtional in your network

#########################################################
## System Configuration Variables - To Help Automate   ##
## Network Setup                                       ##
#########################################################

#########################################################
## Delcare The System Type:                            ##
## (i.e., RB500revA, RB433, RB433AH, etc.)             ##
## If you are running RoS on a non RouterBoard product ##
## you can change 'OTHER' to a custom string.          ##    
#########################################################
:local systemTYPE " " 
:if ([/system router get routerboard] = no) do {
        :set systemTYPE ("RB" . [/system router get model])} else {
        :set systemTYPE "OTHER"
        }


## Installed Location / Part of RADIO-NAME (i.e., 5th_and_Baum.)
:local installedLOCATION "5th_and_Baum"
## System Site Indentifier can be used to specify a site ID (i.e., 5SBA) ##
:local siteID "5SBA"

## LAN IPv4 Subnet First Octet 
:local firstOctet "172"

## LAN IPv4 Subnet Second Octet 
:local secondOctet "16"

## LAN IPv4 IP Third Octet
:local thirdOctet "16"

## LAN IPv4 Subnet IP Suffix (ie Router Host Address 172.16.2.<fourthOctet>)
:local fourthOctet "1"

## LAN IPv4 Subnet in CIDR Notation (Deafault is 24, but may vary)
:local ipLANsubnet "24"

## Your Company Name
:local companyNAME "MikroTik"

## SNMP Community String ##
:local snmpname "PUBLIC"

## Primary DNS Server Ip Address ##
:local pridns "4.2.2.2"

## Secondary DNS Server Ip Address ##
:local secdns "4.2.2.3"
#~#
#########################################################
## END OF System Configuration Variables Setup         ## 
## DO NOT EDIT BELOW THIS LINE                         ##
#########################################################
## Local Subnet IP Prefix ##
:local ipprefix ( $firstOctet . "." . $secondOctet . "." . $thirdOctet . ".")



##########################################################
## Configure Frequency Arrays for All Supported Bands   ##
## This is for Speculation, I'm Still writing this       ##
## section.                                             ## 
#########################################################
#~ Sub wlanCHIPsetDETECT  -- This section is still under dev.
#:local chipSET58 (:toarray "xxxx,xxxx,xxxx,xxxx,xxxx,xxxx")
#:local chipSET24
#:local chipSET700
#:local chipSET900
#:local chipSETWiMAX
#:local freqARRAY [:toarray $freqMODEstring]
#~#

#~ Radio Card Frequency Arrays
## Freq Array: 5Ghz Standard Band##
:global freq58string "5180,5200,5220,5240,5260,5280,5300,5320,5745,5765,5785,5805,5825"
:local freq58array [:toarray $freq58string]
## Freq Array: 700Mhz ##
##:global freq700string " "
## :global freq700array " "

## Freq Array: 900Mhz ##
## :global freq900string " "

## Freq Array: 4.9Ghz ##
## :global freq49string " "

## Freq Array: WiMAX ##
## :global freqWIMAXstring " "
#~#

#~ Start Building The System Identity String (a.k.a., Serial Number)
##########################################################
## This section of the script will set some identifying ##
## information for this unit. This will not affect      ##
## configuration but will help identify the unit for    ##
## warranty purposes.                                   ## 
##########################################################


## Build Router Identity String                        ##
## We use the MAC Address of the 1st Ethernet port to  ##
## specify our serials                                 ##
###### While loop to strip :'s from mac address
:local stripCOUNT 0
:local E1macADDRESS [/interface ethernet get ether1 mac-address]
:local macLEN [:len $E1macADDRESS]
:local snBUILDstring ""
:global serialNUMBER ""
:local i 0
:while ($stripCOUNT != $macLEN) do {
      :if ($stripCOUNT = ($i +2)) do {
          :set stripCOUNT ($stripCOUNT +1)
          :set i ($i +3)} else {
          :set snBUILDstring  [:pick $E1macADDRESS $stripCOUNT]
          :set stripCOUNT ($stripCOUNT +1)
          :set serialNUMBER ($serialNUMBER . $snBUILDstring)
  }
  }
:local ident ("S/N: " . $serialNUMBER . "")
:put ("Asset S/N Found: " . $serialNUMBER . "")

#~# 

#~  Start Configuring Interfaces!
##########################################################
## Start Configuring Interfaces on The Router           ##
## AUTOMATED PROCESS 								    ##
##########################################################

/ interface bridge 
add name="bridge1" arp=proxy-arp comment="" disabled=no 

/ interface bridge port 
add interface=ether2 bridge=bridge1 comment="" \
    disabled=no 
add interface=ether1 bridge=bridge1 comment="" \
    disabled=no 


:local wlanACONF 0
:foreach i in=[/interface wireless find] do { 
    
    :put ("Array Len: " . [:len $i])
    :if ([:len $i] < 2) do {/ interface wireless 
        :set wlanACONF 1
        set wlan1 radio-name=("Remote Station - " . $siteID . "- " . $installedLOCATION . " ") \
            mode=ap-bridge ssid="WASP1" area="" frequency=5180 band=5ghz \
            disabled=no default-authentication=yes antenna-mode=ant-b
		/interface bridge port
          add interface=$i bridge=bridge1
    } else={
              :put ("Configuring: " . ([/interface wireless get $i name]))
              :put ("Freq Selected: " . [:pick $freq58array $wlanACONF])
              :put ("Radio Name: " .  " AP  - " . $siteID . " - " . $installedLOCATION . " ")
              /interface wireless 
                  set $i radio-name=("AP  - " . $siteID . " - " . $installedLOCATION . " ") \
                  mode=ap-bridge ssid=("WASP" . ($wlanACONF +1) . "") area="" frequency=[:pick $freq58array ($wlanACONF)] band=5ghz \
                  disabled=no wds-mode=dynamic wds-default-bridge=bridge1 default-authentication=yes \
                  antenna-mode=ant-b
               :put "Debug: No Error Here"
              /interface bridge port 
                  add interface=$i bridge=bridge1
              :set wlanACONF ($wlanACONF +1) 
                   
          }
}
#~#

#~ Start DNS Configuration 
######################################################
## Change the DNS Server Entries so they match The  ##
## servers provided by the bandwidth agency.        ##
######################################################
/ ip dns 
set primary-dns=$pridns secondary-dns=$secdns \
    allow-remote-requests=yes cache-size=2048KiB cache-max-ttl=1w 
/ ip dns static 
add name="site.router.local" address=172.16.16.1 ttl=1d 

#~#

#~ IP Address Configuration
#######################################################
## This portion of the script will configure ip      ##
## addresses based on the variables populated at the ##
## start of the script.                              ##
#######################################################

/ip address
add address=10.10.10.1/24 interface=ether3 comment="Onsite Service Port Adress" disabled=no 

#######################################################
##           Bridge IP Address                       ##
##                                                   ##
##   This will be set dynamically by the script      ##
#######################################################

add address=($ipprefix . $fourthOctet . "/" . $ipLANsubnet) \
    interface=bridge1 comment="Local LAN IP Address - Added by Config Script" disabled=no 

/ ip route 
add dst-address=0.0.0.0/0 gateway=($ipprefix . "1") comment="Default Route - Added by Config Script"
#~#

#~ Some General Router Settings / Housekeeping
/ system identity 
set name=$ident

/ system ntp client 
set enabled=yes mode=broadcast primary-ntp=132.163.4.102 

## Airport Router System Note ##
:local confTIME ([/system clock get date] . " @ " . [/system clock get time])
:local sysnote ($companyNAME .  "-" . "Site: " . $siteID . " - " . " Model: " . $systemTYPE . " ---- This System Configured on: " . $confTIME . "" )
/ system note 
set show-at-login=yes note=$sysnote 
#~#

#~ "Wrapup and Give some Feedback"
    :put ("")    
:put ("Initial Configuration Complete") 
}] . " seconds.") } else={
#~#

#~# "End of Main Config Script"

#~ "Start The Uninstall Script"
:put ("Deconfiguration Script Executed in " . [:time {
:for i from=1 to=100 step=1 do={
:put " " }
:put ("//WARNING///WARNING///WARNING///WARNING///WARNING///WARNING///WARNING//")
:put ("//                                                                   //")
:put ("//                          DANGEROUS:                               //")
:put ("//            SYSTEM DECONFIGURATION VARIABLE HAS BEEN SET           //")
:put ("//                                                                   //")
:put ("//WARNING///WARNING///WARNING///WARNING///WARNING///WARNING///WARNING//")
:put (" ")
:put ("          YOU HAVE TEN (10) SECONDS TO CANCEL THIS OPERATION           ")
:for i from=1 to=40 step=1 do={
:put " " }
:delay 5
:put ("Countdown:")
:put ("5....")
:delay 1
:put ("4...")
:delay 1
:put ("3..")
:delay 1
:put ("2.")
:delay 1
:put ("1")
:delay 1
:put ("Initiating Configuration Wipe")
:delay 1

##### Reset System Scheduler Settings #####
:put ("##### Resetting System Scheduler Settings #####")
:local schedARRAY [/system scheduler find]
:put ("Deconf: " . [:len $schedARRAY] . " scheduled events found.")
:if ([:len $schedARRAY] != 0) do={
    :foreach i in=($schedARRAY) do={
        :put ("Deconf: Now Removing " . [/system scheduler get $i name] . ".") 
            /system scheduler
           remove $i
           }
  }

##### Deconf: Stripping Identifiers and SysNotes #####
:put "##### Deconf: Removing System Identity. #####"
/system identity set name=MikroTik
:put "##### Deconf: Clearing SysNote. #####"
/system note set note="..." show-at-login="no"

##### Deconf: Resetting Wireless Cards to Defaults #####
:put ("##### Resetting Wireless Cards to Defaults #####")
:local wlanARRAY [/interface wireless find]
:put ("Deconf: " . [:len $wlanARRAY] . " wireless modules found")
:foreach wlanRESET in=($wlanARRAY) do={
    :put ("Deconf: Resetting " . [/interface wireless get $wlanRESET name] . ".")
    /interface wireless reset-configuration $wlanRESET
    :delay 1
}


##### Deconf:  Stripping IP Address #######
:local ipARRAY [/ip address find]
:put ("Deconf: " . [:len $ipARRAY] . " IP addresses found.")
:foreach ipADDY in=($ipARRAY) do={
    :put ("Deconf: Removing IP Addresses - Removing " . [/ip address get $ipADDY address] . " from " . [/ip address get $ipADDY interface] . ".")
    :log warning ("Deconf: Removing IP Addresses - Removing " . [/ip address get $ipADDY address] . " from " . [/ip address get $ipADDY interface] . ".")
    /ip address
    remove $ipADDY
    :delay 1
    }

###### Deconf: Stripping Routes ######
###### Deconf: Stripping Routes ######
:local routeARRAY [/ip route find]
:put "###### Uninstall Routes ######"
:put ("Deconf: " . [:len $routeARRAY] . " routes found.")


:local routeFILL ""
:foreach ipROUTE in=($routeARRAY) do={
    :if ([/ip route get $ipROUTE gateway] != "") do={
        :set routeFILL ("gateway: " . [/ip route get $ipROUTE gateway] . ".") } else={
        :set routeFILL ("pref-src: " . [/ip route get $ipROUTE pref-src] . ".") } 
    :put ("Deconf: Removing Route - DST-Address " . [/ip route get $ipROUTE dst-address] . " with " . $routeFILL )
    :log warning ("Deconf: Removing Route - DST-Address " . [/ip route get $ipROUTE dst-address] . " with " . $routeFILL  .  ".")
    /ip route
    remove $ipROUTE
:delay 1
    }
     

##### Deconf:  Uninstall Bridge Ports ########
:put ("##### Uninstall Bride Ports #####")
:local brpARRAY [/interface bridge port find]
:put ("Deconf: " . [:len $brpARRAY] . " bridge ports found.")
:if ([:len $brpARRAY] != 0) do={
    :foreach bRp in=($brpARRAY) do={
        :put ("Deconf: Uninstall Bride Ports - Removing " . [/interface bridge port get $bRp interface] . ".")
        :log warning ("Deconf: Uninstall Bride Ports - Removing " . [/interface bridge port get $bRp interface]  . ".")
        /interface bridge port
        disable $bRp
        :delay 1
        remove $bRp
        :delay 1
    }
}

####### Deconf:  Uninstall Bridge ########
:put ("##### Deconf: Uninstall Bridge Interfaces #####")
:local bridgeARRAY [/interface bridge find]
:put ("Deconf: " . [:len $bridgeARRAY] . " bridge interfaces found.")
:if (:len $bridgeARRAY != 0) do={
    :foreach i in=($bridgeARRAY) do={
        :put ("Deconf: Debug - Value of 'i'" . $i . "'.")
        :put ("Deconf: Uninstall Bridge - Removing " . [/interface bridge get $i name] . ".")
        :log warning ("Deconf: Uninstall Bridge - Removing " . [/interface bridge get $i name] . ".")
        /interface bridge remove $i
    }
}
    
###### Deconf: Reset DNS Servers & Static Entries #####
:put ("##### Deconf: Reset DNS Servers to Defaults. #####")
/ip dns set primary-dns="0.0.0.0" secondary-dns="0.0.0.0"
:delay 1
:put ("##### Deconf: Remove all Static DNS Entries. #####")
:local staticDNSarray [/ip dns static find]
:put ("Deconf: " . [:len $staticDNSarray] . " static entries found.")
:if ([:len $staticDNSarray] != 0) do={
    :put ("Deconf: Removing The Following Entries:")
    :put ("_______________________________________________________________")
    :put ("| FQDN                   | Address           | TTL             |")
    :put ("---------------------------------------------------------------")
    :foreach i in=($staticDNSarray) do={
        :put ([/ip dns static get $i name] . "        " . [/ip dns static get $i address] . "        " . [/ip dns static get $i ttl])
        /ip dns static 
        remove $i
        :delay 1
    }
}
##### Reset NTP Client #####
:put ("##### Resetting NTP Client #####")
/ system ntp client 
set enabled=no mode=broadcast primary-ntp=0.0.0.0 secondary-ntp=0.0.0.0 

}] . " seconds.")
#~# "End of Uninstall Script"
}
#~# "End of Main Sub"