通过SSH同步两台服务器时间

  • Post author:
  • Post category:其他


expect脚本获取远程服务器时间 time.sh

#!/usr/bin/expect

set timeout 15
set cmd_prompt "]#|~]?"
spawn ssh root@192.168.2.1
expect "password*"
send "密码\r"

set output [open datetime.now "w"]
expect -re $cmd_prompt {
 send "date '+%Y%m%d %T'"
 send "\r"
 expect -re "\r\n(.*)\r\n(.*)"
 set outcome $expect_out(1,string)
 puts $output $outcome
}

send "exit"
send "\r"
expect EOF

执行脚本 run.sh

#!/bin/bash

./time.sh

linuxTime=`cat datetime.now`
echo "获取到时间: ${linuxTime}"
date -s "${linuxTime}"

两个脚本放到同一个目录下,执行run.sh



版权声明:本文为ZuShang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。