Skip to content

Instantly share code, notes, and snippets.

@samrocketman
Forked from rm/show-ip-before-login.sh
Last active March 28, 2016 12:55
Show Gist options
  • Save samrocketman/9d6621d5a46c537e60df to your computer and use it in GitHub Desktop.
Save samrocketman/9d6621d5a46c537e60df to your computer and use it in GitHub Desktop.
Show IP address on prelogin message
#!/bin/bash
# based on http://offbytwo.com/2008/05/09/show-ip-address-of-vm-as-console-pre-login-message.html
# based on https://gist.github.com/rm/3780228
# A better IP address method https://www.linuxquestions.org/questions/blog/sag47-492023/using-a-script-to-get-your-ip-address-35251/
# need to be root to write out these files
if [ "$(id -u)" != "0" ]; then
echo "Need to run as root. Maybe use sudo..."
exit
fi
GET_IP_ADDRESS='/usr/local/bin/get-ip-address'
if [ ! -f "${GET_IP_ADDRESS}" ]; then
cat >"${GET_IP_ADDRESS}" <<EOF
#!/bin/sh
#get ip address based on default route
ip -o ro get \$(ip ro | awk '\$1 == "default" { print \$3 }') | awk '{print \$5}'
EOF
chmod 0755 "${GET_IP_ADDRESS}"
fi
if [ ! -f '/etc/issue-standard' ]; then
cp '/etc/issue' '/etc/issue-standard'
cat >'/etc/network/if-up.d/show-ip-before-login' <<EOF
#/bin/sh
if [ "\${METHOD}" = "loopback" ]; then
exit 0
fi
# Only run from ifup.
#if [ "\${MODE}" != "start" ]; then
# exit 0
#fi
grep -v "^\$" /etc/issue-standard >/etc/issue
echo -n " " >>/etc/issue
$GET_IP_ADDRESS >>/etc/issue
echo "" >> /etc/issue
EOF
chmod 0755 /etc/network/if-up.d/show-ip-before-login
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment