1

This is probably an easy question, however have a simple expect script that I've add the executable bit to that seems to be ignoring the #!/usr/bin/expect interpreter line. Further more, it also seems like variables are not being set since when I echo them they are blank...

#!/usr/bin/expect -f
set device "1.1.1.1"
set user   "testuser"

spawn ssh $user@$device
echo $device
echo $user

ls -lh
-rwxr-xr-x  root  root    testexpect.exp

Thanks for your help community!!

P.S. I'm running Debian Wheezy, installed expect via apt-get install expect...thanks

Jim
  • 988
  • 7
  • 20
  • 33
  • What error (if any) are you getting? – John Jun 16 '15 at 18:42
  • spawn command not found – Jim Jun 16 '15 at 20:25
  • Please can you post post the full error ? – krisFR Jun 16 '15 at 21:01
  • How are you running the script? Does it work as expected if you explicitly run it using expect (`expect -f textexpect.exp`)? Is the #! line the very first line of the file (that includes blank lines - the first two characters of file should be #!). – Paul Haldane Jun 16 '15 at 21:16
  • Also (not related to your immediate issue) `echo` isn't an `expect` command. You probably want `send_user`. – Paul Haldane Jun 16 '15 at 21:17
  • @Paul - actually, `echo` is valid in `expect`. It simply echoes the value passed to it to STDOUT. – John Jun 17 '15 at 11:13
  • @John - that's a `tcl` feature rather than `expect` and only applies to interactive shells not within scripts (at least by default). See http://wiki.tcl.tk/2541 – Paul Haldane Jun 17 '15 at 15:33

2 Answers2

1

Expect is based on Tcl language, so you shouldn't use bash 'echo' - you should use 'puts' to print something on the screen:

#!/usr/bin/expect -f
set device "1.1.1.1"
set user   "testuser"
spawn ssh $user@$device
puts $device
puts $user

And you will got result like this:

$ ./test.exp
spawn ssh testuser@1.1.1.1
1.1.1.1
testuser
dave
  • 303
  • 3
  • 16
0

The error messages suggests you are running the command with an explicit interpreter, eg:

bash ./testexpect.exp

This will ignore the "#!" line. Whatever is launching the script needs to just name the file (or explicitly say expect ./testexpect.exp). If your #! line was bad you would have got /usr/bin/expect: bad interpreter: ...

meuh
  • 1,563
  • 10
  • 11