Facebook

Play a song as soon as someone comes online


Today we are going to create a very simple and small script which plays a song when someone starts their system i.e. as soon as they come online (Of-course you both should be in same network. i.e their system should be reachable form yours) .

So have you heard of ping command. Yes? Great. We will use this command only.



Man page description:
ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway. ECHO_REQUEST datagrams (''pings'') have an IP and ICMP header, followed by a struct timeval and then an arbitrary number of ''pad'' bytes used to fill out the packet.

Here is the 23 line script which achieve this target for you.

count=0

if [ $# -lt 1 ]; then
    echo "Please pass the IP address as parameter"
    exit 2
fi

while [ true ]
do
    ping -c 2 -W 5 $1 1> /dev/null 2> /dev/null
    if [ `echo $?` -eq '0' ]; then
        echo 
        echo "--------- User is online -------"  `date`
        play ht.mp3 1> /dev/null 2> /dev/null
        exit 2
    else 
        clear
        count=$(($count+1))
        echo
        echo "--------- User is offline ------ Attempt -" $count  `date`
    fi
    
    sleep 100
    
done


-c 2  : sends 2 packets.
-W 5 : timeout of 5 minutes.
$1 : argument passed to script.

copy this code in a empty file and save with any_name.sh, place in some directory.

Now you need to do is to place a music file in same directory in which this script file is.
Replace the name of music file with the name highlighted in Red. Make sure play is recognized as a valid command in your terminal. If not then you need to install sox.

What is sox? Its a swiss army knife for audio manipulation in Linux. You can achieve almost anything related to audio with it. Concatenate audio file, Play an audio file in reverse, mix two files etc etc.

To install sox into your system, go to terminal and type this command
$  sudo apt-get install sox
Now to enable sox to play mp3 files as well just run this below mentioned command as well.
$  sudo apt-get install sox libsox-fmt-all

you are done with sox. Now to check if it is working fine or not, go to any directory where your music files are located.
$ cd "Desktop/Crazy Heart Soundtrack"
Run the command
$ play filename.mp3

it will start playing songs in terminal. To skip to next song press ctrl+c, to abort the player press ctrl+c twice.


Instead of sox, you can also use mplayer command, if already installed.

Ok. Now back to the script.
To run it, go to command prompt. and type this.
$ sh  your-script-file-name.sh  [ip-address]

If you are not providing any Ip address then it will inform you about the same and exit gracefully. [line 4 and 5]

otherwise it pings the given Ip and check if command is successfully executed or not. If yes then it print that user is online, plays the song and exit.
Otherwise it will attempt every 100 second. For every attempt it prints the attempt number and the date-time.

1> /dev/null is used to send all output of command to null device i.e. output is not printed on terminal.
2> /dev/null is used to send all error messages to null device i.e. no error message is printed on terminal.

these both above are just for decoration purpose so that when command runs, terminal should look neat and clean and only messages you want to display gets printed there.

How to know if someone is sniffing you using ping command:


How to know if someone is sniffing you using this above mentioned technique.
There is a command for that too. Just copy paste it into you terminal and hit enter.

$ sudo tcpdump -i wlan0 icmp and icmp[icmptype]=icmp-echo


It will list all packets that reached to your system along with Ip address.


If there is any query regarding this script or in general. Leave a comment.


Credit - Ashutosh Gupta.

No comments:

Post a Comment