mwdowns
mwdowns

Reputation: 85

Trying to use expect in Mac script

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 [email protected]
expect "[email protected]'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?

Upvotes: 2

Views: 14922

Answers (1)

larsks
larsks

Reputation: 311635

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 [email protected]
expect "[email protected]'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.

Upvotes: 7

Related Questions