Tuesday, January 13, 2015

Getting your local IP address in the GNU Screen status line

So I have this Digital Picture Frame made from an old laptop at work. It doesn't have that many uses other than to amuse myself and others in the form of a music player and an overpowered clock.

When I use it as a music player I use GNU Screen and Camp, the latter in combination with cp437 of course. This gives me the ability to log in remotely from my work computer to change track, volume and whatnot. The problem is having the correct local IP address of the machine since it gets updated every 8 hours. First I wrote a script that will check the local IP and send me an e-mail with the new one if there has been any changes. I run this script via cron once an hour.

But what I really wanted was to have it display in the status line of GNU Screen. It took some digging but I found this article which mentions an option called "backtick" in GNU Screen. In short backtick allows you to run a command from .screenrc and have it's output displayed in the status line.

An almost 20 year old laptop chugging along on Debian Wheezy playing
music and pulling of a look that might induce wet dreams to retro nerds.  

How to do it

I've added the following to my .screenrc
backtick 101 600 600 hostname -I

101 is the identifier of this particular backtick that will be used in the status line string, it could just as well be 1 or whatever number I choose.
600 and 600 determines how often to run the program and how often to refresh the line. I use the same number on both mainly because that's what the author of the article did and I don't see any reason to go beyond that. If you're curious read more about it here.
hostname -I this is the command I run to get my local IP address.

That's it for the command but you need to add want to display the output in the statusline:
hardstatus alwayslastline "%{= g} %{= w}%-Lw%{=r}%n%f* %t  %{-}%+LW %-=%| %101` | %D %M %d %Y %c:%s"

So really I just added %101` (yes the ` is required) to the string in order to display the output. That and two | and a couple of spaces to act as dividers.   

Now don't come around asking me questions on GNU Screen's status line because that is a whole other story. I barley know what mine does.