#!/bin/bash # from # https://github.com/oneclickvirt/pve # 2025.07.06 # 不支持debian13及更新版本的debian,未对sysctl做适配 export DEBIAN_FRONTEND=noninteractive is_noninteractive() { case "${noninteractive:-}" in true | TRUE | True | 1 | yes | YES | Yes | y | Y) return 0 ;; esac case "${NONINTERACTIVE:-}" in true | TRUE | True | 1 | yes | YES | Yes | y | Y) return 0 ;; esac return 1 } detect_wifi_interface() { ip -o link show | awk -F': ' ' { iface = $2 sub(/@.*/, "", iface) if (iface ~ /^(wlp|wlan|wlx|wl)[[:alnum:]_.-]*$/) { print iface exit } } ' } validate_interface_name() { local iface="$1" [[ "$iface" =~ ^[A-Za-z0-9_.-]+$ ]] } cd /mnt/wireless || exit 1 shopt -s nullglob deb_files=(*.deb) for i in {1..6}; do INSTALLED_PACKAGES="" FAILED_PACKAGES="" for deb in "${deb_files[@]}"; do if apt-get install -y "./$deb" &>/dev/null; then INSTALLED_PACKAGES="$INSTALLED_PACKAGES $deb" else FAILED_PACKAGES="$FAILED_PACKAGES $deb" fi done done apt-get install -f -y &>/dev/null if [ -n "$INSTALLED_PACKAGES" ]; then echo "Successfully installed packages:$INSTALLED_PACKAGES" else echo "No new packages were installed" fi if [ -n "$FAILED_PACKAGES" ]; then echo "Failed to install packages:$FAILED_PACKAGES" fi rfkill unblock wifi WIFI_INTERFACE="${WIFI_INTERFACE:-$(detect_wifi_interface)}" WIFI_INTERFACE="${WIFI_INTERFACE%%@*}" if [ -z "$WIFI_INTERFACE" ]; then echo "Could not detect WiFi interface" echo "Set WIFI_INTERFACE manually, for example: export WIFI_INTERFACE=wlan0" echo "无法检测无线网卡接口,可手动设置 WIFI_INTERFACE,例如:export WIFI_INTERFACE=wlan0" exit 1 fi if ! validate_interface_name "$WIFI_INTERFACE"; then echo "Invalid WiFi interface name: $WIFI_INTERFACE" echo "无线网卡接口名无效:$WIFI_INTERFACE" exit 1 fi WIFI_INTERFACE_REGEX=$(printf '%s' "$WIFI_INTERFACE" | sed 's/[][\\.^$*+?{}()|/]/\\&/g') echo "Detected WiFi interface: $WIFI_INTERFACE" if is_noninteractive; then SSID="${WIFI_SSID:-}" PASSWORD="${WIFI_PASSWORD:-}" if [ -z "$SSID" ] || [ -z "$PASSWORD" ]; then echo "noninteractive=true requires WIFI_SSID and WIFI_PASSWORD" echo "无交互模式需要设置 WIFI_SSID 和 WIFI_PASSWORD" exit 1 fi if [[ "${WIFI_SKIP_SCAN^^}" != "TRUE" ]]; then SCAN_RESULT=$(iwlist "$WIFI_INTERFACE" scan 2>/dev/null | grep -iF "ESSID:\"$SSID\"") if [ -n "$SCAN_RESULT" ]; then echo "WiFi network '$SSID' found." else echo "Error: WiFi network '$SSID' not found in available networks." echo "Set WIFI_SKIP_SCAN=true if the SSID is hidden or scanning is unavailable." echo "如果 SSID 隐藏或扫描不可用,可设置 WIFI_SKIP_SCAN=true。" echo "Available networks:" iwlist "$WIFI_INTERFACE" scan 2>/dev/null | grep "ESSID:" | grep -v "ESSID:\"\"" | sort | uniq exit 1 fi fi else while true; do read -r -p "Enter WiFi SSID: " SSID read -r -p "Enter WiFi Password: " PASSWORD echo "WiFi Configuration:" echo "SSID: $SSID" echo "Password: ********" read -r -p "Is this correct? (y/n): " CONFIRM if [[ "$CONFIRM" =~ ^[Yy]$ ]]; then SCAN_RESULT=$(iwlist "$WIFI_INTERFACE" scan 2>/dev/null | grep -iF "ESSID:\"$SSID\"") if [ -n "$SCAN_RESULT" ]; then break else echo "Error: WiFi network '$SSID' not found in available networks." echo "Please check the SSID and try again." echo "Available networks:" iwlist "$WIFI_INTERFACE" scan 2>/dev/null | grep "ESSID:" | grep -v "ESSID:\"\"" | sort | uniq echo "Please re-enter the WiFi credentials..." fi else echo "Please re-enter the WiFi credentials..." fi done fi rm -rf /etc/wpa_supplicant/wpa_supplicant.conf wpa_passphrase "$SSID" "$PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf if [ ! -f /etc/systemd/system/wpa_supplicant.service ]; then cat > /etc/systemd/system/wpa_supplicant.service << EOF [Unit] Description=WPA supplicant Before=network.target After=dbus.service Wants=network.target IgnoreOnIsolate=true [Service] Type=dbus BusName=fi.w1.wpa_supplicant1 ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant/wpa_supplicant.conf -i $WIFI_INTERFACE Restart=always [Install] WantedBy=multi-user.target Alias=dbus-fi.w1.wpa_supplicant1.service EOF systemctl daemon-reload fi if ! grep -Fxq "auto $WIFI_INTERFACE" /etc/network/interfaces; then if grep -q "^iface $WIFI_INTERFACE_REGEX inet \(auto\|static\|manual\)" /etc/network/interfaces; then echo "Found existing iface configuration for $WIFI_INTERFACE, commenting it out..." cp /etc/network/interfaces /etc/network/interfaces.backup sed -i "/^iface $WIFI_INTERFACE_REGEX inet \(auto\|static\|manual\)/,/^$/s/^/#/" /etc/network/interfaces fi if grep -q '^iface vmbr0 inet static' /etc/network/interfaces && \ ! grep -A 5 '^iface vmbr0 inet static' /etc/network/interfaces | grep -q '^\s*metric\s\+100'; then sed -i '/^iface vmbr0 inet static$/a\ metric 100' /etc/network/interfaces fi cat >> /etc/network/interfaces << EOF auto $WIFI_INTERFACE iface $WIFI_INTERFACE inet static address 192.168.124.144/24 gateway 192.168.124.1 dns-nameservers 144.144.144.144 metric 10 EOF # auto vmbr1 # iface vmbr1 inet static # address 172.16.100.1 # netmask 255.255.255.0 # bridge_ports none # bridge_stp off # bridge_fd 0 # post-up echo 1 > /proc/sys/net/ipv4/ip_forward # post-up iptables -t nat -A POSTROUTING -s '172.16.100.0/24' -o $WIFI_INTERFACE -j MASQUERADE # post-down iptables -t nat -D POSTROUTING -s '172.16.100.0/24' -o $WIFI_INTERFACE -j MASQUERADE # EOF echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf sysctl -p echo "Added network interface configuration for $WIFI_INTERFACE" else echo "Network interface $WIFI_INTERFACE already configured" fi ifup "$WIFI_INTERFACE" sleep 5 ip link set "$WIFI_INTERFACE" up sleep 5 systemctl restart networking sleep 5 echo "Waiting for network to stabilize..." sleep 5 if [ ! -f /usr/local/bin/dns-setup.sh ]; then cat > /usr/local/bin/dns-setup.sh << 'EOF' #!/bin/bash sleep 50 DNS_SERVER="144.144.144.144" RESOLV_CONF="/etc/resolv.conf" rfkill unblock wifi sleep 10 WPA_STATUS=$(systemctl is-active wpa_supplicant 2>/dev/null) if [ "$WPA_STATUS" != "active" ] && [ "$WPA_STATUS" != "activating" ]; then systemctl start wpa_supplicant || systemctl restart wpa_supplicant fi if ! grep -q "^nameserver 8.8.8.8$" ${RESOLV_CONF}; then echo "nameserver 8.8.8.8" >>${RESOLV_CONF} fi if ! grep -q "^nameserver 144.144.144.144$" ${RESOLV_CONF}; then echo "nameserver 144.144.144.144" >>${RESOLV_CONF} fi sleep 5 systemctl restart networking sleep 5 systemctl restart wpa_supplicant EOF chmod +x /usr/local/bin/dns-setup.sh fi cat > /etc/systemd/system/dns-setup.service << EOF [Unit] Description=DNS Setup Service (One-time) After=network-online.target Wants=network-online.target [Service] Type=oneshot ExecStart=/usr/local/bin/dns-setup.sh User=root RemainAfterExit=yes [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable dns-setup.service DNS_SERVER="144.144.144.144" RESOLV_CONF="/etc/resolv.conf" rfkill unblock wifi sleep 3 WPA_STATUS=$(systemctl is-active wpa_supplicant 2>/dev/null) systemctl start wpa_supplicant sleep 5 if ! grep -q "^nameserver 8.8.8.8$" ${RESOLV_CONF}; then echo "nameserver 8.8.8.8" >>${RESOLV_CONF} fi if ! grep -q "^nameserver 144.144.144.144$" ${RESOLV_CONF}; then echo "nameserver 144.144.144.144" >>${RESOLV_CONF} fi sleep 3 echo "Restarting wpa_supplicant and networking services..." systemctl restart wpa_supplicant sleep 5 systemctl restart networking echo "Configuration completed. Rebooting in 15 seconds ..." sleep 15 && reboot