Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash return control to user from expect

Tags:

linux

bash

expect

I am writing a bash script with expect in it.

#!/bin/bash
IP1="a.b.c.d"
IP2="e.f.g.h"
HOST="xyz.com"
KEY="/path/to/key/file"
PORT="sshport"
/usr/bin/expect << EOD
    spawn ssh -p $PORT -i $KEY $HOST
    expect "*#"
    send "sh somescript\r"
    expect "Prompt from script:"
    send "$IP1\r"
    expect "Second Prompt from script"
    send "$IP2\r"
    interact

EOD

I am expecting to get the control back so that user can interact with the script after sending IP2 , but the script terminates, logs out of session from remote host. Any pointers please ?

Thank you Amit

like image 653
Amit Avatar asked Dec 06 '25 09:12

Amit


1 Answers

You are not setting "expect" commands, but just sending to "expect"'s stdin.

You can use "-c" to specify expect command like below, or "-f" to specify command file.

/usr/bin/expect -c "
  spawn ssh -p $PORT -i $KEY $HOST
  expect \"*#\"
  send \"sh somescript\r\"
  expect \"Prompt from script:\"
  send \"$IP1\r\"
  expect \"Second Prompt from script\"
  send \"$IP2\r\"
  interact
"
like image 150
Tsuneo Yoshioka Avatar answered Dec 07 '25 21:12

Tsuneo Yoshioka



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!