【问题标题】:How to upgrade scripts from iwconfig to iw?如何将脚本从 iwconfig 升级到 iw?
【发布时间】:2021-04-21 20:42:27
【问题描述】:

我有这个脚本来打印一个格式化的字符串,代表我的实际 wifi 连接及其强度:

#! /run/current-system/sw/bin/nix-shell
#! nix-shell -i fish -p fish

set active (ip addr | awk '/state UP/ {print $2}' | sed 's/.$//' | head -n 1)

if test -n $active
    set essid (iwconfig $active | awk -F '"' '/ESSID/ {print $2}')
    set strength (iwconfig $active | awk -F '=' '/Quality/ {print $2}' | cut -d '/' -f 1)
    set bars (expr $strength / 10)
else
    echo "No device up"
    exit 1
end

switch $bars
    case 0
        set bars "[----------]"
    case 1
        set bars "[/---------]"
    case 2
        set bars "[//--------]"
    case 3
        set bars "[///-------]"
    case 4
        set bars "[////------]"
    case 5
        set bars "[/////-----]"
    case 6
        set bars "[//////----]"
    case 7
        set bars "[///////---]"
    case 8
        set bars "[////////--]"
    case 9
        set bars "[/////////-]"
    case 10
        set bars "[//////////]"
    case "*"
        set bars "[----!!----]"
end

echo $essid $bars

它工作正常,但是我将我的 Linux 发行版更改为 NixOS,其中不推荐使用 iw* 命令,因此我们只有 iw 命令。

在我的搜索中,我发现了这个命令:iw <interface> station dump,它显示了很多信息,但现在正是我需要的。

它没有显示我的实际AP,信号 ios 显示为带负数的 dBm。

如何将旧的iwconfig 命令升级到iw?有可能吗?

【问题讨论】:

    标签: linux shell fish


    【解决方案1】:

    基于这个superuser answer,我们有以下内容:

    cfg80211 wext 兼容层假定信号范围为 -110 dBm 到 -40 dBm,质量值是通过在信号电平上加上 110 得出的

    因此,考虑到这一点,以下内容(基于 iw 命令的示例输出):

    set essid (iw dev $active link | sed -n 's/[[:space:]]*SSID:[[:space:]]*//p')
    set dbstrength (iw dev $active link | awk '$1 ~ /signal:/ { print $2 }')
    set quality (math -s0 \( 110 + $dbstrength \) \* 10 / 70)
    

    会给你一个max out为 10 的值,但你几乎永远不会达到 10,因为它会向下取整。如果您希望它对 9.6 等值进行四舍五入,请在该值上加上 0.5,然后再相乘,例如

    $ set dbstrength -41
    $ math -s0 \( 110 + $dbstrength \) \* 10 / 70
    9
    $ math -s0 \( \( 110 + $dbstrength + 0.5 \) \* 10 \) + 0.5
    10
    

    【讨论】:

    • 这更有意义。我不知道iw dev <interface> link!这解决了我的问题。
    • 能否请您也为SSID添加解决方案?我解决为:set essid (iw dev $active link | awk '$1 ~ /SSID/ { $1=""; print $0 }')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多