Skip to content

Instantly share code, notes, and snippets.

@ake-persson
Last active August 29, 2015 14:15
Show Gist options
  • Save ake-persson/6df93ee3d7f17aed70ba to your computer and use it in GitHub Desktop.
Save ake-persson/6df93ee3d7f17aed70ba to your computer and use it in GitHub Desktop.
Generate dynamic hostname based on hwaddr and aspell dict
#!/bin/bash
out_of_bounds() {
local num=$1 max=$2
while [ $num -gt $max ]; do
num=$(( $num - $max ))
done
echo $num
}
generate_hostname() {
local hwaddr=$1
# Generate dictionary
aspell dump master | sed -e "s/\W//g" -e 's/\(.*\)/\L\1/' >$TMPFILE
max=$(cat $TMPFILE | wc -l)
# Strip out hwaddr separator
stripped=${hwaddr//[:-]/}
# Split hwaddr in two parts
part1=${stripped:0:6}
part2=${stripped:6:6}
# Convert hex to a dec number
num1=$((16#${part1}))
num2=$((16#${part2}))
# Fix number if it's out of bounds
num1=$(out_of_bounds $num1 $max)
num2=$(out_of_bounds $num2 $max)
# Lookup word in dictionary
word1=$(tail -n+$num1 $TMPFILE | head -n1)
word2=$(tail -n+$num2 $TMPFILE | head -n1)
printf '%s-%s' $word1 $word2
}
TMPFILE='/tmp/aspell_dict'
hwaddr=$(cat /sys/class/net/eth0/address)
echo $(generate_hostname $hwaddr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment