2018년 2월 5일 월요일

Change Linux hostid through shell script

check your current hostid
$ hostid
007f0101

make shell
$ vi hostid.sh

copy and paste below code
#!/bin/bash
#
# Purpose: Write the passed in parameter as hostid to /etc/hostid
#          If no parameter is passed, write current hostid to /etc/hostid
# Author:  Fazle Arefin [fazlearefin at yahoo dot 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
else
  host_id=$(hostid)
fi

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

exit 0

run script
# sh hostid.sh AABBCCDD
AABBCCDD
Success

If shell script doesn't work, maybe you should change mode (chmod)
# chmod 755 hostid.sh

check your changed hostid
$ hostid
AABBCCDD
참고 : https://github.com/fazlearefin/sysadm-scripts/blob/master/hostid.sh

댓글 없음:

댓글 쓰기