| 
									
										
										
										
											2023-03-07 10:57:42 +01:00
										 |  |  | #!rsc by RouterOS | 
					
						
							|  |  |  | # RouterOS script: firmware-upgrade-reboot | 
					
						
							| 
									
										
										
										
											2024-01-01 15:25:25 +01:00
										 |  |  | # Copyright (c) 2022-2024 Christian Hesse <mail@eworm.de> | 
					
						
							| 
									
										
										
										
											2023-03-07 10:57:42 +01:00
										 |  |  | # https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2023-11-15 21:31:18 +01:00
										 |  |  | # requires RouterOS, version=7.12 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2023-03-07 10:57:42 +01:00
										 |  |  | # install firmware upgrade, and reboot | 
					
						
							|  |  |  | # https://git.eworm.de/cgit/routeros-scripts/about/doc/firmware-upgrade-reboot.md | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :global GlobalFunctionsReady; | 
					
						
							|  |  |  | :while ($GlobalFunctionsReady != true) do={ :delay 500ms; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-04 13:48:01 +01:00
										 |  |  | :local Main do={ | 
					
						
							|  |  |  |   :local ScriptName [ :tostr $1 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   :global LogPrintExit2; | 
					
						
							|  |  |  |   :global ScriptLock; | 
					
						
							|  |  |  |   :global VersionToNum; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 16:12:36 +01:00
										 |  |  |   :if ([ $ScriptLock $ScriptName ] = false) do={ | 
					
						
							|  |  |  |     :return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-03-04 13:48:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   :local RouterBoard [ /system/routerboard/get ]; | 
					
						
							|  |  |  |   :if ($RouterBoard->"current-firmware" = $RouterBoard->"upgrade-firmware") do={ | 
					
						
							|  |  |  |     $LogPrintExit2 info $ScriptName ("Current and upgrade firmware match with version " . \ | 
					
						
							| 
									
										
										
										
											2024-03-04 15:46:46 +01:00
										 |  |  |       $RouterBoard->"current-firmware" . ".") false; | 
					
						
							|  |  |  |     :return true; | 
					
						
							| 
									
										
										
										
											2024-03-04 13:48:01 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   :if ([ $VersionToNum ($RouterBoard->"current-firmware") ] > [ $VersionToNum ($RouterBoard->"upgrade-firmware") ]) do={ | 
					
						
							| 
									
										
										
										
											2024-03-04 15:46:46 +01:00
										 |  |  |     $LogPrintExit2 info $ScriptName ("Different firmware version is available, but it is a downgrade. Ignoring.") false; | 
					
						
							|  |  |  |     :return true; | 
					
						
							| 
									
										
										
										
											2024-03-04 13:48:01 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   :if ([ /system/routerboard/settings/get auto-upgrade ] = false) do={ | 
					
						
							|  |  |  |     $LogPrintExit2 info $ScriptName ("Firmware version " . $RouterBoard->"upgrade-firmware" . \ | 
					
						
							|  |  |  |       " is available, upgrading.") false; | 
					
						
							|  |  |  |     /system/routerboard/upgrade; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   :while ([ :len [ /log/find where topics=({"system";"info";"critical"}) \ | 
					
						
							|  |  |  |       message="Firmware upgraded successfully, please reboot for changes to take effect!" ] ] = 0) do={ | 
					
						
							|  |  |  |     :delay 1s; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   :local Uptime [ /system/resource/get uptime ]; | 
					
						
							|  |  |  |   :if ($Uptime < 1m) do={ | 
					
						
							|  |  |  |     :delay $Uptime; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $LogPrintExit2 info $ScriptName ("Firmware upgrade successful, rebooting.") false; | 
					
						
							|  |  |  |   /system/reboot; | 
					
						
							| 
									
										
										
										
											2023-03-07 10:57:42 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-04 13:48:01 +01:00
										 |  |  | $Main [ :jobname ]; |