expect命令应用实例

前提:当前Linux服务器已安装expect命令

在当前Linux服务器上,通过执行命令 sudo -u testid expect /home/test/expect.sh 20181103 201811达到调用远程服务器(192.168.1.1)上/home/testid/remote_test.sh的目的。

#!/usr/bin/expect
set timeout -1
set USER "testid"
set PWD "testpwd"
set HOST "192.168.1.1"
set YMD [lindex $argv 0]
set YM [lindex $argv 1]

spawn ssh $USER@$HOST
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send  "$PWD\r"}
}
expect "$*" {
 send "/home/testid/remote_test.sh \r"
}

expect {
 "fail" {
  send "exit \r"
  exit 1
 }
 "success" {
  send "exit \r"
  exit 0
 }
 "$*" {
  send "exit \r"
  exit 2
 }
}

猜你喜欢

转载自blog.csdn.net/sjmz30071360/article/details/83686080