Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'if else' statement in Expect script

I am trying to create a script which will "send" input as per the output received from executing the previous script.

#!/usr/bin/expect --
set timeout 60

spawn ssh user@server1

expect "*assword*" { send "password\r"; }

expect "*$*" { send "./jboss.sh status \r"; }
if [ expect "*running*" ];
        then { send "echo running \r"; }
else { send "./jboss.sh start \r"; }
fi

I am trying to do something like this, but I am stuck in the if else statement. How can I fix it?

like image 908
vishal gupta Avatar asked Oct 30 '25 16:10

vishal gupta


1 Answers

You can simply group them into single expect statement and whichever matched, it can be processed accordingly.

#!/usr/bin/expect
set timeout 60
spawn ssh user@server1
expect "assword" { send "password\r"; }
# We escaped the `$` symbol with backslash to match literal '$' 
# The last '$' sign is to represent end-of-line
set prompt "#|%|>|\\\$ $"
expect {
        "(yes/no)"  {send "yes\r";exp_continue}
        "password:" {send "password\r";exp_continue}
        -re $prompt 
}
send "./jboss.sh status\r"
expect {
        "running" {send "echo running\r"}
        -re $prompt {send "./jboss.sh start \r"}
}
expect -re $prompt
like image 124
Dinesh Avatar answered Nov 01 '25 07:11

Dinesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!