Display IP address in banner text

Rather then use the console when working on a system I generally prefer to connect remotely using ‘ssh’ as it is so much easier to be able to cut and paste from a terminal session on my workstation that way.

The problem is that if you can’t remember the machine’s IP address (or name) to find it you need to run ‘ifconfig’, and the only way to do that is to login in to the console.  Not a big deal but is gets a bit tedious after a while.  What I really wanted was to have the IP address of the machine to be displayed on the console when the machine booted so I would not have to login in order to find out what it was.

Screen shot
I discovered that when a network interface is brought up any script that is the ‘/etc/network/if-up.d’ folder is executed (provided it is marked executable and properly named), and that when the network is shutdown the system will run any scripts in ‘/etc/network/if-down.d’.

My script below is based on this example but I’ve developed it further to allow it to restore the banner message to its original state when the network is shut down.

# nano /etc/network/if-up.d/sh-update-address

# /etc/network/if-up.d/sh-update-address
# Checks to see if the current folder is on a file system with less than the
# specified percentage of free space and prints a warning if it is.
# For a description of the environment vairables  that can be sued by network
# scripts refer to 'man interfaces'.
# 24 Nov 16 - 0.1   - Initial version - MEJT
# 26 Jul 17 - 0.2   - Updated for Debian (stretch) - MEJT

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>

# If '/etc/issue.std' does not exist then create if from '/etc/issue'.
if [ ! -e /etc/issue.std ]; then
  if [ -e /etc/issue ]; then mv /etc/issue /etc/issue.std; fi

if [ "$METHOD" != loopback ]; then # Ignore the loop back adapter.
  if [ "$MODE" = start ]; then
    # Check that an IP address is defined
    if [ "$ADDRFAM" = inet ] || [ "$ADDRFAM" = inet6 ]; then
      if [ -e /etc/issue.std ]; then # Update banner text if it exists
        cp /etc/issue.std /etc/issue
        /sbin/ifconfig | grep "inet " | grep -v "" | sed "s/[^0-9. ]*//g" |tr -s " " | cut -f 2 -d " " >> /etc/issue
        # /sbin/ifconfig | grep "inet addr" | grep -v "" | cut -d ":" -f 2 | cut -d " " -f 1 >> 
        printf "\n" >> /etc/issue
    if [ "$MODE" = stop ]; then
      if [ -e /etc/issue.std ]; then cp /etc/issue.std /etc/issue ; fi

After you have created the script you need to make it execuitable

# chmod +x /etc/network/if-up.d/sh-update-address

This script will append the IP address of a machine to the text in ‘/etc/issue’, when the network comes up.  This may be all you need but I found it slightly confusing to have the old IP address shown in the login banner when the network had been shutdown, so when run from ‘/etc/network/if-down.d’ the script will restore ‘/etc/issue’ to it’s original state.

To make this happen all you need to do is simply create a link in ‘/etc/network/if-down.d’ to the original script in ‘/etc/network/if-up.d’.

# ln /etc/network/if-up.d/sh-update-address \
> /etc/network/if-down.d/sh-update-address

This entry was posted in Debian, Linux, Raspbian, Ubuntu and tagged , , , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s