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?
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With