darkfire13 / RouterOS-CHR-Install

Installing MikroTik RouterOS (CHR) on a VPS VDS
4 stars 3 forks source link

one-liner script #2

Open volkirik opened 2 weeks ago

volkirik commented 2 weeks ago
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"](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"](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 /mt_tmp and mounts a temporary file system (tmpfs) to it.
    It then changes the current directory to /mt_tmp.

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.

volkirik commented 2 weeks ago
# Determine the package manager
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 found. This script is not supported."; 
    exit 1; 
fi

# Update packages and install unzip, pwgen, and coreutils
if [ "$pkg_manager" == "yum" ]; then 
    sudo yum -y update && sudo yum -y install unzip pwgen coreutils; 
elif [ "$pkg_manager" == "apt" ]; then 
    sudo apt-get -y update && sudo apt-get -y install unzip pwgen coreutils; 
fi

# Determine the root file system device
root_device=$(df / | awk 'NR==2 {print $1}')
root_device_base=$(echo $root_device | sed 's/[0-9]\+$//')

echo "Root filesystem is on device: $root_device"
echo "Device path: $root_device_base"

# Create and mount a temporary directory
mkdir /mt_ros_tmp && mount -t tmpfs tmpfs /mt_ros_tmp/ && cd /mt_ros_tmp

# Get IP address and gateway
ADDRESS=$(ip addr show enp0s3 | grep global | cut -d' ' -f 6 | head -n 1)
GATEWAY=$(ip route list | grep default | cut -d' ' -f 3)

# Download the latest version of MikroTik RouterOS
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)

if [ -z "$latest_version" ]; then
    echo "Could not retrieve the latest version number."
    exit 1
fi

echo "Latest version: $latest_version"
download_url="https://download.mikrotik.com/routeros/$latest_version/chr-$latest_version.img.zip"

echo "Downloading from $download_url..."
wget --no-check-certificate -O "chr-$latest_version.img.zip" "$download_url"

if [ $? -eq 0 ]; then
    echo "File successfully downloaded: chr-$latest_version.img.zip"
else
    echo "File download failed."
    exit 1
fi

# Unzip and prepare the image
gunzip -c "chr-$latest_version.img.zip" > "chr-$latest_version.img"

# Mount the image
mount -o loop,offset=33571840 "chr-$latest_version.img" /mnt

# Generate a random password
PASSWORD=$(pwgen 12 1)

# Write autorun script to configure the RouterOS instance
echo "Username: admin"
echo "Password: $PASSWORD"

echo "/ip address add address=$ADDRESS interface=[/interface ethernet find where name=ether1]" > /mnt/rw/autorun.scr
echo "/ip route add gateway=$GATEWAY" >> /mnt/rw/autorun.scr
echo "/ip service disable telnet" >> /mnt/rw/autorun.scr
echo "/user set 0 name=admin password=$PASSWORD" >> /mnt/rw/autorun.scr
echo "/ip dns set server=8.8.8.8,1.1.1.1" >> /mnt/rw/autorun.scr

# Remount all mounted filesystems to read-only mode
sync && echo u > /proc/sysrq-trigger

# Flash the image to the disk
dd if="chr-$latest_version.img" of=$root_device_base bs=4M oflag=sync

# Force system reboot
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

Key Updates and Explanations:

Installing Additional Packages:
    Added installation commands for pwgen and coreutils in both yum and apt package managers.

IP Address and Gateway Retrieval:
    The script captures the system's IP address and gateway using ip addr and ip route.

Unzipping and Mounting:
    The image is unzipped and mounted using gunzip and mount commands with appropriate options.

Generating and Setting Password:
    A random 12-character password is generated using pwgen and then set in the autorun script for RouterOS.

Autorun Script:
    The autorun script includes commands to configure the RouterOS instance, including adding the IP address, setting the gateway, disabling telnet, setting the admin password, and configuring DNS servers.

System Reboot:
    Filesystem sync is performed before forcing a system reboot using the SysRq trigger, ensuring that all data is written to disk.

Additional Notes:

Offset Value in Mount: The offset=33571840 is specific to the image format; ensure this value is correct for the particular image you are working with.
SysRq Trigger: The sync command before the SysRq trigger ensures that any pending I/O operations are completed, minimizing the risk of data corruption.

If you have any further questions or need additional adjustments, feel free to ask!

one-liner script:

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 found. This script is not supported."; exit 1; fi && \
[ "$pkg_manager" == "yum" ] && sudo yum -y update && sudo yum -y install unzip pwgen coreutils || [ "$pkg_manager" == "apt" ] && sudo apt-get -y update && sudo apt-get -y install unzip pwgen coreutils && \
root_device=$(df / | awk 'NR==2 {print $1}') && root_device_base=$(echo $root_device | sed 's/[0-9]\+$//') && \
echo "Root filesystem is on device: $root_device" && echo "Device path: $root_device_base" && \
mkdir /gecici && mount -t tmpfs tmpfs /gecici/ && cd /gecici && \
INTERFACE=$(ip route | grep default | awk '{print $5}') && ADDRESS=$(ip addr show "$INTERFACE" | grep global | awk '{print $2}' | head -n 1) && \
GATEWAY=$(ip route list | grep default | awk '{print $3}') && \
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 retrieve the latest version number." && exit 1 || \
echo "Latest version: $latest_version" && download_url="https://download.mikrotik.com/routeros/$latest_version/chr-$latest_version.img.zip" && \
echo "Downloading from $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 "File download failed." && \
gunzip -c "chr-$latest_version.img.zip" > "chr-$latest_version.img" && mount -o loop,offset=33571840 "chr-$latest_version.img" /mnt && \
PASSWORD=$(pwgen 12 1) && echo "Username: admin" && echo "Password: $PASSWORD" && \
echo "/ip address add address=$ADDRESS interface=[/interface ethernet find where name=ether1]" > /mnt/rw/autorun.scr && \
echo "/ip route add gateway=$GATEWAY" >> /mnt/rw/autorun.scr && echo "/ip service disable telnet" >> /mnt/rw/autorun.scr && \
echo "/user set 0 name=admin password=$PASSWORD" >> /mnt/rw/autorun.scr && echo "/ip dns set server=8.8.8.8,1.1.1.1" >> /mnt/rw/autorun.scr && \
sync && echo u > /proc/sysrq-trigger && 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