Unable to do SFTP using expect

Hi,
I am trying to sftp using expect, but not getting through as it prompt for password is coming

Following is the code

#/usr/bin/expect > output.log
sftp medcdr@10.130.254.50
expect "password:"
send "Med@Cdr12\n"
expect "sftp>"
send "put ZTE_*201505*\r"
expect "sftp>"
send "bye\r"
EOF

 

output is as follows:

 Connecting to 10.130.254.50...
medcdr@10.130.254.50's password:

 

Hello siramitsahrma,

Could you please try following for same.

expect "medcdr@10.130.254.50's password:"

Thanks,
R. Singh

Tries the same but still getting prompted for password. Please help

---------- Post updated at 05:03 PM ---------- Previous update was at 10:18 AM ----------

Hi,
I have created a function in which I am trying to use the expect command for sftp but it is not working. While copying the same in another script(test.sh) it is working.
CAN YOU PLEASE LET ME KNOW WHY THE SAME IS NOT WORKING IN FUNCTION?

function is as follows

         function smsiuc_ftp()
        {
                        spawn sftp ftp_dl@10.132.249.50
                        expect "ftp_dl@10.132.249.50's password:"
                        send "database123#\n"
                        expect "sftp>"
                        send "put ZTE_*201505*\n"
                        expect "sftp>"
                        send "bye\n"
                        EOF
                fi
        }

 

error is as follows:

 smsiuc_ftp
-bash: spawn: command not found
couldn't read file "ftp_dl@10.132.249.50's password:": no such file or directory
-bash: send: command not found
couldn't read file "sftp>": no such file or directory
-bash: send: command not found
couldn't read file "sftp>": no such file or directory
-bash: send: command not found
-bash: EOF: command not found
cat: output.log: No such file or directory
Null message body; hope that's ok

 

test.sh script in which sftp is used which is working fine is as follows

 #!/usr/bin/expect
spawn sftp ftp_dl@10.132.249.50
expect "ftp_dl@10.132.249.50's password:"
send "database123#\n"
expect "sftp>"
send "put ZTE_*201505*\n"
expect "sftp>"
send "bye\n"
exit
~
 

That's because neither spawn nor expect , send or EOF are bash commands; they're expect commands. A "shebang" (quite close to which you get in test.sh; remove the leading space) will run the script with the command indicated.
In post#1, the password might be requested because sftp is not run from within expect but as an independent command. Try embedding it in a here document (c.f. man bash )

Thanks RudiC for the comments. But we cannot set the expect in function which is my exact requirement. Can you please throw some light on this for the way out of using it in function?

Note that sftp is specifically designed to prevent you from doing what you are trying to do because it is an extremely bad and insecure method of transferring files. That is why you are forced to use the expect brute-force password injection tool to inject passwords into it brute-force. It would be better to cooperate with the way sftp is designed to work, and use passwordless keys.

You might want to consider Corona688's comment, though. With ssh and keys installed, you could even simpler just scp files over, sort of on the fly.

Sorry for overquoting, but I skip all the answers already made :slight_smile: And moderators should also have some fun with my posts.

If you look ag ssh source code, you will notice, that ssh asks for password in a specific manner:

/*
 * Reads a passphrase from /dev/tty with echo turned off/on.  Returns the
 * passphrase (allocated with xmalloc).  Exits if EOF is encountered. If
 * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
 * tty is available
 */

ssh doesn't write to standard output, where expect waits for the string "password", that's why you can't handle sftp password prompt with your script.

I didn't want to waste too much time with your script and still don't know the correct answer, but:

  • your script works for me on my RHEL6 with expect 5.44
$ cat test.expect 
#!/usr/bin/expect -f
spawn sftp test@localhost
expect "password:"
send "database123#\n"
$ expect -v
expect version 5.44.1.15
$ ssh -V
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
$ ./test.expect 
spawn sftp test@localhost
Connecting to localhost...
test@localhost's password: 
Permission denied, please try again.

(Permission denied is because I don't have user test on the box)

  • your script doesn't work on my Macbook.
$ expect -v
expect version 5.45
$ ssh -V
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
  • your script with small corrections works on my Macbook.
$ cat test.expect 
#!/usr/bin/expect -f
spawn sftp test@localhost
expect_tty "password:"
send "database123#\r\n"
$ expect -v
expect version 5.45
$ ssh -V
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
$ ./test.expect 
spawn sftp test@localhost
Password:
Password:^C

It asks the second time for the password, because I don't have user test :wink:
I hope you noticed 2 small corrections I've made:

  • first I used expect_tty instead of expect, because OpenSSH uses /dev/tty as output device and not stdout
  • second I added \r before \n to password line, because we use /dev/tty which requires (afair) both symbols and not POSIX-standard stdout.

As some moderators write, I hope it helps ... to find a solution to your expect problem.