如何使用ip命令确定接口是否为以太网

时间:2019-07-11 23:22:01

标签: shell unix networking posix

当我尝试使用ip命令确定所选接口是否用于以太网时,它在WiFi接口和以太网接口下均显示ether,如何确定哪个是以太网接口?

我已经开始编写一些程序来替换我没有完全使用的其他较大的程序,并且正在着手编写程序来替换nm-applet。我正在编写的程序是用100%UNIX Shell脚本编写的(不是BASH,ZSH,FISH等,而是UNIX Shell脚本)。我已经能够弄清我需要解决的所有问题,但是在解决它时遇到了一些麻烦。因此,该程序的概述如下(我仅添加了概述,以便您可以更好地了解该程序的工作原理和应该做什么),所有带有-的问题都已解决:

# Outline:

* list available network interfaces -

* ask which interface the user would like to use -

* check if the interface is for Ethernet

* check if the selected interface is up or down, if down set the interface up -

* scan for available networks -

* ask which network the user would like to use -

* check if the network is open or requires a password

* if the network is open connect, else if the network is WPA (closed) then ask for the password

* associate the interface with desired network -

* connect to the network -

* if the script is run with the -s option, then save the network information in wpa_supplicant -

我正在努力解决check if the interface is for Ethernet。当我开始写这篇文章时,我从ifconfig命令转到了ip命令,因为ip更易于解析和格式化。我遇到的问题是,我需要检查ip给定的接口是否用于ethernet。以便了解我运行的ip输出:

ip link show

但是在我的WiFi卡和我的以太网卡下都有:

link/ether

我的问题是我该如何实际检查该接口是否实际上是使用ip的以太网接口?我知道如何使用ifconfig来做到这一点,但无法在ip上弄清楚。

我当前的脚本如下:

#!/bin/sh

save_nwid=0

while [ $# -gt 0 ]; do
    key="$1"
    case "$key" in
        -s|--save)
            save_nwid=1
            shift
            ;;
    esac
done

setup_interface() {
    # list network interfaces
    iface=$(ip link show | awk '{if (NR % 2 == 1) print $2}' | sed -e 's/://' | awk -F: '{printf "%s, ",$1 }' | sed 's/\(.*\),/\1/')

    # pick interface
    echo "interfaces found: $iface"
    echo "which interface would you like to use? "

    read -r inet

        # check if interface is Ethernet (this is not how to do it)
        ip link show "$inet" | grep "ether"

    # check if interface is up or down
    state=$(ip link show "$inet" | awk '{print $9}')

    if [ "$state" = DOWN ]; then
        sudo ip link set "$inet" up
    fi
}

scan_networks() {
    # scan for networks
    echo "scanning available networks..."
    nets=$(sudo iw $inet scan | grep 'SSID' | sort -u | awk -F: '/SSID/ {printf "%s\n",$2 }' | sed -e '/^SSID/d' -e '/^[[:space:]]*$/d')
    printf "networks found:\n%s\n" "$nets"
}

connect_network() {
    # pick SSID and SSID Password
    echo "which network would you like to connect to? "
    read -r ssid

    echo "what is the network password? "
    read -r key

    # associate the interface with desired SSID
    sudo iwconfig "$inet" essid "$ssid"

    # save connection details if -s passed
    if [ $save_nwid -eq 1 ]; then
        save_network
    fi
}

save_network() {
    sudo sh -c ' echo
    "network {
        ssid="$ssid"
        psk="$psk"
    }" ' >> /etc/wpa_supplicant/wpa_supplicant.conf
}

check_connection() {
    ping_cmd="ping"
    ping_url="duckduckgo.com"

    $ping_cmd -c 3 $ping_url > /dev/null
    if [ $? -eq 0 ]; then
        echo "connection made."
    else
        echo "connection failed."
    fi
}

setup_interface
scan_networks

connect_network
check_connection

我期望IP链接显示在{if1}中有一个Ethernet,但遗憾的是没有。我不确定ether是否意味着两者都可以用于以太网,但是我对此表示怀疑。

0 个答案:

没有答案