Home > Enterprise >  Running multiple expect/TCL code inside same shell script
Running multiple expect/TCL code inside same shell script

Time:02-01

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.

CodePudding user response:

Here's an example of writing the whole thing in Expect/Tcl as suggested in the comments:

#!/usr/bin/expect

#spawn /bin/bash
#expect -re ".*\$"
spawn -- "ssh   -oStrictHostKeyChecking=no -oCheckHostIP=no admin@localstack1\r"
expect -re  ".*assword.*"
send 'admin\r'
expect -re ".*\$"
interact

#running shell commands
puts [exec ls -lrt]

expect -re ".*\$"
send -- "hostname\r"
expect -re ".*\$"

I've commented out the spawn of bash because that appears redundant.

  • Related