4

I my shell script, I am trying to remove password prompt for scp command (as given in https://stackoverflow.com/questions/459182/using-expect-to-pass-a-password-to-ssh/459225#459225) and this is what I have so far :-

#!/usr/bin/expect
spawn scp $DESTINATION_PATH/exam.tar $SSH_CREDENTIALS':/'$PROJECT_INSTALLATION_PATH
expect "password:"
send $sshPassword"\n";
interact

On running the script, I am getting errors

spawn: command not found
send: command not found
interact: command not found

I was also getting error expect: command not found also, then I realised the path to expect was not correct and expect was not installed at all. So, I did yum install expect, corrected the path and the error was gone. But not able to remove the other 3 errors still.

2 Answers 2

14

You're invoking the script badly.

If you say:

bash scriptname

then the #! line is ignored and bash takes the file as though it were bash instructions. But this is not a bash script, it's an expect script.

The #! line is only ever interpreted by the kernel, when given the script as a command to run in its own right.

Either give the script execute permission, so that you can just invoke ./scriptname, or use the expect command to start the script.

3
  • Now I get expect command not found. Dec 13, 2018 at 18:15
  • Did you specify the full path to the expect command, or use env to invoke it? #!/usr/bin/env expect should be fairly portable. #!/usr/bin/expect might be correct on some systems. And can you invoke the expect command at a shell prompt? If not, did somebody remove the package? Try reinstalling it, if so.
    – Phil P
    Dec 14, 2018 at 22:58
  • Prompt and excellent answer!
    – Salam
    Jul 8, 2020 at 13:14
9
  1. make sure you have "expect" tool before, if not, do it

    # apt-get install expect

  2. create the a script file with following content. (# vi /root/scriptfile)

    spawn scp /path_from/file_name user_name_here@to_host_name:/path_to

    expect "password:"

    send put_password_here\n;

    interact

  3. execute the script file with "expect" tool

    # expect /root/scriptfile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .