Linux shell 脚本在网络接口重置期间“卡住”

Linux shell 脚本在网络接口重置期间“卡住”

我正在编写一个 Linux Shell 脚本来帮助重置系统上的网络设备,这比输入 5 条命令要快一些。问题是脚本会间歇性地卡住并返回错误,指出设备“忙”。我收到此消息没有任何逻辑原因?有人可以看看这个并指出我哪里出错了吗?

#!/bin/bash

# Check if script is being run as root
if [ "$(id -u)" != "0" ]; then
    echo "Please run this script with sudo."
    exit 1
fi

# Function to check if a device is in monitor mode
is_monitor_mode() {
    monitor_mode=$(iwconfig $1 2>/dev/null | grep "Mode:Monitor")
    if [ -n "$monitor_mode" ]; then
        return 0 # Device is in monitor mode
    else
        return 1 # Device is not in monitor mode
    fi
}

# Enumerate available network interfaces
interfaces=$(ip link show | grep '^[0-9]' | awk -F': ' '{print $2}')

# Ask user which device to reset

echo "";
echo "Network Interface Reset Tool"
echo "===================================="
echo "Please select an interface to reset."
echo ""
echo "Available network interfaces:"
echo ""
select interface in $interfaces "Reset all devices" "Cancel"; do
    case $interface in
        "Reset all devices")
            for iface in $interfaces; do
                sudo ifconfig $iface down
                
                echo "Resetting $iface"
                if is_monitor_mode $iface; then
                    read -p "Device $iface is in monitor mode. Do you want to return it to managed mode? (y/n): " choice
                    if [ "$choice" = "y" ]; then
                        sudo iw dev $iface set type managed
                        echo "$iface returned to managed mode"
                    fi
                    sudo ifconfig $iface up
                fi
            done
            echo "All devices reset"
            break
            ;;
        "Cancel")
            echo "Operation canceled"
            exit
            ;;
        *)
            sudo ifconfig $interface down
            sudo ifconfig $interface up
            echo "Reset $interface"
            if is_monitor_mode $interface; then
                read -p "Device $interface is in monitor mode. Do you want to return it to managed mode? (y/n): " choice
                if [ "$choice" = "y" ]; then
                    sudo iw dev $interface set type managed
                    echo "$interface returned to managed mode"
                fi
            fi
            break
            ;;
    esac
done

# Ask if user wants to restart networking and NetworkManager
echo "Restart networking and NetworkManager?"
select option in "Restart networking and NetworkManager" "Start networking only" "Start NetworkManager only" "No, don't restart"; do
    case $option in
        "Restart networking and NetworkManager")
            sudo systemctl start networking
            sudo systemctl restart NetworkManager
            echo "Networking Daemon and NetworkManager restarted"
            break
            ;;
        "Start networking only")
            sudo systemctl start networking
            echo "Networking restarted"
            break
            ;;
        "Start NetworkManager only")
            sudo systemctl restart Network-Manager
            echo "NetworkManager restarted"
            break
            ;;
        "No, don't restart")
            echo "Operation canceled"
            break
            ;;
    esac
done

相关内容