Skip to content

Instantly share code, notes, and snippets.

@raphink
Created November 4, 2014 15:53
Show Gist options
  • Save raphink/aa3e27b58a9c6fb7f68a to your computer and use it in GitHub Desktop.
Save raphink/aa3e27b58a9c6fb7f68a to your computer and use it in GitHub Desktop.
#!/bin/sh
IP="192.168.0.1"
HOST="myhost.mydomain"
ALIAS="myhost"
augtool -sAt "Hosts.lns incl /etc/hosts" << EOC
# \$noentry will match /files/etc/hosts if the entry isn't there yet
defvar noentry /files/etc/hosts[count(*[ipaddr="${IP}" or canonical="${HOST}"])=0]
# Create the entry if it's missing
set \$noentry/01/ipaddr "${IP}"
set \$noentry/01/canonical "${HOST}"
set \$noentry/01/alias "${ALIAS}"
# Amend existing entry
defvar entry /files/etc/hosts/*[ipaddr="${IP}" or canonical="${HOST}"]
# This will fail if more than one entry was found
set \$entry/ipaddr "${IP}"
set \$entry/canonical "${HOST}"
# Remove all aliases
rm \$entry/alias
set \$entry/alias "${ALIAS}"
EOC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment