Skip to content

Instantly share code, notes, and snippets.

@Manouchehri
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manouchehri/dffa79ebc87727361e87 to your computer and use it in GitHub Desktop.
Save Manouchehri/dffa79ebc87727361e87 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Purpose: Write the passed in parameter as hostid to /etc/hostid
# If no parameter is passed, write current hostid to /etc/hostid
# Original Author: Fazle Arefin <fazlearefin@yahoo.com>
# http://fazlearefin.blogspot.ca/2013/03/set-hostid-etchostid-of-linux-hosts.html
# Modified by David Manouchehri <david@davidmanouchehri.com>
if [ -n "$1" ]; then
host_id=$1
# chars must be 0-9, a-f, A-F and exactly 8 chars
egrep -o '^[a-fA-F0-9]{8}$' <<< $host_id || exit 1
a=${host_id:6:2}
b=${host_id:4:2}
c=${host_id:2:2}
d=${host_id:0:2}
echo -ne \\x$a\\x$b\\x$c\\x$d > /etc/hostid && echo "Success" 1>&2
else
host_id=$(hostid)
printf 'Usage: sethostid [hostid]\nNote: The hostid must only contain valid hex values (0-9, a-f and A-F) and be exactly 8 char long.\n'
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment