magic_shutdown: Shutdown computer remotely using Magic Packet

The title says it all. If you don’t know what a Magic Packet is then read here. Below is the code of a shell script (I call magic_shutdown) that uses tcpdump to listen for Magic Packets. When it receives on then the shell script verifies its content to make sure that this packet was meant for this computer. Note this script doesn’t require that your network interface card support Wake-on-LAN. It has no special hardware dependencies.

All-in-all, this script lets you create create a shortcut which will act as remote power button for your computer. Press it to turn the computer ON. Press it again to turn the computer OFF.

Get magic_shutdown‘s code
License: GNU Public License version 3.

[code lang=”bash”]#!/bin/bash
#Author:AppleGrew
#License:GPL version 3

listenPort=9
interface="eth0"

#Forking to daemonize…
if [[ "$2" != "forked" ]]
then
echo "Forking $0…"
"$0" "$1" forked &
echo "Forked."
exit 0
fi

#Creating pid file
ppid=$$
echo $ppid >"$1"

echo "Started"
mac=`ifconfig "$interface"|head -n1|sed -e ‘s/.*HWaddr \([0-9:a-fA-F]*\)/\1/g’ -e ‘s/://g’`
pckt_expect=`echo "ff ff ff ff ff ff $mac $mac $mac $mac $mac $mac $mac $mac"|sed ‘s/ //g’|tr ‘A-Z’ ‘a-z’`
while `true`
do
pckt_data=`tcpdump -i "$interface" -x -c 1 udp port ${listenPort}`
if [[ $? != 0 ]]
then
echo "tcpdump returned error."
exit 1
fi

pckt_data=`echo "$pckt_data" | \
grep ‘0x[0-9]*:’| \
tr ‘A-Z’ ‘a-z’| \
sed ‘s/[ \t]//g’| \
sed ‘s/0x[0-9]*:\([0-9a-f]*\)/\1/g’| \
tr -d ‘\n\r’| \
cut -c 57-`
if [[ "$pckt_data" == "$pckt_expect" ]]
then
echo "Matched! Received Magic packet shutting down…"
shutdown -P now #Not recommended
#For Gnome #Doesn’t work dunno why?
#dbus-send –session –dest=org.gnome.PowerManager \
# –type=method_call –print-reply –reply-timeout=2000 \
# /org/gnome/PowerManager org.gnome.PowerManager.Shutdown
#For KDE 3.5
#dcop `dcop|grep power-manager` power-manager shutdown
exit 0
fi
done

echo "EXITED"
exit 0[/code]

Below is the startup script that must be used to launch magic_shutdown script.

Get launch_magic_shutdown‘s code
License: GNU Public License version 3.

[code lang=”bash”]#!/bin/bash
#Author:AppleGrew
#License:GPL version 3

SCRIPT="/opt/magic_shutdown"
PID_FILE="/var/run/magic_shutdown.pid"
case "$1" in
start)
test -f "$PID_FILE" && echo "Already Running…" && exit 1
"$SCRIPT" "$PID_FILE"
echo "Started"
;;
stop)
pid=`cat "$PID_FILE"`

tcpPid=`pgrep -P $pid tcpdump`
kill -9 $pid
kill -2 $tcpPid
if [ -f "$PID_FILE" ] && ! ps -p $pid >/dev/null
then
rm -f "$PID_FILE"
else
echo "Failed to delete pid file. Maybe its already deleted."
fi
echo "Stopped"
;;
esac[/code]

Installations:-
Assuming that you have downloaded the above two codes into your home directory. Now run the following commands.

sudo cp launch_magic_shutdown /etc/init.d
sudo chmod a+x /etc/init.d/launch_magic_shutdown
cd /etc/rc2.d
sudo ln -s ../init.d/launch_magic_shutdown S99launch_magic_shutdown

sudo cp magic_shutdown /opt/magic_shutdown
sudo chmod a+x /opt/magic_shutdown

Hope this helps. Report in the comments section if you encounter any problem.


Update: Stuart’s Script
Stuart (see comment section) posted his version of magic_shutdown script. If the above one doesn’t work then maybe you should give this one a try.
[code lang=”bash”]#!/bin/sh
#Author: Stuart
#Original Author:AppleGrew
#License:GPL version 3

#Forking to daemonize…
if [[ "$2" != "forked" ]]
then
echo "Forking $0…"
"$0" "$1" forked &
echo "Forked."
exit 0
fi

#Creating pid file
ppid=$$
echo $ppid >"$1"

echo "Started"
interface=`route -n | grep "^0.0.0.0" | awk -F " " ‘{print $8}’`
mac=`ifconfig "$interface"|head -n1|sed -e ‘s/.*HWaddr \([0-9:a-fA-F]*\)/\1/g’ -e ‘s/://g’`
pckt_expect=`echo "$mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac $mac"|sed ‘s/ //g’|tr ‘A-Z’ ‘a-z’`
while `true`
do
pckt_data=`tcpdump -i "$interface" -s 0 -x -c 1 \( \(ether dst "$mac" and not ip and not arp and not rarp\) or \(udp port 9\) \)`
if [[ $? != 0 ]]
then
echo "tcpdump returned error."
exit 1
fi
pckt_data=`echo "$pckt_data" | \
grep ‘0x[0-9]*:’| \
tr ‘A-Z’ ‘a-z’| \
sed ‘s/[ \t]//g’| \
sed ‘s/0x[0-9]*:\([0-9a-f]*\)/\1/g’| \
tr -d ‘\n\r’ | \
awk -F "ffffffffffff" ‘{print $2}’`
if [[ "$pckt_data" == "$pckt_expect" ]]
then
echo "Matched! Received Magic packet shutting down…"
rm -f $1
/sbin/poweroff
exit 0
fi
done

echo "EXITED"
exit 0[/code]

Turn on (power on) your computer remotely

I was scanning the net today for ways to turn on any computer remotely. The solution I stumbled upon was Wake-On-LAN (WOL). (Read more) What’s more good, is that most of the computers sold today support this feature.

To know if your system supports this feature or not run the following command

sudo ethtool eth0

You will get output similar to the one below.

Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: MII
PHYAD: 32
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000007 (7)
Link detected: yes

In the above please substitute eth0 with the network interface card’s name you want to listen for “Magic Packets“. The Supports Wake-on gives the methods your card supports to wake your computer up. In the output above, this card supports waking on any physical activity, on unicast message, on multicast message, on broadcast message and “Magic Packets” respectively. A value of d for Supports Wake-on means that your card doesn’t support WOL. (For the full list of meanings for these letters see ethtool‘s manpage under the wol portion.) In the output above Wake-on is g, which means that WOL for eth0 is enabled and is set to listen for “Magic Packets” only.

Useful links:-

  1. On Wkipedia, has steps to enable WOL via Windows.
  2. How-to for Ubuntu users.
  3. Very useful resource on WOL.
  4. Shutdown remote computers using Magic Packets.

Posting PHP codes on blog.

If you want to post PHP code on blog or any web page then its easy. Use the command:-

php -s /path/to/your/php/script > The_Code_to_post

This will generate a html code that you can directly paste in the web page’s code to display your code with syntax highlighting, but if you post this code in Blogger.com or Drupal then you will notice extraneous blank lines after every line of your code. This is due the fact that Blogger.com and Drupal insert a line break themselves whenever they encounter a new-line or carriage-return. To fix this problem, use the following command instead of the above one.

php -s /path/to/your/php/script | tr -d ‘\n\r’ > The_Code_to_post

Dataone – BSNL Broadband: Common problems and quick fixes.

I am a BSNL Dataone user and as any BSNL user knows that in case of problem u can turn towards BSNL for help. So, I guess I should help fellow users to let know the solutions to common problems I faced or face.

problem When trying to dial and connect to BSNL you get Error 678 – Remote computer is not responding.
sol Make sure that the link light of your modem (actually router) is ON (green). If not then wait for about 2 mins. If still not contact ur BSNL local exchange.

If the link light is lit happily then make sure the router is connected to ur computer. Now turn OFF the router. And now turn it ON. Wait for link light to turn ON. Now try. It should connect now.

problem I get Invalid Username or Password and I am pretty sure that they r correct.
sol Nothing. It happened to me sometimes. Just keep redialing.

problem The check usage site doesn’t open.
sol Then goto alternative portals of the same site. Try any one of the followings.
http://10.240.43.216/
http://10.240.89.199/
http://10.240.16.195/
http://10.240.0.195/

problem Connection gets lost after a power cut though u r using an UPS.
sol My router (Huawei MT882) has this problem. The reason I guess is that it is extremely sensitive to slight distruption of power which happens when online UPSs switch to battery mode from AC mode. So, getting an offline UPS may help.

Importing iTunes playlist ino Winamp, Windows Media Player or any other player.

I don’t like iTunes much, but I still use it, to syncronise my iPod Shuffle. I have hand-picked the songs on my harddrive and created a playlist in iTunes, which I sync with my iPod. The problem is, due to iTunes’ inability to save its playlist in anything but a cryptic XML or text format, it cannot be imported into any other player. I have now chanced upon a simple method to do that.

Open your intended target player (e.g. Winamp). Select all the songs in iTunes that you want to add to target player’s playlist (press Ctrl+A after clicking on any song entry in iTunes, to select all the songs). Now left click on any of the selected song in iTune and drag (while keeping your mouse button still pressed) to the playlist of the target player. Now all these songs have been added there!

Making folders invisible in Windows – The easy way.

My friend Kaustubh suggested an ingenious way hiding your folders in Windows. The method is easy to follow for any user. I have modified it a bit to make the folder completely disappear. It will be right in front of you but u will not be able to located it, unless you have been told that there is a folder there. See below for example.

Locate the folder in the screenshot below.

Did u find it? See below.


Now did u find it? U can open the folder like any ordinary folder, i.e. by double-clicking it.

The method:-
1) First rename the folder. (Right-click on the folder and choose ‘rename’ or select the folder and just press F2 on ur keyboard.) You must rename the folder to blank but Windows doesn’t allow you to name your files or folders just blank. Hence u need to name ur folder with a character which appears to be blank but it is not a blank. Confused? Well just follow me.
a) Select the folder and press F2 on your keyboard.
b) Make sure NumLock of your keyboard is on. (You do this by making sure that the LED corresponding to your NumLock is glowing).
c) Now press Alt on your keyboard and while keeping it pressed, type 255 on your NumPad. NOTE: You must use the number keys on your NumPad to type in 255 not the number keys on the top row of the alphabet keys.
d) Now press Enter key on your keyboard.

2) Now after making the file name to disappear, it is time to make the icon disappear.
a) Right click on that folder and then left click on Properties.
b) Now left click on Customize.
c) Left click on Change Icon button.
d) A new dialog will pop-up. Scroll the icons list and choose the blank icon. (There are 4 blank icons.) Refer the picture below.


e) Now click on OK. Click on OK in the following dialog too.

Now u have an invisible folder! Enjoy!