Thursday 26 September 2013

Get an alert sound when ever wifi is detected in your Ubuntu

 Only paste the bold ones and read care fully and then paste them
 Copy the below script and then create an empty document in the file explorer. Then paste the script in the document and save the document with .sh extension

       

  #!/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 $HOME/.networks ] || > $HOME/.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 $HOME/.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 > $HOME/.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 $HOME/.networks > /tmp/tmp_networks
    #cat /tmp/new_networks >> /tmp/tmp_networks
    #cat /tmp/tmp_networks | sort | uniq > $HOME/.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 command:
 'ls /sys/class/net'
  
Then use this command in the 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

No comments:

Post a Comment