问题描述
我有这个脚本来打印一个格式化的字符串,代表我的实际 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。
解决方法
基于此 superuser answer,我们有以下内容:
cfg80211 wext compat 层假设信号范围为 -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)
会给你一个最大值为 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