{ ####################################### # # AMPRNet enc.txt parser # version 0.0.2 # # # by YT9TP, Pedja, http://yt9tp.iz.rs # ####################################### # PARAMETERS ####################################### # local address of AMPR gateway :local localAddr "192.168.1.1" ; # custom contents fora a comment on created IPIP tunnel interface :local ipipComment ""; # custom parameters to add to /interface add command :local ipipCustomParams ""; # custom contents fora a comment on created route :local routeComment ""; # custom parameters to add to /ip route add command :local routeCustomParams "routing-mark=amprs"; # identificator to be added to comments or names which is used to identify settings added by this script (to be auto deleted when needed) :local scriptIdentificatior "amprs-d"; # name of file where encap routign data is stored :local fileName "encap.txt" ######################################## ######################################## # DO NOT EDIT BELOW THIS LINE !!! ######################################## # load encap file :local content [/file get [/file find name=$fileName ] contents] ; :local contentLen [ :len $content ] ; # removing ampr routes /ip route ; :foreach i in=[find] do={ :local iName [get $i gateway]; :local iAddr [get $i dst-address]; :local iComment [get $i comment]; :local netID; :set netID [:pick $iAddr 0 3] ; :local isAmpr [:find $iComment $scriptIdentificatior]; :if ($isAmpr = 1) do={ :put ("Removing route " . $iAddr . " via " . $iName); /ip route remove $i ; } } # removing ampr interfaces /interface ipip ; :foreach i in=[find] do={ :local iName [get $i name]; :local isAmpr [:find $iName $scriptIdentificatior]; :if ($isAmpr >= 0) do={ :put ("Removing interface: " . $iName); /interface ipip remove $i ; } } / ; :set ipipComment ("[" . $scriptIdentificatior . "] " . $ipipComment); :set routeComment ("[" . $scriptIdentificatior . "] " . $routeComment); :local NL "\n" # initialize script :local lineEnd 0; :local line ""; :local lastEnd 0; # parse file :local linecounter 0; :do { :set linecounter ($linecounter+1); :set lineEnd [:find $content $NL $lastEnd ] ; :if ($lineEnd = "" ) do={ :set lineEnd 0 ; } :set line [:pick $content $lastEnd $lineEnd] ; :set lastEnd ( $lineEnd + [ :len $NL ] ) ; :if ( [:pick $line 0 1] != "#" ) do={ local line1 "" :set line1 [:pick $line 17 [ :len $line ]] ; :local idTextPos 0 ; :set idTextPos [:find $line1 "encap" ] ; :local netIP ; :set netIP [:pick $line1 0 $idTextPos ] ; # fix shorted IP part of network address :local fixedIP "" ; :local cdrpos ; :set cdrpos [:find $netIP "/" ] ; :local rest ([:pick $netIP 0 $cdrpos ] . ".0.0.0.0.") ; :local cdr [:pick $netIP $cdrpos [:len $netIP] ] ; :local counter 0 ; :do { :local octpos ; :set octpos [:find $rest "." ] ; :loc oct ; :set oct [:pick $rest 0 $octpos ] ; :set fixedIP ($fixedIP . $oct) ; :if ($counter < 3) do= { :set fixedIP ($fixedIP . ".") ; } :set rest [:pick $rest ($octpos+1) [:len $rest] ] ; :set counter (counter +1) ; } while ( $counter < 4 ) ; :set netIP ($fixedIP . $cdr) ; :local gateIP ; :set gateIP [:pick $line1 ($idTextPos+6) [ :len $line ]] ; :put ("net: " . $netIP . " gateway: " . $gateIP); :local interfaceName ; :set interfaceName ($scriptIdentificatior . "-" . $gateIP); # check if IPIP for this gateway already exeists /interface ipip ; :local dupeInterface "" ; :set dupeInterface [find name=$interfaceName]; /; if ($dupeInterface = "") do={ # create IPIP interface :local interfaceCmd ; :set interfaceCmd ("/interface ipip add local-address=" . $localAddr ." name=" . $interfaceName . " remote-address=" . $gateIP . " disabled=no"); if ($ipipComment != "") do={ :set interfaceCmd ($interfaceCmd . " comment=\"" . $ipipComment . "\" "); } if ($ipipCustomParams != "") do={ :set interfaceCmd ($interfaceCmd . " " . $ipipCustomParams); } # :put ("interfaceCmd=" . $interfaceCmd); :local pCmd [:parse $interfaceCmd] ; $pCmd; } else { :put ("Interface " . $interfaceName . " already exists!") ; } # create route :local routeCmd :set routeCmd ("/ip route add dst-address=" . $netIP . " gateway=" . $scriptIdentificatior . "-". $gateIP . " comment=\"" . $routeComment . "\" " . $routeCustomParams); # :put ("routeCmd=" . $routeCmd); :local pCmd [:parse $routeCmd] ; $pCmd; } } while ($lineEnd < $contentLen); }