#!/bin/bash
set -e
#ADDRESS=xxx.xxx.xxx.xxx/24
ADDRESS=$(ip -4 addr show scope global | grep inet | head -n1 | awk '{print $2}')
#GATEWAY=xxx.xxx.xxx.1
GATEWAY=$(ip route list | grep default | cut -d' ' -f 3)
#DISK_DEVICE=/dev/xdx
DISK_DEVICE=$(fdisk -l | grep "^Disk /dev" | grep -v "^Disk /dev/loop" | cut -d' ' -f 2 | tr -d ':')
PASSWORD="ros7.15.3"
# Raw disk image https://mikrotik.com/download#chr
VERSIONCHR=7.15.3
wget -4 https://download.mikrotik.com/routeros/$VERSIONCHR/chr-$VERSIONCHR.img.zip -O chr.img.zip
gunzip -c chr.img.zip > chr.img
# 安装图像
mount -o loop,offset=33571840 chr.img /mnt
# 使用MikroTik命令创建自动运行脚本
echo "正在创建自动运行脚本…"
echo "Username: admin"
echo "Password: $PASSWORD"
cat > /mnt/rw/autorun.scr <<EOF
/ip address add address=$ADDRESS interface=[/interface ethernet find where name=ether1]"
/ip route add gateway=$GATEWAY"
/ip dns set server=8.8.8.8,1.1.1.1"
/ip service set telnet disabled=yes
/ip service set ftp disabled=yes
/ip service set www disabled=yes
/ip service set ssh disabled=yes
/ip service set api disabled=yes
/ip service set api-ssl disabled=yes
/user set 0 name=admin password=$PASSWORD"
/ip firewall filter add action=drop chain=input dst-port=53 in-interface=ether1 protocol=udp
EOF
# 卸载映像
echo "卸载映像..."
umount /mnt
# 刷新内核缓存
echo "刷新内核缓存"
echo u > /proc/sysrq-trigger
# 将映像写入主磁盘设备
echo "将映像写入主磁盘设备..."
dd if=chr.img bs=1024 of=$DISK_DEVICE
echo "sync disk"
# 同步所有挂载的文件系统
echo s > /proc/sysrq-trigger
echo "Sleep 10 seconds"
#/usr/bin/sleep 10
read -t 10 -u 1
echo "Ok, reboot"
# 执行类似于RESET按钮的立即操作系统重新启动(无需同步和卸载文件系统)
echo b > /proc/sysrq-trigger
RouterOS CHR Install
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可