166 lines
4.4 KiB
Bash
166 lines
4.4 KiB
Bash
#!/bin/sh
|
|
# OpenWrt DRCom 登录脚本 - 一键安装脚本
|
|
|
|
echo "=========================================="
|
|
echo " DRCom Login - OpenWrt Installation"
|
|
echo "=========================================="
|
|
echo ""
|
|
|
|
# 检查是否为 root 用户
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
echo "[ERROR] Please run as root"
|
|
exit 1
|
|
fi
|
|
|
|
# 颜色定义
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
RED='\033[0;31m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 检查架构
|
|
ARCH=$(uname -m)
|
|
echo "[INFO] Architecture: $ARCH"
|
|
|
|
# 安装依赖
|
|
echo ""
|
|
echo "[STEP 1/5] Installing dependencies..."
|
|
opkg update
|
|
|
|
# 安装 Python3 和 requests 库
|
|
if ! command -v python3 >/dev/null 2>&1; then
|
|
echo "[INFO] Installing python3..."
|
|
opkg install python3 python3-light
|
|
else
|
|
echo "[OK] python3 already installed"
|
|
fi
|
|
|
|
# 检查 requests 模块
|
|
python3 -c "import requests" 2>/dev/null
|
|
if [ $? -ne 0 ]; then
|
|
echo "[INFO] Installing python3-requests..."
|
|
opkg install python3-requests
|
|
if [ $? -ne 0 ]; then
|
|
echo "[WARNING] Failed to install python3-requests via opkg"
|
|
echo "[INFO] Trying pip3 installation..."
|
|
opkg install python3-pip
|
|
pip3 install requests
|
|
fi
|
|
else
|
|
echo "[OK] python3-requests already installed"
|
|
fi
|
|
|
|
# 创建目录
|
|
echo ""
|
|
echo "[STEP 2/5] Creating directories..."
|
|
mkdir -p /etc/drcom
|
|
mkdir -p /var/log
|
|
|
|
# 复制文件
|
|
echo ""
|
|
echo "[STEP 3/5] Copying files..."
|
|
|
|
# 检查是否从当前目录安装
|
|
if [ -f "./drcom_login_fixed.py" ]; then
|
|
echo "[INFO] Installing from current directory..."
|
|
cp -f ./drcom_login_fixed.py /usr/bin/drcom_login.py
|
|
chmod +x /usr/bin/drcom_login.py
|
|
elif [ -f "./drcom_login.py" ]; then
|
|
echo "[INFO] Installing from current directory..."
|
|
cp -f ./drcom_login.py /usr/bin/drcom_login.py
|
|
chmod +x /usr/bin/drcom_login.py
|
|
else
|
|
echo "[ERROR] Main program not found!"
|
|
echo "Please run this script from the project directory."
|
|
exit 1
|
|
fi
|
|
|
|
# 安装 init.d 脚本
|
|
if [ -f "./drcom-login" ]; then
|
|
cp -f ./drcom-login /etc/init.d/drcom-login
|
|
chmod +x /etc/init.d/drcom-login
|
|
else
|
|
echo "[WARNING] Init script not found, creating default..."
|
|
# 这里会手动创建一个简单版本的
|
|
fi
|
|
|
|
# 配置文件
|
|
if [ ! -f "/etc/drcom/config.json" ]; then
|
|
if [ -f "./config.json" ]; then
|
|
cp -f ./config.json /etc/drcom/config.json
|
|
else
|
|
echo "[INFO] Creating default config file..."
|
|
cat > /etc/drcom/config.json << EOF
|
|
{
|
|
"username": "your_username",
|
|
"password": "your_password",
|
|
"server_ip": "172.17.8.2",
|
|
"server_port": 801,
|
|
"interface": "eth1",
|
|
"fixed_mac": ""
|
|
}
|
|
EOF
|
|
fi
|
|
fi
|
|
|
|
# 修改主程序以支持配置文件
|
|
echo ""
|
|
echo "[STEP 4/5] Configuring..."
|
|
|
|
# 备份原程序
|
|
cp /usr/bin/drcom_login.py /usr/bin/drcom_login.py.bak
|
|
|
|
# 检查是否需要添加配置文件支持
|
|
if ! grep -q "import json" /usr/bin/drcom_login.py; then
|
|
echo "[ERROR] Main program structure incompatible"
|
|
exit 1
|
|
fi
|
|
|
|
echo "[OK] Files installed successfully"
|
|
|
|
# 提示配置
|
|
echo ""
|
|
echo "[STEP 5/5] Configuration needed!"
|
|
echo "${YELLOW}==========================================${NC}"
|
|
echo "${YELLOW}Please edit the configuration file:${NC}"
|
|
echo " vi /etc/drcom/config.json"
|
|
echo ""
|
|
echo "Change the following values:"
|
|
echo " - username: Your campus network account"
|
|
echo " - password: Your campus network password"
|
|
echo " - server_ip: DRCom server IP (default: 172.17.8.2)"
|
|
echo " - interface: Network interface (default: eth1)"
|
|
echo "${YELLOW}==========================================${NC}"
|
|
echo ""
|
|
|
|
# 询问是否立即启用
|
|
echo "Do you want to start the service now? (y/n)"
|
|
read -r choice
|
|
if [ "$choice" = "y" ] || [ "$choice" = "Y" ]; then
|
|
# 启用开机自启
|
|
/etc/init.d/drcom-login enable
|
|
|
|
# 立即启动
|
|
/etc/init.d/drcom-login start
|
|
|
|
echo ""
|
|
echo "${GREEN}==========================================${NC}"
|
|
echo "${GREEN}Installation completed!${NC}"
|
|
echo ""
|
|
echo "Commands:"
|
|
echo " Start: /etc/init.d/drcom-login start"
|
|
echo " Stop: /etc/init.d/drcom-login stop"
|
|
echo " Restart: /etc/init.d/drcom-login restart"
|
|
echo " Status: /etc/init.d/drcom-login status"
|
|
echo " Log: tail -f /var/log/drcom-login.log"
|
|
echo "${GREEN}==========================================${NC}"
|
|
else
|
|
echo ""
|
|
echo "${GREEN}Installation completed!${NC}"
|
|
echo "Please edit config first, then run:"
|
|
echo " /etc/init.d/drcom-login enable"
|
|
echo " /etc/init.d/drcom-login start"
|
|
fi
|
|
|
|
exit 0
|