Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there any way to play a sound to alert me when a wifi(wireless) network is detected?

share|improve this question
    
When detected or when connected? –  Radu Rădeanu Sep 15 '13 at 18:55
    
when a netork is detected –  M.Tarun Sep 16 '13 at 6:14
2  
In brief, for when you get connected, you can combine the answers from these posts: How to run a script every time internet connects and Sound notification in Ubuntu terminal –  Radu Rădeanu Sep 16 '13 at 7:09

2 Answers 2

up vote 6 down vote accepted
+50

The problem is that there is no a specific program/service/daemon to check when a new wireless network appear/disappear in/from the range of the wireless antenna as far as I know (in fact there is airodump-ng and maybe others, but this conflicts with Network Manager).

As alternative, you can scan for wifi networks from time to time, using iwlist tool.

Note: In tests iwlist tool may only work with Atheros, Intel or Broadcom cards.

These being said, the following script can do what you wish (I named it 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

Note: You have to change in script username with your user name and wlan0 with your interface name of your wireless network card; you can find it using 'ls /sys/class/net' command.

Don't forget to make the script executable, using the following command in terminal:

chmod +x /path/to/scripts/wifidetect

Finally, edit the crontab entries for root using sudo crontab -e command by adding the following line:

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

I have set the cron job for every minute, but you can change as you wish or as you think is better. See http://en.wikipedia.org/wiki/Cron in this sense.

share|improve this answer
    
Does this script execute every time I start my computer? –  M.Tarun Sep 21 '13 at 6:40
    
@M.Tarun Have you read the answer until the end? If you add a cron job for this script, the script will be executed from time to time as you wish. For example */1 * * * * /path/to/scripts/wifidetect will execute the script at every minute. –  Radu Rădeanu Sep 21 '13 at 7:27
    
When I run the script from the terminal it beeps but after that it doesn't make the sound –  M.Tarun Sep 21 '13 at 12:17
    
I mean that when I turn off the wifi and turn it on again it doesn't make the sound. –  M.Tarun Sep 21 '13 at 12:28
    
@M.Tarun That's because no new networks appeared in this time. If you want this you need another script to empty the content of $HOME/.networks when you turn off or on the wifi. –  Radu Rădeanu Sep 21 '13 at 12:42

Cuttlefish could be a good solution yet it doesn't send any stimuli when a new connection is detected (it only does when you connect to or disconnect from it)

another solution would be to write a script using this command:

iwlist <your interface> scan

Where <your interface> is your wifi network interface, wlan0 for instance.
This will display a list of all available wifi connection with detailed information: address, SSID, quality, encryption...

You would have to watch for variation in the output of this command and play your sound when a new network appears using beep, cvlc or mplayer.
The first one will play a simple bip while the two others will allow to play sound files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.