Prerequisite: The expect command has been installed on the current Linux server
On the current Linux server, execute the command sudo -u testid expect /home/test/expect.sh 20181103 201811 to call /home/testid/remote_test.sh on the remote server (192.168.1.1).
#!/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
}
}