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

To achieve this I decided to take a copy of ‘/etc/issue’ and update it to include the IP address of each network interface as it comes up, the IP addresses are then displayed on the console with the login prompt. I’ve based my scripts on this example but I’ve developed things further to allow it to restore the banner message to its original state when the network is shut down.

How this works depends on the distribution as Debian and RedHat do things slightly differently. My solution it isn’t perfect, all the IP address information is cleared if any of the network interfaces are shutdown, and ‘/etc/issue’ isn’t always updated before the login prompt is shown on the console (pressing <enter> at the login prompt a few times causes solves this).

Debian/Ubuntu

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’.

To do this you need to begin by creating a new script.

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

Then paste in the following script which will append the IP address of a machine to the text in ‘/etc/issue’ when the network comes up, and if it is run again when the network goes down will restore ‘/etc/issue’ to it’s original state.

#!/bin/bash
#
# /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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# 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
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 "127.0.0.1" | sed "s/[^0-9. ]*//g" |tr -s " " | cut -f 2 -d " " >> /etc/issue
        # /sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | cut -d ":" -f 2 | cut -d " " -f 1 >> 
        printf "\n" >> /etc/issue
      fi
    fi
  else
    if [ "$MODE" = stop ]; then
      if [ -e /etc/issue.std ]; then cp /etc/issue.std /etc/issue ; fi
    fi
  fi
fi

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

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

To have the script run when the network shuts down you need to create a link in ‘/etc/network/if-down.d’ to ‘/etc/network/if-up.d’.

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

 

RedHat/CentOS

When a network interface is brought up the system will execute ‘/sbin/ifup-local’ if it exists and is marked executable, and ‘/sbin/ifdown-local’ when a network interface shuts down – However the scripts ‘/sbin/ifup-local’ and ‘/sbin/ifdown-local’ are only executed when the network devices are controlled by the /etc/init.d/network service, so if you are running RedHat 7 which uses ‘NetworkManager’ then these scripts will not be executed at startup (when I have tested a fix I’ll update this post but I’ll probably put a modified version of the script I used on Debian in ‘/etc/NetWorkManager/dispatcher.d/30-update-ip-address).

I also had some issues trying to obtain the IP address when executing ‘/sbin/ifup-local’, so I decided to update the IP address using a separate script ‘/sbin/if-update-address’.

Begin by updating each of the existing scripts.

# nano /sbin/ifup-local

#!/bin/bash
#
# /sbin/ifup-local
#
# Runs /sbin/if-update-issue in the background to update the IP address
# shown in the banner message on the console.
#
# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# 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 [ "$1" != lo ]; then # Ignore the loop back adapter.
  if [ -e /sbin/if-update-issue ]; then # Check script exists.
    /sbin/if-update-issue &
  fi
fi
#

# nano /sbin/ifdown-local

#!/bin/bash
#
# /sbin/ifdown-local
#
# Restores the banner message shown on the console to it's original
# state.
#
# 12 Dec 16 - 0.1   - Initial version - 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# 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
fi
 
# If '/etc/issue.std' exists then restore '/etc/issue' to it's original
# state.
if [ -e /etc/issue.std ]; then cp /etc/issue.std /etc/issue ; fi
#

Then you need to create a the new script to actually update ‘/etc/issue’.

# nano /sbin/if-update-issue

This script will wait six seconds (to allow the configuration of the interface to complete) and then attempt to get it’s IP address. If unsuccessful it will retry ten times before giving up.

#!/bin/bash
#
# /sbin/if-update-issue
#
# Updates the local banner message to include the IP address.
#
# 24 Nov 16 - 0.1   - Initial version - MEJT
# 12 Dec 16 - 0.2   - Updated for RedHat - MEJT
# 29 Mar 17 - 0.3   - Now runs in the background - 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# 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/>
#

_count=10 # Number of retries - don't want the script running forever!

# 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 
    cp /etc/issue.std /etc/issue
  fi
fi

if [ -e /etc/issue.std ]; then # Update banner text if it exists
  while [ -z "$_address" ] && [ $_count -gt 0 ]; do
    sleep 6
    _address=$(/sbin/ifconfig | grep "inet " | grep -v "127.0.0.1" | \
      sed "s/[^0-9. ]*//g" |tr -s " " | cut -f 2 -d " ")
    _count=$(($_count +1))
  done
  if [ -n "$_address" ]; then
    echo $_address >> /etc/issue
    printf "\n" >> /etc/issue
  fi
fi
#

Finally you need to make sure these scripts are executable.

# chmod +x /sbin/if-update-issue
# chmod +x /sbin/ifup-local
# chmod +x /sbin/ifdown-local

When you reboot you should see the IP address on the console.

Advertisements
This entry was posted in Debian, Linux, Raspbian, RedHat, 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