Open volkirik opened 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
Explanation of the One-Liner Script:
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.