1

Trying to write a simple shell script to ssh into a server and then run a tail for error logs, but I'm getting an error saying "spawn command not found". I'm using expect rather than bash and have checked /usr/bin and it is there. Script is:

#!/usr/bin/expect -f
echo "starting tail"
echo "password for the box?"
read -s theBoxPassword
spawn ssh root@10.0.0.10
expect "root@10.0.0.10's password: "
send $theBoxPassword\r

Not exactly sure what the problem is. I've looked at a bunch of examples online and it seems like I have the shebang thing right and the syntax correct. Any ideas?

mwdowns
  • 75
  • 1
  • 2
  • 8

1 Answers1

5

You appear to be mixing shell (/bin/sh) syntax with expect syntax. Both echo and read are shell commands. Expect performs input and output using commands like expect_user and send_user, as demonstrated in this answer.

If you want to mix shell syntax and expect, you could do something like this:

#!/bin/sh
echo "starting tail"
echo "password for the box?"
read -s theBoxPassword

expect <<EOF
spawn ssh root@10.0.0.10
expect "root@10.0.0.10's password: "
send "$theBoxPassword\r"
EOF

This uses the shell to produce your prompt and read the password, and then passes a script to expect on stdin. The shell will perform variable substitution on the script before it gets passed to expect.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • 1
    Would #!/bin/bash work at the top (I have another script that I could add this to in order to keep things all in one file)? – mwdowns Jan 22 '18 at 16:03
  • Seems to work, but rather than showing the prompt for the box that I ssh into, I get kicked back out to my computer's command prompt. Did I miss something else in the script? – mwdowns Jan 22 '18 at 16:13
  • Your script is currently working as expected. Add the expect command `communicate` just before the `EOF` line to get what it sounds like you want. – larsks Jan 22 '18 at 16:29
  • Thank you for your help. Ended up getting it working by adding expect "theBox:~#" then send "tail stuff" then expect "$ ". Showed the tail and I was able to ctrl-C out cleanly. – mwdowns Jan 22 '18 at 16:37