#!/usr/bin/expect not working

10,416

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 [email protected]
1.1.1.1
testuser
Share:
10,416

Related videos on Youtube

Jim
Author by

Jim

Updated on September 18, 2022

Comments

  • Jim
    Jim almost 2 years

    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

    • John
      John about 9 years
      What error (if any) are you getting?
    • Jim
      Jim about 9 years
      spawn command not found
    • krisFR
      krisFR about 9 years
      Please can you post post the full error ?
    • Paul Haldane
      Paul Haldane about 9 years
      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
      Paul Haldane about 9 years
      Also (not related to your immediate issue) echo isn't an expect command. You probably want send_user.
    • John
      John about 9 years
      @Paul - actually, echo is valid in expect. It simply echoes the value passed to it to STDOUT.
    • Paul Haldane
      Paul Haldane about 9 years
      @John - that's a tcl feature rather than expect and only applies to interactive shells not within scripts (at least by default). See wiki.tcl.tk/2541