Getting Your (Annoyingly Dynamic) IP Address Emailed To You Every Day

Standard

I recently moved house and with that had to adjust to a new internet service that is unfortunately not as reliable as my old one. As I rely on my raspberry pi as a remote media server, I need to know what the IP address is, which is a bit of a pain when it keeps changing while you’re out the house.

I started thinking “Maybe there’s a way I could automate the emailing of my IP address once a day” so I started investigating CLI mailers.

Initially I looked at sendmail but it seemed a little bit overkill for what I needed, and I got a little bit lost in the configuration options, but perhaps that’s just my poor attention span.

In the end I went for ssmtp and followed this guide for setting it up with my email address (I used a gmail account)*.

The next step was setting the pi to obtain the external ip address, and for this I used curl with checkip.dyndns.org and sent the output to a text file called todaysip.txt. This can be put in a script with the ssmtp email command, and it looks like this:

#!/bin/sh
date > date.txt && curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//' > todaysip.txt && paste date.txt todaysip.txt >> today.txt && tac today.txt | mail -s "IP Address" recipient@emailprovider.com

The next step was setting crontab to run the script at midday everyday. Job’s a good ‘un.**

If there’s a more elegant way of achieving the same result, I’d be interested in hearing it as this is a bit of a quick ‘n’ dirty solution.

*You will need to set-up a separate app password if you use Gmail’s 2-factor authentication.
**It took me a bit of troubleshooting to get this to work, but ultimately I used /bin/sh rather than /bin/bash – See source below

EDIT: Added date stamp to script.  Probably in an overkill kinda way.

Sources:

1. http://iqjar.com/jar/sending-emails-from-the-raspberry-pi/

2. http://askubuntu.com/questions/95910/command-for-determining-my-public-ip

3. http://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work

Advertisements

Script for connecting to Raspbmc

Standard

All my music and videos are stored on a drive connected to a Raspberry Pi running Raspbmc, and as I used a Acer C720 (ex)Chromebook1 with only 16GB of disk space, I tend to stream stuff over my home network.  For this, I use SSHFS and libnotify:

echo media_password | sshfs mediaonly@ip_address:/media/Media_Folder /home/user/pi -o follow_symlinks,password_stdin,uid=1000,gid=1000,umask=0 &&
notify-send "The Raspbmc box is connected yo"

Once connected, libnotify sends a message to the desktop.  It’s not ideal: the script stores the password in plain text, however the permissions are restricted and that user has no sudo rights.  I might see if I can tweak it to prompt for a password using Zenity perhaps.  I’ll update this when I do.

UPDATE: Duh – SSH keys is the way to go.

1. Got rid of ChromeOS and using Mint instead.

Creating a hacky linux digital signage system

Standard

Background

In my workplace there was a digital signage system set up by a commercial operator.  It displayed videos, slides and webpages, and had a rather attractive content management system.  Weirdly, rather than sitting atop a stable OS, it used an old version of Fedora – bleeding edge wobbliness no longer supported by security updates – nice.

One day, a portable appliance tester switched it off, and upon his departure it failed to reboot.  After reading the logs and contacting the company, they suggested sending it back for £180.  Being from Yorkshire, I found this unacceptable so I started looking for alternatives.

First up was Xibo which looked pretty promising, but after a few false starts and actually reading the documentation, this had to be run on Ubuntu 10.04 which is again, quite an old base, plus it seemed quite a ball-ache getting the dependencies for a Debian base (my preferred option for stability).  After getting it installed on my hardware, the video was choppy – too choppy to be of use.

I checked out a couple more options like Concerto and some others (didn’t quite meet my specifications) I had a thought: What do I actually want this system to do?  Well, I want it to show a webpage for about 30 seconds, and then play a folder of media and I want to be able to run on pretty low spec hardware.  The first thing I did was work out my ‘player’ – a shell script that does the stuff I want:

#!/bin/bash
DISPLAY=:0.0
while true; do
timeout -k 31 30 midori -e Fullscreen -a http://website/
cvlc --play-and-exit --fullscreen --no-osd --playlist-tree /home/user/player/*
done

The first line specifies the display in case I want to start it from SSH, followed by a While loop to… loop the script.  ‘Timeout’ is a handy application to run processes for a set period of time.  Here, it’s for 30 seconds, with the -k (kill) option killing the process after 31 seconds in case it doesn’t stop for whatever reason.  I initially used Chromium as the browser in kiosk (fullscreen, no decorations) mode, but then found that the lighter Midori browser was snappier.

VLC kicks ass – that’s a given, and here I’m using the command line version CVLC.  By default, it plays videos and importantly, slides/images for 10 seconds when in a playlist, so it didn’t need any configuration from me (you can however change the image display time in the GUI version).  The flags I have in there are pretty straightforward: --play-and-exit errm… plays and exits, --fullscreen shows the media in fullscreen mode, --no-osd hides the filenames from showing and --playlist-tree enables a playlist mode (which I didn’t end up using).

This script worked, although there was a brief showing of the desktop between VLC stopping and Midori starting, so I turned this into a feature.  As my base system was Linux Mint 17 XFCE I set the panel to hide and set the desktop background to some corporate branding.  Huzzah!  Now I get a graphical branding sting and everything looks intentional!

As the box itself would be stuffed into a roofspace area, I set up an autologin to the X session and made sure I had openssh-server installed for access purposes.  I also stuck Vino VNC server on there so that I could have a check what was playing remotely.

VGA and Plasma Screens – Annoying Resolution Issue

My testing of everything with a regular monitor was fine, however when plugged into the plasma screen, the correct resolution1 (1366×768) was not detected.  I could correct this by creating a mode in xrandr but this would revert back on each reboot – time for another script.

Another issue was that the mouse pointer was showing on the screen during the Midori bit, so I installed a ‘hide the cursor’  programme called Unclutter and threw that in the script as well:


#!/bin/bash
xset -dpms off &&
xset s noblank &&
xset s off &&
unclutter -idle 0 &
xrandr --newmode "1366x768_60.00" 85.25 1368 1440 1576 1784 768 771 781 798 -hsync +vsync &&
xrandr --addmode VGA1 "1366x768_60.00" &&
xrandr --output DVI1 --off --output VIRTUAL1 --off --output LVDS1 --off --output VGA1 --mode 1366x768_60.00 --pos 0x0 --rotate normal &&
sleep 8s &&
plasmaplayer

So, in order, we hide the cursor, create and add a new display mode, switch to that display mode, wait 8 seconds and start my player script.

Users and Adding Media

At first, I figured our regular users could use something like Filezilla to connect and copy their images and videos to the playlist folder, so I wrote a guide for them to do that.  And then I thought a better solution would be to mount a network folder via an entry in /etc/fstab.  This now means that our users can just dump their stuff in a specific folder, and sure enough, it then appears on the screen.


//SERVER/Path/To/Shared/Folder404/Plasma_Playlist/ /home/user/player cifs username=Blahh,password=obvious,sec=ntlm,noserverino,rw,file_mode=0777,dir_mode=0777 0 0

This didn’t work at first because of a space in the network path (“Folder 4”).  As the fstab file uses spaces you have to replace them with a 40.  It took me a while to work out what was wrong, so I figured this could be useful for someone else with the same issue.

Extra Bits

I wanted a nice splash screen on reboot so I chose the lovely Numix Plymouth theme (I love Numix!) which is a pleasing red background and a swooshing line.

Edit 2: One of the screens kept blanking every so often so I had to add the ‘xset’ bits to the boot up script.

1. Although the screen could do ‘Full HD’ with HDMI, the max setting for VGA was this. I’m not sure why but never mind.