"command not found" errors in expect script executed by shell script

13,719

Don't do any of this! You should use public key authentication as the comment above suggests. The way you're going leaves passwords in the clear and is fragile.

Public key authentication is way easier to setup, for example: setup instructions

Share:
13,719
Sandeepan Nath
Author by

Sandeepan Nath

Organizer of meetup group PHP Micro Meetups (Pune) About me I am a Software Developer/Designer from India. Here is my Brief Résumé on Stackoverflow Careers. Contact me at sandeepan (dot) nits (at) google's mail. Other than programming I have deep interests in Human Psychology, Entrepreneurship, Social work and Scientific Research. While younger I had deep interests in Genetics, Astronomy etc. I love computer games, especially FPS games on multi-player, cricket, music, racing and adventure. I am a 24 years old guy and as time is passing by, I am gradually coming to realize that there are so many things to see and do in this beautiful world. I am afraid that a few years down the line I will repent over so many things I wished to do but could not do. However, I have started serious planning for all this.

Updated on June 05, 2022

Comments

  • Sandeepan Nath
    Sandeepan Nath almost 2 years

    I am trying to implement "shell script calling expect script" so that it does not prompt the user for entering ssh password every time. I started with Using a variable's value as password for scp, ssh etc. instead of prompting for user input every time and understood that I should have a .sh file and a .exp file. I have expect installed in my system (running expect -v shows expect version 5.43.0).

    In my upload-to-server.sh file I have

    cd $SOURCE_PATH/shell
    ./password.exp $DESTINATION_PATH $SSH_CREDENTIALS $PROJECT_INSTALLATION_PATH $PASSWORD
    

    And in my password.exp file I have

    #!/usr/bin/expect -f
    
    set DESTINATION_PATH [lindex $argv 0];
    set SSH_CREDENTIALS [lindex $argv 1];
    set PROJECT_INSTALLATION_PATH [lindex $argv 2];
    set PASSWORD [lindex $argv 3];
    
    spawn scp $DESTINATION_PATH/exam.tar $SSH_CREDENTIALS':/'$PROJECT_INSTALLATION_PATH
    expect "password:"
    send $PASSWORD"\n";
    interact
    

    On running the upload-to-server.sh file I get the following error -

    ./password.exp: line 9: spawn: command not found
    couldn't read file "password:": no such file or directory
    ./password.exp: line 11: send: command not found
    ./password.exp: line 12: interact: command not found
    

    I arrived at the above code (in the exp file) from multiple sources (without understanding much basics). In one source the code is like this

    #!/usr/local/bin/expect
    spawn  sftp  -b cmdFile [email protected]
    expect "password:"
    send "shhh!\n";
    interact
    

    Whereas in another source like this

    #!/usr/local/bin/expect -f
    set TESTCASE_HOME [lindex $argv 0];
    set TESTCASE_LIST [lindex $argv 1];
    set PASSWORD [lindex $argv 3];
    
    set timeout 200
    spawn $TESTCASE_HOME/dobrt -p $TESTCASE_HOME/$TESTCASE_LIST
    expect "*?assword:*" {send -- "$PASSWORD\r";}
    expect eof
    

    There are some differences there -

    • there is an extra -f in the #!/usr/local/bin/expect line
    • expect "?assword:" {send -- "$PASSWORD\r";} is different from expect "password:" send "shhh!\n";

    • interact replaced with expect eof.

    This is my first expect script so don't have much idea what to code. Any pointers?

    Thanks,
    Sandeepan