0.readme.txt
完成功能:以当前服务器时间为标准时间,同步时间给ip.txt文件中列出的服务器
需要同步的IP:添加到ip.txt(注意:不能把linux本机IP放到里面)执行前配置:ntp.conf中 server 122.138.73.8(修改为linux本机IP)执行用户:root执行路径:/root/bin(建议放在该路径下执行)执行命令:./synctime.shps:为了执行同步时间的正确性,延长远程执行时间为1分钟,主要用于判断时间是否真正同步成功
另外借用了颜色区分显示执行结果,让执行者更快看到执行情况1.copyall.sh
#!/usr/bin/expect
#set timeout 15spawn scp -r path1/file 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 hello1233.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, <>## Michael Skibbe, <>####################################################################################
## 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 filetrustedkey 1 # define trusted keysrequestkey 1 # key (7) for accessing server variables# controlkey 15 # key (6) for accessing server variables4.ntp_test.sh
#!/bin/ksh
for((i=1;;i++))doecho -e "\033[40;36mntp sync begin \033[0m"ntpd -g -q >ntp.outlen=`du -s ntp.out|awk '{print $1}'`if [ $len -gt 0 ];then cat ntp.out echo -e "\033[40;36mntp sync success! \033[0m" exitelseif [ $i -ge 3 ];thenecho -e "\033[40;36mntp sync failed,pls check your linux environment \033[0m"exitelseecho -e "\033[40;36mntp sync failed,try again! \033[0m"fifidone5.ssh_test.sh
#!/usr/bin/expect -f
set timeout 60spawn ssh -l USER IPSexpect { "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 eof6.synctime.sh
#!/bin/ksh
status=`service ntp status|grep "network time"|awk -F: '{print $2}'|grep running|wc -l`#echo $statusif [ $status != 1 ];thenecho -e "\033[40;36mstart ntp service...\033[0m"rcntp starti=21for((i=$i;i--;i>=1))doif [ $i -le 0 ];thenecho -e "\033[40;36mntp service normal running\033[0m"break;fiecho -e "\033[40;36mntp service normal running at $i seconds later\033[0m"sleep 10 i=`expr $i - 9`donefipath1=`pwd`path2='/etc'cat ip.txt |grep -v IP|while read linedoIP=`echo $line |awk '{print $1}'`USER=`echo $line |awk '{print $2}'`PASSWD=`echo $line |awk '{print $3}'`cp copyall.sh copyall.sh.bakcp ssh_test.sh ssh_test.sh.bakeval 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 docp copyall.sh copyall.sh.bak2eval sed -i 's/file/$file/g' `grep -rl file copyall.sh`expect -b copyall.shmv copyall.sh.bak2 copyall.shdoneexpect -b ssh_test.shmv copyall.sh.bak copyall.shmv ssh_test.sh.bak ssh_test.shdone