您的位置:首页 > 运维架构 > Shell

同步集群单板时间的shell脚本

2013-05-13 11:06 447 查看

0.readme.txt

完成功能:以当前服务器时间为标准时间,同步时间给ip.txt文件中列出的服务器
需要同步的IP:添加到ip.txt(注意:不能把linux本机IP放到里面)
执行前配置:ntp.conf中 server 122.138.73.8(修改为linux本机IP)
执行用户:root
执行路径:/root/bin(建议放在该路径下执行)
执行命令:./synctime.sh

ps:为了执行同步时间的正确性,延长远程执行时间为1分钟,主要用于判断时间是否真正同步成功
另外借用了颜色区分显示执行结果,让执行者更快看到执行情况

1.copyall.sh

#!/usr/bin/expect
#set timeout 15
spawn scp -r path1/file USER@IPS:path2
expect {
"yes/no" { send "yes\r"; exp_continue }
"Password:" { send "PASSWD\r";exp_continue }
"#" {send "\r"}
}

2.ip.txt

IP USER PASSWD
122.138.168.45 root hello123

3.ntp.conf

################################################################################
## /etc/ntp.conf
##
## Sample NTP configuration file.
## See package 'ntp-doc' for documentation, Mini-HOWTO and FAQ.
## Copyright (c) 1998 S.u.S.E. GmbH Fuerth, Germany.
##
## Author: Michael Andres, <ma@suse.de>
## Michael Skibbe, <mskibbe@suse.de>
##
################################################################################

##
## Radio and modem clocks by convention have addresses in the
## form 127.127.t.u, where t is the clock type and u is a unit
## number in the range 0-3.
##
## Most of these clocks require support in the form of a
## serial port or special bus peripheral. The particular
## device is normally specified by adding a soft link
## /dev/device-u to the particular hardware device involved,
## where u correspond to the unit number above.
##
## Generic DCF77 clock on serial port (Conrad DCF77)
## Address: 127.127.8.u
## Serial Port: /dev/refclock-u
##
## (create soft link /dev/refclock-0 to the particular ttyS?)
##
# server 127.127.8.0 mode 5 prefer

##
## Undisciplined Local Clock. This is a fake driver intended for backup
## and when no outside source of synchronized time is available.
##
server 10.137.168.41 # local clock (LCL)
fudge 127.127.1.0 stratum 10 # LCL is unsynchronized

##
## Add external Servers using
## # rcntp addserver <yourserver>
##

##
## Miscellaneous stuff
##

driftfile /var/lib/ntp/drift/ntp.drift # path for drift file

logfile /var/log/ntp # alternate log file
# logconfig =syncstatus + sysevents
# logconfig =all

# statsdir /tmp/ # directory for statistics files
# filegen peerstats file peerstats type day enable
# filegen loopstats file loopstats type day enable
# filegen clockstats file clockstats type day enable

#
# Authentication stuff
#
keys /etc/ntp.keys # path for keys file
trustedkey 1 # define trusted keys
requestkey 1 # key (7) for accessing server variables
# controlkey 15 # key (6) for accessing server variables

4.ntp_test.sh

#!/bin/ksh
for((i=1;;i++))
do
echo -e "\033[40;36mntp sync begin \033[0m"
ntpd -g -q >ntp.out
len=`du -s ntp.out|awk '{print $1}'`
if [ $len -gt 0 ];then
cat ntp.out
echo -e "\033[40;36mntp sync success! \033[0m"
exit
else
if [ $i -ge 3 ];then
echo -e "\033[40;36mntp sync failed,pls check your linux environment \033[0m"
exit
else
echo -e "\033[40;36mntp sync failed,try again! \033[0m"
fi
fi
done

5.ssh_test.sh

#!/usr/bin/expect -f
set timeout 60
spawn ssh -l USER IPS
expect {
"yes/no" { send "yes\r"; exp_continue }
"Password:" { send "PASSWD\r";exp_continue }
"#" {send "\r"}
}
expect "#"
send "cd path2\r"
send "rcntp stop\r"
send "sh ntp_test.sh\r"
send "date\r"
expect eof

6.synctime.sh

#!/bin/ksh
status=`service ntp status|grep "network time"|awk -F: '{print $2}'|grep running|wc -l`
#echo $status
if [ $status != 1 ];
then
echo -e "\033[40;36mstart ntp service...\033[0m"
rcntp start
i=21
for((i=$i;i--;i>=1))
do
if [ $i -le 0 ];then
echo -e "\033[40;36mntp service normal running\033[0m"
break;
fi
echo -e "\033[40;36mntp service normal running at $i seconds later\033[0m"
sleep 10
i=`expr $i - 9`
done
fi
path1=`pwd`
path2='/etc'
cat ip.txt |grep -v IP|while read line
do
IP=`echo $line |awk '{print $1}'`
USER=`echo $line |awk '{print $2}'`
PASSWD=`echo $line |awk '{print $3}'`
cp copyall.sh copyall.sh.bak
cp ssh_test.sh ssh_test.sh.bak
eval sed -i 's/IPS/$IP/g' `grep -rl IPS ssh_test.sh copyall.sh`
eval sed -i 's/USER/$USER/g' `grep -rl USER ssh_test.sh copyall.sh`
eval sed -i 's/PASSWD/$PASSWD/g' `grep -rl PASSWD ssh_test.sh copyall.sh`
eval sed -i 's:path1:$path1:g' `grep -rl path1 ssh_test.sh copyall.sh`
eval sed -i 's:path2:$path2:g' `grep -rl path2 ssh_test.sh copyall.sh`
for file in ntp.conf ntp_test.sh
do
cp copyall.sh copyall.sh.bak2
eval sed -i 's/file/$file/g' `grep -rl file copyall.sh`
expect -b copyall.sh
mv copyall.sh.bak2 copyall.sh
done
expect -b ssh_test.sh
mv copyall.sh.bak copyall.sh
mv ssh_test.sh.bak ssh_test.sh
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: