当检测到 wifi 网络时发出警报?

当检测到 wifi 网络时发出警报?

当检测到 wifi(无线)网络时,有什么方法可以播放声音来提醒我吗?

答案1

问题是,据我所知,没有特定的程序/服务/守护程序来检查新的无线网络何时在无线天线的范围内出现/消失(事实上有,airodump-ng也许有其他的,但这与网络管理器冲突)。

另外,您也可以扫描 wifi 网络时不时地使用iwlist工具。

笔记:测试中iwlist该工具可能只适用于 Atheros、Intel 或 Broadcom 卡。

话虽如此,下面的脚本可以做你想做的事(我将其命名为wifidetect):

#!/bin/bash

#wifidetect - Alert when new wireless 

#Licensed under the standard MIT license:
#Copyright 2013 Radu Rădeanu (http://askubuntu.com/users/147044/).
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

DISPLAY=:0.0
#change 'username' with your username
HOME=/home/username/
XAUTHORITY=$HOME/.Xauthority
export DISPLAY XAUTHORITY HOME

#check if you run the script as root
if [ "$(whoami)" != "root" ]; then
    notify-send -i error "You must to run $(basename $0) script as root."
    echo "You must to run $(basename $0) script as root."
    exit
fi

#check if beep is installed
if [ ! -n "$(dpkg -s beep 2>/dev/null | grep 'Status: install ok installed')" ]; then
    notify-send -i error "The package 'beep' must to be installed before to run $(basename $0)." "Use 'sudo apt-get install beep' command in terminal to install it."
    echo -e "The package 'beep' must to be installed before to run $(basename $0)\nUse 'sudo apt-get install beep' command in terminal to install it."
    exit
fi

[ -f /tmp/networks ] || > /tmp/networks

#scan for networks - need root privileges
#change 'wlan0' with your interface name of your wireless network card; you can find it using 'ls /sys/class/net' command
sudo iwlist wlan0 scan | grep -E "Address|ESSID" |  awk 'BEGIN {FS=": |\""} {print $2}' | sed 'N;s/\n/ /' | sort > /tmp/new_networks

#check if there are new networks
new_networks=$(comm -1 -3 /tmp/networks /tmp/new_networks)
new_essids=$(echo $new_networks | sed "s/..:..:..:..:..:..//g" | sed "s/  /, /g")

if [ -n "$new_networks" ]; then   #if there are new networks
    #send a graphical notification
    notify-send -i /usr/share/app-install/icons/wifi-radar.svg "New network(s) detected:" "$new_essids"
    #send a sound notification
    pcspkr_on=$(lsmod | grep pcspkr)
    if [ -n "$pcspkr_on" ]; then 
        beep
    else 
        sudo modprobe pcspkr
        beep
        sudo modprobe -r pcspkr
    fi

    #change the old list of networks with the list containing new networks
    #the networks that were detected in the previous scan and wern't detected this time will be removed from the list
    cat /tmp/new_networks > /tmp/networks

    #if you want that only the new networks detected (who were not detected in previous scans) to be added to list, comment the previous line and and uncomment the following three
    #cat /tmp/networks > /tmp/tmp_networks
    #cat /tmp/new_networks >> /tmp/tmp_networks
    #cat /tmp/tmp_networks | sort | uniq > /tmp/networks
fi

笔记:您必须在脚本中username使用您的用户名和wlan0无线网卡的接口名称进行更改;您可以使用“ls /sys/class/net”命令找到它。

不要忘记使脚本可执行,在终端中使用以下命令:

chmod +x /path/to/scripts/wifidetect

最后,使用命令编辑 root 的 crontab 条目,sudo crontab -e添加以下行:

*/1 * * * * /path/to/scripts/wifidetect

我已经设置了每分钟的 cron 任务,但你可以按照自己的意愿或你认为更好的方式进行更改。参见http://en.wikipedia.org/wiki/Cron在这个意义上。

答案2

Cuttlefish 可能是一个很好的解决方案,但它在检测到新连接时不会发送任何刺激(只有当您连接或断开连接时才会发送)

另一个解决方案是使用以下命令编写脚本:

iwlist <your interface> scan

例如<your interface>,您的 wifi 网络接口在哪里。 这将显示所有可用 wifi 连接的列表,其中包含详细信息:地址、SSID、质量、加密...wlan0

您必须注意此命令输出的变化,并在出现新网络时使用beepcvlc或播放声音mplayer
第一个将播放简单的 bip,而其他两个将允许播放声音文件。

相关内容