linux批量同步更新服务器时间(ntp)

上一篇 / 下一篇  2019-12-03 14:37:59 / 个人分类:Linux

一、同步脚本

#!/bin/bash
#需要修改时间的ip地址
iplist=(
10.204.125.219
10.204.125.220
)
user=root
#需要修改时间机器密码
password=(
rtck219
rtck220
)
#echo ${iplist[*]}
#echo ${password[*]}
for ((i=0;i<${#iplist[*]};i++))
do
#echo ${iplist[i]}
#echo ${password[i]}
/usr/bin/expect << EOF
spawn ssh root@${iplist[i]}
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "${password[i]}\r" }
}
#第二次执行,下面两行安装ntp可不要
expect "]#"
send "yum -y install ntp\r"
expect "]#"
send "service ntpd  stop\r"
expect "]#"
send "ntpdate ntp1.aliyun.com\r"
expect "]#"
send "chkconfig ntpd on\r"
send "exit\r"
expect eof
EOF
done

二、启动linux定时任务
[root@iskytest01 uptime]# crontab -e
配置每3分钟执行一次
*/3 * * * *  sh /opt/uptime/test2.sh  >/opt/uptime/testlog.txt

查看已生效任务
[root@iskytest01 uptime]# crontab -l
*/1 * * * *  sh /opt/uptime/test2.sh  >/opt/uptime/testlog.txt

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar