Jerald Valmoria Jerald Valmoria - 3 months ago 21
Linux Question

Run bash script inside the expect script

Im trying to run my bash script inside an expect script but getting errors.

/usr/bin/expect <<EOD
spawn ssh nginubud@10.123.25.83 $(< try1.sh)
expect "assword:"
send "$reg\r"
expect eof
EOD


im trying to do this in expect
ssh nginubud@10.123.25.83 "$(< try1.sh)"
, this one is working but i need to find a way to run it in automated way. I dont want to use RSA keys.

error that in encountered:

spawn ssh nginubud@10.123.25.83 #tats script
invalid command name "echo"
while executing
"echo "Enter Year:""


Also i can run my expect ssh script but when i include and try to run my
$(< try1.sh)
im getting "no variable errors"

Answer

You can use ssh user@host bash -c .... For example:

[bash] % cat foo.sh
export CMD=$( printf '%q' "$(< try.sh)" )
expect << EOF
spawn ssh foo@localhost bash -c \$::env(CMD)
expect -nocase password:
send bar\r
expect eof
EOF
[bash] % cat try.sh
echo hello world | tr a-z A-Z
[bash] % bash foo.sh
spawn ssh foo@localhost bash -c echo\ hello\ world\ \|\ tr\ a-z\ A-Z
foo@localhost's password:
HELLO WORLD
[bash] %