if command -v yum &> /dev/null; then pkg_manager="yum"; elif command -v apt &> /dev/null; then pkg_manager="apt"; else echo "Neither yum nor apt is found. This script is not supported.."; exit 1; fi && \
[ "$pkg_manager" == "yum" ] && sudo yum -y update && sudo yum -y install unzip || [ "$pkg_manager" == "apt" ] && sudo apt-get -y update && sudo apt-get -y install unzip && \
root_device=$(df / | awk 'NR==2 {print $1}') && root_device_base=$(echo $root_device | sed 's/[0-9]\+$//') && \
echo "Root filesystem is found on this device: $root_device" && echo "Device base path: $root_device_base" && \
mkdir /mt_tmp && mount -t tmpfs tmpfs /mt_tmp/ && cd /mt_tmp && \
rss_feed="https://download.mikrotik.com/routeros/latest-testing.rss" && rss_content=$(curl -s $rss_feed) && \
latest_version=$(echo "$rss_content" | grep -oP '(?<=<title>RouterOS )[\d\.]+rc\d+' | head -1) && \
[ -z "$latest_version" ] && echo "Could not get latest version number." && exit 1 || \
echo "Latest version number: $latest_version" && \
download_url="https://download.mikrotik.com/routeros/$latest_version/chr-$latest_version.img.zip" && \
echo "File download is starting from URL : $download_url ..." && \
wget --no-check-certificate -O "chr-$latest_version.img.zip" "$download_url" && \
[ $? -eq 0 ] && echo "File successfully downloaded: chr-$latest_version.img.zip" || echo "The file could not be downloaded." && \
unzip chr-$latest_version.img.zip && \
dd if=chr-$latest_version.img of=$root_device_base bs=4M oflag=sync && \
echo 1 > /proc/sys/kernel/sysrq && echo b > /proc/sysrq-trigger
Explanation of the One-Liner Script:
Determine the Package Manager:
The command checks if yum exists using command -v yum &> /dev/null. If found, pkg_manager is set to yum.
If yum is not found, it checks for apt in the same way. If apt is found, pkg_manager is set to apt.
If neither yum nor apt is found, the script prints a message indicating that neither package manager is supported and exits.
Update Packages and Install unzip:
Depending on the value of pkg_manager, the script performs a package update and installs unzip using the appropriate package manager.
If yum is being used, the script runs sudo yum -y update followed by sudo yum -y install unzip.
If apt is being used, the script runs sudo apt-get -y update followed by sudo apt-get -y install unzip.
Determine the Root File System Device:
The script finds the device where the root file system is mounted by running df / | awk ‘NR==2 {print $1}’, which returns something like /dev/sda2.
The script then removes the partition number (e.g., 2) to obtain only the base device path (e.g., /dev/sda) using sed.
Display the Root Device Information:
The script prints the device path of the root file system (/dev/sda2) and the base device path (/dev/sda).
Create and Mount a Temporary Directory:
The script creates a directory /gecici and mounts a temporary file system (tmpfs) to it.
It then changes the current directory to /gecici.
Download the Latest Version of MikroTik RouterOS:
The script fetches the latest RouterOS version by downloading the RSS feed from https://download.mikrotik.com/routeros/latest-testing.rss.
It extracts the latest version number (e.g., 7.16rc4) from the RSS feed using grep and head.
Check if the Latest Version was Found:
If no version number was found, the script prints an error message and exits.
Construct the Download URL:
The script constructs the download URL for the latest RouterOS image file using the version number it found.
Download the RouterOS Image:
The script uses wget to download the RouterOS image file from the constructed URL.
If the download is successful, it prints a success message; otherwise, it prints a failure message.
Unzip the Downloaded Image and Write it to the Disk:
The script unzips the downloaded file to extract the image.
It writes the image to the base device path (e.g., /dev/sda) using the dd command with a block size of 4MB.
Reboot the System:
The script syncs the file system buffers and then uses the /proc/sysrq-trigger mechanism to force a system reboot by sending the magic SysRq key b.
This one-liner script efficiently automates the entire process of updating the package manager, downloading the latest MikroTik RouterOS version, and flashing it to the root device.