【问题标题】:assign wifi device name to variable将 wifi 设备名称分配给变量
【发布时间】:2018-10-24 17:42:32
【问题描述】:

我正在运行以下命令来确定当前正在使用哪个网络接口。

networksetup -listallhardwareports | grep -C1 $(route get default | grep interface | awk '{print $2}')

如何使用该命令并解析出该接口的设备名称,然后将其与 Wi-Fi 接口的设备名称进行比较?


所以我运行上面的命令,这就是我得到的:

Hardware Port: Ethernet
Device: en0
Ethernet Address: a8:60:b6:03:0a:97

这告诉我我的以太网端口或设备 en0 是我计算机上的活动网络端口。我想将 en0 分配给一个变量,比如 currentPort。

我也会运行这个命令:

networksetup -listallhardwareports

结果如下:

Hardware Port: Ethernet
Device: en0
Ethernet Address: a8:60:xx:xx:xx:xx

Hardware Port: Wi-Fi
Device: en1
Ethernet Address: 2c:f0:xx:xx:xx:xx

从此我想为我的 wifi 提取设备名称,在本例中为 en1 并将其分配给另一个变量,例如 wifiPort。

所以我应该有 currentPort=en0 和 wifiPort=en1。我希望然后我可以运行一个 if then 命令说 currentPort 与 wifiPort 不同,做一件事,如果 currentPort 与 wifiPort 相同,做其他事情。

希望这有助于更好地解释事情。


这就是我现在正在使用的,基于您的帮助。 echo $currentport 和 echo $wifiport 没有给我任何输出,并且使用 if 命令,无论我使用什么设备,它都会回显“它们是相同的”。

#!/bin/bash

#currentport=`grep -A3 'Ethernet' t6.txt | grep "Device"| awk -F": " '{print $2}'`
currentport=`echo $a|grep -A3 'Ethernet'| grep "Device"| awk -F": " '{print $2}'`
echo $currentport

#wifiport=`grep -A3 'Wi-Fi' t5.txt | grep "Device"| awk -F": " '{print $2}'`
wifiport=`echo $b|grep -A3 'Wi-Fi'| grep "Device"| awk -F": " '{print $2}'`
echo $wifiport

if [[ $currentport == $wifiport ]]
then
    echo They are the same
else
    echo They are different
fi

exit 0

【问题讨论】:

  • 示例输入和预期输出?
  • 我为使用的命令添加了一些示例输出。我不清楚的是你想要的结果。请澄清所需的输出。
  • 我想获取 wifi 端口的设备名称(en0、en1 等)以及当前使用的网络接口的设备名称,并将它们分配给变量……最终的游戏是如果设备正在使用 wifi 连接,则运行部分脚本;如果设备使用的不是 wifi,则退出脚本。也许有更好的方法来做到这一点?
  • @kbrem 请在上面的问题中添加预期的输出,并使用适当的样本输入。它会让你的问题更清楚。
  • @Mayank Porwal 我更新了原始问题,希望能澄清我想要做什么。

标签: macos unix


【解决方案1】:

试试这个:

对于currentport

假设您的networksetup 命令的输出存储在一个文件中:t6.txt

Hardware Port: Ethernet
Device: en0
Ethernet Address: a8:60:b6:03:0a:97

这样做:

mayankp@mayank:~/ currentport=`echo $a|grep -A3 'Ethernet'| grep "Device"| awk -F": " '{print $2}'`
mayankp@mayank:~/ echo $currentport
en0

对于wifiport

假设您的networksetup 命令的输出存储在一个文件中:t5.txt

Hardware Port: Ethernet
Device: en0
Ethernet Address: a8:60:xx:xx:xx:xx

Hardware Port: Wi-Fi
Device: en1
Ethernet Address: 2c:f0:xx:xx:xx:xx

这样做:

mayankp@mayank:~/ wifiport=`echo $b|grep -A3 'Wi-Fi'| grep "Device"| awk -F": " '{print $2}'`
mayankp@mayank:~/ echo $wifiport
en1

现在,您拥有两个变量:$currentport$wifiport

使用if 做你想做的事:

if [[ $currentport == $wifiport ]]
then
<do your stuff>
else
<do other stuff>
fi

如果这有帮助,请告诉我。

【讨论】:

  • 有没有办法在不创建 .txt 文件的情况下做到这一点......将变量存储在内存中?
  • 我刚刚在grep command 之前添加了echo $a 并从中删除了文件名。这应该解决它。让我知道它是否有效。
【解决方案2】:

我想我会避免使用所有这些管道,只需在 awk 中执行此操作:

awk 'NR==FNR&&/interface/{i=$2} NR==FNR{next} index($0,"Device: "i)' \
 <(route get default) \
 RS= \
 <(networksetup -listallhardwareports)

awk 脚本首先解析route 命令的输出以确定要使用的接口,然后将RS= 设置为强制awk 进入多行模式,从而允许将index() 命令用作打印条件记录。

如果你只想要 WiFi 端口,awk 脚本就更简单了,因为你不需要解析 route 命令:

awk 'index($0,"Hardware Port: Wi-Fi")' RS= <(networksetup -listallhardwareports)

如果您想比较结果,即确定您当前是否在使用 WiFi,很容易将您需要的命令封装在进程替换中。

#!/bin/bash

currentPort=$(
    awk '/interface/{print $2;exit}' <(route get default)
)
wifiPort=$(
    awk 'index($0,"Hardware Port: Wi-Fi") {
            sub(/.*Device: /,"");sub(/[[:space:]].*/,"")
            print; exit
        }' \
     RS= \
     <(networksetup -listallhardwareports)
)

if [[ "$wifiPort" = "$currentPort" ]]; then
    echo "I'm on WiFi!"
else
    echo "I'm NOT on WiFi!"
fi

当然,您也可以单独在 bash 中执行此操作,而无需 awk:

#!/bin/bash

junk=$(route get default)
[[ $junk =~ .*interface:\ ([a-z]+[0-9]+) ]]
currentPort="${BASH_REMATCH[1]}"

while read line; do
        if [[ $line = *Wi-Fi ]]; then
                read line
                wifiPort="${line##* }"
                break
        fi
done < <(networksetup -listallhardwareports)

if [[ "$wifiPort" = "$currentPort" ]]; then
        echo "I'm on WiFi!"
else
        echo "I'm NOT on WiFi!"
fi

请注意,while read line 循环并不是特别有效,而 bash 4 mapfile/readarray 的性能会更好。但是macOS中bash的默认版本还是3。:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多