#!/bin/sh

# 检查是否安装了docker
if ! [ -x "$(command -v docker)" ]; then
  read -p "系统未安装docker，是否要安装docker？(y/n) " install_docker
  if [ "$install_docker" = "y" ]; then
    sudo apt update
    sudo apt install docker.io
  else
    echo "用户选择不安装docker，退出脚本"
    exit 0
  fi
fi



# Debian: 重启 Docker 服务
if [[ -e /etc/debian_version ]]; then
    sudo systemctl daemon-reload
    sudo systemctl restart docker
fi

# Ubuntu: 重启 Docker 服务
if [[ -e /etc/lsb-release ]]; then
    sudo systemctl daemon-reload
    sudo systemctl restart docker
fi

# OpenWrt: 重启 Docker 服务
if [[ -e /etc/openwrt_release ]]; then
    /etc/init.d/dockerd restart
fi


sleep 5s

#拉取Ubuntu镜像
docker pull docker.free-img.eu.org/ubuntu
docker tag docker.free-img.eu.org/ubuntu:latest ubuntu:latest

sleep 1s

WAN_IFACE="$(uci get network.wan.device | cut -d' ' -f2)"

WAN_IP="$(ip addr show | grep "$WAN_IFACE" | awk '{print $2}' | cut -d/ -f1 | tail -n 1)"

WAN_GATEWAY="$(ip route show default | awk '/default/ {print $3}')"

WAN_IPRANGE="$(echo "$WAN_GATEWAY" | sed "s/([0-9]*.[0-9]*.[0-9]*).[0-9]*/1.0/")"

MACVLAN_IP="$(echo "$WAN_GATEWAY" | sed "s/([0-9]*.[0-9]*.[0-9]*).[0-9]*/1.234/")"


ip link set "$WAN_IFACE" promisc on


docker network create -d macvlan --subnet="$WAN_IPRANGE"/24 --gateway="$WAN_GATEWAY" -o parent="$WAN_IFACE" macnet


sleep 5s 

#创建Ubuntu容器，并且设置
docker run -dit 
  --name=PcdnUbuntu 
  --restart always 
  --network macnet 
  --ip "$MACVLAN_IP" 
  --privileged 
 ubuntu:latest

#容器外执行准备任务
docker exec PcdnUbuntu bash -c "apt-get update -y" #更新源


sleep 3s 


docker exec PcdnUbuntu bash -c "apt install iproute2 -y" #安装网络工具


sleep 3s 


docker exec PcdnUbuntu bash -c "apt-get install -y openssh-client" #安装ssh客户端


sleep 3s 


docker exec PcdnUbuntu bash -c "apt-get install -y openssh-server" #安装ssh服务端


sleep 3s 


docker exec PcdnUbuntu bash -c "/etc/init.d/ssh start" #开启ssh服务


sleep 3s 


docker exec PcdnUbuntu bash -c "echo 'PermitRootLogin yes' | tee -a /etc/ssh/sshd_config" #开启 root 用户登录


sleep 3s 


docker exec PcdnUbuntu bash -c "service ssh restart" #重启ssh服务


sleep 3s 


docker exec PcdnUbuntu bash -c "echo 'root:123456789' | chpasswd" #修改root密码为123456789


sleep 3s 


docker exec PcdnUbuntu bash -c "service ssh restart" #重启ssh服务


sleep 3s 


docker exec PcdnUbuntu bash -c "ifconfig" 



