0

I need some help on below code. I just need to achieve some simpliest task. bakcup and replace some ssl certificate files. but it doesn't seem to work. what is wrong with below code:

import_esx() {
local username=root
local backuptimestamp=$(date +"%m-%d-%y-%I:%M:%S_%p")

/usr/bin/expect << EOF
set timeout 30
spawn ssh -l $username $Ip_Address
expect {
        "(yes/no)?" { send "yes\r"; exp_continue }
        "*?assword: " { send "$CommonPassword\r"; exp_continue}
}

send_user "Backing up current certificates\r"
send "mv /etc/vmware/ssl/rui.key /etc/vmware/ssl/rui.key.$backuptimestamp\r"
send "mv /etc/vmware/ssl/rui.crt /etc/vmware/ssl/rui.crt.$backuptimestamp\r"
send "mv /etc/vmware/ssl/castore.pem /etc/vmware/ssl/castore.pem.$backuptimestamp\r"


EOF
}

thanks Jerry

Jerry Jie
  • 1
  • 2
  • `\r` - why not `\n`? And also why not `-o "StrictHostKeyChecking no"`? – KamilCuk Oct 05 '20 at 07:44
  • 1
    `\r` is idiomatic to expect: it represents "hitting Enter". – glenn jackman Oct 05 '20 at 13:15
  • Before each `send`, you should `expect` something, typically the shell prompt. Also, after the last `mv` command, you need to `send "exit\r"` and then `expect eof` – glenn jackman Oct 05 '20 at 13:16
  • thanks. maybe ask how to check each "mv" command return code. if I use Bash shell, I normally would do a "if" to check command return code, if return code is not zero, then do something. is there a similar thing in expect shell? – Jerry Jie Oct 06 '20 at 08:29
  • Expect uses the [Tcl language](http://www.tcl.tk/) which has different syntax from shell. You need to learn Tcl before you can write Expect scripts. If you are more comfortable with shell syntax you can try my [sexpect (Expect for Shells)](https://github.com/clarkwang/sexpect). – sexpect - Expect for Shells Oct 08 '20 at 13:22

1 Answers1

0
expect {
        "(yes/no)?" { send "yes\r"; exp_continue }
        "*?assword: " { send "$CommonPassword\r"; exp_continue}
}

you exp_continue on each branch: You have to expect something else so you can stop looping.

Before each send, you should expect something, typically the shell prompt. Also, after the last mv command, you need to send "exit\r" and then expect eof

Assuming your shell prompt ends with "dollar space", you can put it all together:

set prompt {[$] $}

expect {
    "(yes/no)?" { send "yes\r"; exp_continue }
    "*?assword: " { send "$CommonPassword\r"; exp_continue}
    -re $prompt
}
send_user "Backing up current certificates\r"
send "mv /etc/vmware/ssl/rui.key /etc/vmware/ssl/rui.key.$backuptimestamp\r"

expect -re $prompt
send "mv /etc/vmware/ssl/rui.crt /etc/vmware/ssl/rui.crt.$backuptimestamp\r"

expect -re $prompt
send "mv /etc/vmware/ssl/castore.pem /etc/vmware/ssl/castore.pem.$backuptimestamp\r"

expect -re $prompt
send "exit\r"
expect eof
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • thanks for your help. "before each send, I should expect something". I get it. if I write this in Bash shell, I would check return code for each "mv" for $?, but in expect shell, I find it so different to do. that is why I don't know what to expect after each "mv" command. the command normally returns nothing. – Jerry Jie Oct 06 '20 at 08:23