Skip to content

Instantly share code, notes, and snippets.

@jirutka
Last active October 21, 2016 00:21
Show Gist options
  • Save jirutka/3a72ebf1906a50dbaba8c2264e16ffb3 to your computer and use it in GitHub Desktop.
Save jirutka/3a72ebf1906a50dbaba8c2264e16ffb3 to your computer and use it in GitHub Desktop.
This script adds “primary” IP addresses (both IPv4 and IPv6) of the machine to /etc/issue (text for login screen). Add it to /etc/network/if-post-up.d and /etc/network/if-post-down.d.
#!/bin/sh
msg_file='/etc/issue'
test_ips='208.67.222.222 2620:0:ccc::2'
my_ips=$(printf '%s\n' $test_ips \
| xargs -rn 1 ip route get 2>/dev/null \
| grep -v unreachable \
| sed -En 's/.* src ([0-9a-f.:]+) .*/\1/p' \
| xargs)
if [ -z "$my_ips" ]; then
sed -i '/^IP /d' $msg_file
elif grep -q '^IP ' $msg_file; then
sed -i "s/IP .*/IP $my_ips/" $msg_file
else
sed -i '${/^$/d;}' $msg_file
printf '%s\n\n' "IP $my_ips" >> $msg_file
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment