Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Sunday, March 10, 2013

Shutdown Linux nicely with a USB stick

PLEASE NOTE

This post is sort obsolete since there is another way to shutdown linux nicely that requires less of everything. You can read about it here. However maybe you came for the script that looks for a file and issues a command. Well then you're in the right place, just scroll down a bit.

Shutdown Linux nicely with a USB stick

If I had such a thing like a following from regular readers you would probably know about my ancient Toshiba Satellite 320CDS laptop that now serves as a digital picture frame. The only problem is to have it shut down properly.

Why is that a problem you ask?
Well, I can't use the power button as it just cuts the power and I risk getting disk errors that most likely forces me to hook up a keyboard on the next boot. Something that I rather avoid.
But I can do it via a ssh connection. However that requires a working network connection and another device. That leaves me with the option I'd rather avoid. Connecting a keyboard and manually shutting the system down. Ok, not the end of the world but it's a bit tedious to have to drag out a keyboard each time I want to do a shut down.


My solution for this is a script that checks if a certain file is present in a defined directory. If the file is present the script issues the "shutdown -h now" command. The physical part of this is that the file is contained on a USB stick that when inserted automounts to a certain location. By running the script from root's crontab I now have a way to make Linux shut down nicely without a working network connection or keyboard.

Howto: Shutdown Linux nicely with a USB stick 

So what is needed for this to work? Well first of all here's my script. It's not very complicated and you should be able to tailor it according to your needs. Perhaps you want to run a slideshow with the pictures on a USB stick when inserted or maybe even completely wipe your system.

The next step is to make a USB stick automount in a certain directory.
For this we need to install autofs. There's good tutorial on this here and here, but I'll give you the basics below.

Since I run debian installation is as easy as:
$ sudo apt-get install autofs 

To configure autofs I started by adding this line to /etc/auto.master
/media   /etc/auto.removable     --timeout=2

Then I added this line to /etc/auto.removable
usb-shutdown -fstype=ext2 UUID=UUIDOFYOURDEVICE

The UUID is the Universally unique identifier of a disk. In this case we use it as way to know what USB stick to mount in /media/usb-shutdown/
You can find out the UUID of your device with:
$ ls -la /dev/disk/by-uuid/ 

This will give you a list of your devices with symlinks pointing to their labels i.e /dev/sda1 etc. Alternatively you could run the following command, substituting /dev/sda1 to your device.
$ sudo blkid /dev/sda1

Finally to make autofs recognize your changes run:
$ /etc/init.d/autofs restart

Last but not least we have to make the script run at a certain interval. This is done via the cron daemon. To edit root's crontab type following, if you want to edit the current user's crontab just omit the "sudo":
$ sudo crontab -e

Add the following lines:

# Check if USB device with "systemHaltNow" is present
  * *   *   *   *    /PATH/yourscript.sh >/dev/null 2>&1

The stars tell cron to run the script once every minute forever and ever and ever. The last part stops cron from e-mailing you and logging everytime it executes the script, more info on that here.

Well that's it.

Tuesday, September 22, 2009

Determine if user is logged in remotely or locally

Since I do all the maintenance on the digital picture frame via ssh I don't want the same login scripts to run when i log in remotely as when I log in locally. Well to be more specific I don't really log in locally but the user I have created logs in automatically when the DPF is turned on and from there on a number of things start. Needless to say I don't need to run "startx" everytime I log in via ssh.

So I wrote a small script to help me determine whether or not I was logged in locally or remotely (via ssh). The script is in its bare form here, I use it in my .bash_profile but you might find another use for it.

Important note: This script doesn't really determine if a user is logged in locally or remotely but rather using a tty or pts although the latter usually means that the user is logged in remotely, but not always. ;)
#!/bin/sh

#
# tty-vs-pts.sh
# tty vs pts checker by Markus Ulfberg 2009-09-22
# Inteded use is running different scripts wether
# the user logs in remotely or locally.
#

# Get the name of the tty the user is on.
FULL_USER_TTY=`tty`
# strip everything but tty or pts from the string that "tty" produces.
USER_TTY=${FULL_USER_TTY:5:3}

# Check if it is tty or pts.
if [ $USER_TTY == 'tty' ]
then
echo "You are logged in locally using: $FULL_USER_TTY"

else
if [ $USER_TTY == 'pts' ]
then
echo "You are logged in remotely using: $FULL_USER_TTY"
else
# Return failure if it is neither.
echo "Failed to establish type of tty"
fi
fi


Formatted for blogger using: formatmysourcecode.blogspot.com

Wednesday, May 13, 2009

Automated notification of updated of external IP

Updated 2013-03-16

Script stopped working due to changes in html2text this was resolved by adding curl as a dependency.

For my DSL line I get a dynamic IP address. On the inside it's quite easy to find out what the external IP is, but on the outside damn near impossible to find out what the IP might have changed to.
To get around that problem I wrote a little script that checks the external IP and sends me an e-mail if there has been any changes.
Dependencies: curl, html2text and ssmtp.
Source formatted by formatmysourcecode.blogspot.com and available for download on github here.
# /bin/sh

# ipquery.sh
# IP checker and updater by Markus Ulfberg 2009-05-12 

# Variables
# Path where ip.txt resides or will be created if it doesen't exist already
DIR=youdirhere
# E-mail address, both from and to
ADDRESS=your@emailadress.here
# E-mail Subject
SUBJECT="New IP number"

# Check for ip.txt and create if it doesn't exist. 
if [ ! -s $DIR/ip.txt ] 
    then 
        # Create ip.txt
        echo "ip.txt doesn't exist. Creating ip.txt ..."
        echo "0.0.0.0" > $DIR/ip.txt
    else
        echo "ip.txt exists. Continuing ..."
fi

    # Get current IP
    currentIP=`curl http://checkip.dyndns.org | html2text | grep Current | awk '{print$4}'`
    echo "Current IP: $IP"

    # Read old IP from file
    oldIP=`cat $DIR/ip.txt`
    echo "Old IP: $oldIP"

    # Compare current and old IP
    if [ $oldIP != $currentIP ]
        then
            echo "IP has changed to: $currentIP"
            # Write new IP to ip.txt
            echo $currentIP > $DIR/ip.txt
            # Set e-mail content to new IP
            CONTENT=$currentIP
            # Send the e-mail notification
            echo -e "FROM:$ADDRESS\nTO:$ADDRESS\nSUBJECT:$SUBJECT\nContent-type:text/plain\n$CONTENT\n" | `/usr/sbin/ssmtp $ADDRESS`
        else
            echo "IP is still: $currentIP"
        fi
exit 0