달력

22025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

#!/bin/sh

 

NTPIP=1.1.1.1

DATE=`date +%Y%m%d%H`

OUTPUT="/tmp/ntpdate.log"

 

echo "################## NTP synchronization Start : [$(date +"%Y-%m-%d %H:%M:%S")] ##################"         >> $OUTPUT

echo "################## NTP synchronization Start : [$(date +"%Y-%m-%d %H:%M:%S")] ##################"

echo "/usr/sbin/ntpdate -d $NTPIP 2>/dev/null |egrep \"^originate timestamp|^transmit timestamp|^offset\""        >> $OUTPUT

echo "/usr/sbin/ntpdate -d $NTPIP 2>/dev/null |egrep \"^originate timestamp|^transmit timestamp|^offset\""

/usr/sbin/ntpdate -d $NTPIP 2>/dev/null |egrep "^originate timestamp|^transmit timestamp|^offset"            >> $OUTPUT

/usr/sbin/ntpdate -d $NTPIP 2>/dev/null |egrep "^originate timestamp|^transmit timestamp|^offset"

echo " "

echo " "                                                        >> $OUTPUT

/usr/sbin/ntpdate -d $NTPIP 2>/dev/null |egrep "^originate timestamp|^offset" |awk 'NF>1{print $NF}' | xargs -l2 | awk '{if ($2 < 5 && $2 > -5 && $2 != 0) { print "date -s "$1 } else { print "echo NTP NOT SYNC!! OFFSET : "$2}}' | bash

/usr/sbin/ntpdate -d $NTPIP 2>/dev/null |egrep "^originate timestamp|^offset" |awk 'NF>1{print $NF}' | xargs -l2 | awk '{if ($2 < 5 && $2 > -5 && $2 != 0) { print "date -s "$1 } else { print "echo NTP NOT SYNC!! OFFSET : "$2}}' | bash >> $OUTPUT

echo "################## NTP synchronization End : [$(date +"%Y-%m-%d %H:%M:%S")] ##################"             >> $OUTPUT

echo "################## NTP synchronization End : [$(date +"%Y-%m-%d %H:%M:%S")] ##################"

echo "

 

<스크립트 설명>

ntpdate -d IP

-> NTP 동기화 서버와의 시간 체크 진행

 

/usr/sbin/ntpdate -d 1.1.1.1 2>/dev/null |egrep "^originate timestamp|^offset"

originate timestamp: e3096d6b.6d815a7f  Mon, Sep 14 2020 13:17:47.427

offset 0.000663

-> NTP서버의 현재 시간과 offset(시간차이, 초단위) 결과만 도출

 

/usr/sbin/ntpdate -d 1.1.1.1 2>/dev/null |egrep "^originate timestamp|^offset" |awk 'NF>1{print $NF}' | xargs -l2

13:41:13.674 -0.019226

-> egerp 결과 값의 마지막 컬럼 결과 값 출력

 

/usr/sbin/ntpdate -d 1.1.1.1 2>/dev/null |egrep "^originate timestamp|^offset" |awk 'NF>1{print $NF}' | xargs -l2 | awk '{if ($2 < 5 && $2 > -5 && $2 != 0) { print "date -s "$1 } else { print "echo NTP NOT SYNC!! OFFSET : "$2}}' | bash

-> offset 결과 값을 비교하여 5초 이내, 5초 이하일 경우에는 date -s $1(NTP시간)으로 수동 시간 설정

-> 범위가 5초 초과일 경우에는 date 명령 미 수행

 

Posted by 짜꾸미의골골몽
|