Show active network interface and current IP address at TTY console login prompt
I have several virtual linux guests running on VirtualBox: a bunch of test machines to which I connect exclusively via ssh from my host terminal.
Virtualbox setting for their network adapter is usually set as bridged, in order to acquire the IP address from the DHCP server of the network I am connected to.
I find it annoying to login on the console after every boot to check the current IP address, then logging out and connect via ssh.. that’s why I came up with the following script. Essentially it appends (‘echoes’) the currently connected network interfaces (and relative IP) to the welcome message showed at tty console login prompt after boot (/etc/issue
). Some little trick was needed to preserve the original content of /etc/issue
and being pleasant to the eye.
Save it somewhere on your virtual machine filesystem (a good place could be /etc/init.d/
), make it executable (chmod +x /etc/init.d/boot_prompt.sh
) and then add a reference to its full path into /etc/rc.local
just before the exit 0
line:
/etc/init.d/boot_prompt.sh
exit 0
#!/bin/bash
# Filename: boot_prompt.sh
# Description: Shows connected interface(s) at boot and related ip address(es) on tty login prompt
# Usage: Put it somewhere (ie. /etc/init.d/) and then add its full path entry to /etc/rc.d/rc.local
#
# Author: ops[at]agate[dot]pw
# Version: 201304051644
DSTFILE="/etc/issue"
KEEPLINE=$(head -n 1 ${DSTFILE})
IFACE_UP=$(/sbin/ifconfig | grep 'Bcast:' -B1 | grep -v inet | grep -v - -- | awk '{print $1}')
echo ${KEEPLINE} | sed 's/\\\\/\\/g' > ${DSTFILE}
echo >> $DSTFILE
if [ -z "$IFACE_UP" ]; then
echo "\n No active connection found." >> $DSTFILE
else
echo "Currently connected via:">> $DSTFILE
for IFACE in $IFACE_UP;
do IPADDR=$(/sbin/ifconfig $IFACE | grep 'Bcast:' | cut -d: -f2 | awk '{ print $1}')
echo " $IFACE $IPADDR" >> $DSTFILE
done
fi
echo >> $DSTFILE
exit 0
Gist available here: https://gist.github.com/korovamilk/5320187 Screenshot of an example boot prompt: