I have to run multiple chunks of expect script inside the shell script. In between each expect scripts, I have to run shell code. Below is conceptual non-working code where, 1st chunk would SSH into a remote node, and the subsequent chunks will run some other commands on the same remote server.
#!/bin/bash
#first chunk of expect code
/usr/bin/expect - <<EOF
global spawn_id
spawn /bin/bash
expect -re ".*\$"
spawn -- "ssh -oStrictHostKeyChecking=no -oCheckHostIP=no admin@localstack1\r"
expect -re ".*assword.*"
send 'admin\r'
expect -re ".*\$"
interact
EOF
#running shell commands
ls -lrt
#2nd chunk of expect code
/usr/bin/expect - <<EOF
expect -re ".*\$"
send -- "hostname\r"
expect -re ".*\$"
EOF
This seems to be impossible, However would like to know more from experts. unlike pexepct, here I am unable to find any way to reuse the spawned shell(passing the child object).
why?
from another program I am getting the list of commands,expected_results and timeout , so I cannot hardcode them within the expect script. Sometime there are 1 set of commands,sometimes multiple set of commands.
pexpect). you can try my sexpect which is specifically designed to be used in shell scripts.ls -lrt).lsat all; you can get and sort filenames directly in it.spawnof bash appears redundant, it's unrelated to the secondspawnwhich starts ssh.