본문으로 바로가기

NTP 서버 변경 스크립트

category Linux/Bash Script 2022. 8. 10. 14:00
반응형

RHOSP의 Compute 노드들의 NTP 서버 주소를 변경해야할 일이 있어 만들었던 ntp 변경 스크립트입니다.

 

[root@hk-vm ~]# cat ntp-change.sh
#!/bin/bash

sleep 1

# ntp.conf backup file check
backup=/etc/ntp.conf-backup

if [ ! -e $backup ]; then
	sudo cp -pr /etc/ntp.conf /etc/ntp.conf-backup
	echo "###################################"
	echo " $backup file backup successed....."
	echo "###################################"
else
	echo "$backup file exist.! "
fi

echo -e "\n"

sleep 1

# tinker add
tinker=`cat /etc/ntp.conf | grep step | awk -F' ' '{print $2}'`

if [ -z $tinker ]; then
	sed -i'' -r -e "/disable monitor/i\tinker step 0.5" /etc/ntp.conf
	echo "###################################"
	echo "tinker step 0.5 add successed...   "
	echo "###################################"
else
	echo "tinker step 0.5 is exist.!"
fi

echo -e "\n"

sleep 1


# original ntp server # Add and OSC#1 ~ OSC3 IP ADD
orig=`cat /etc/ntp.conf | grep -c "^server 100.100.100.9 iburst"`

ntp_ser_ip_list="55.55.24.5 20.20.20.20 30.30.30.30"

echo
echo "ntp server list are ${ntp_ser_ip_list}"
echo

if [ $orig == 1 ]; then
        sed -i 's/^server 100.100.100.9 iburst/#server 100.100.100.9 iburst/' /etc/ntp.conf
        for ntp_ser_ip in ${ntp_ser_ip_list}
        do
        sed -i'' -r -e "/#server 100.100.100.9 iburst/i\server ${ntp_ser_ip} iburst" /etc/ntp.conf
        done
else
        echo "osc#1 ~ 3 already exists. or please check the ntp.conf file"
fi

echo -e "\n"
sleep 1


#server=$(cat /etc/ntp.conf | grep -i "^server")

echo "###################################"
#echo $server
cat /etc/ntp.conf | grep -i "^server"
echo "###################################"
echo -e "\n"

sleep 1


# /etc/sysconfig/ntpd backup file check
ntpd=/etc/sysconfig/ntpd-backup

if [ ! -e $ntpd ]; then
	sudo cp -pr /etc/sysconfig/ntpd /etc/sysconfig/ntpd-backup
	echo "###################################"
	echo "$ntpd file backup successed.."
	echo "###################################"
else
	echo "$ntpd file exist.! "
fi

echo -e "\n"
sleep 1


# change info in ntpd file.
change=`cat /etc/sysconfig/ntpd | grep -c "ntpd.pid"`

if [ $change == 0 ]; then
	sed -i 's/^OPTIONS="-g"/#OPTIONS="-g"/g' /etc/sysconfig/ntpd
	sed -i 's/^#OPTIONS="-g"/OPTIONS="-x -u ntp\:ntp -p \/var\/run\/ntpd\.pid -g"/g' /etc/sysconfig/ntpd
	echo "###################################"
	echo "$ntpd info change successed.."
	echo "###################################"
else
	echo "already ntpd file include ntpd.pid info.."
fi

echo -e "\n"
sleep 1


systemctl restart ntpd
systemctl status ntpd

sleep 1
echo -e "\n"


# Configuration hwclock
hwclock=`ntpq -np | grep -c "^*"`
end=1
if [ $hwclock == "0" ]; then

	while [ $hwclock -lt $end ]
	do
		echo
		hwclock=`ntpq -np | grep -c "^*"`
		echo "##### now ntpq -np status...####"
		echo
		ntpq -np
		sleep 1
	done

fi

sleep 1
echo -e "\n"


# Setting hwclock
hwclock -w && echo && hwclock && echo && date

sleep 3

# Check NTP Status
ntpq -np

echo -e "\n"

 

 

 

728x90