Unable to do SFTP using expect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to do SFTP using expect
# 8  
Old 06-01-2015
Quote:
Originally Posted by siramitsharma
Hi,
I am trying to sftp using expect, but not getting through as it prompt for password is coming

Following is the code
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:

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

Sorry for overquoting, but I skip all the answers already made :-) 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:
https://github.com/openssh/openssh-p...ter/readpass.c

Code:
/*
 * 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
Code:
$ 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.
Code:
$ 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.
Code:
$ 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 ;-)
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with expect and sftp in bash

I'm having trouble with some automated sftp pulls. I'm using expect inside bash scripts and spawning SFTP. Some times the expect seems bog down. I have tried to put sleeps in my code to give everything time to work before I move on to next step but I till continue to get issues. For example when... (2 Replies)
Discussion started by: gosteen
2 Replies

2. Shell Programming and Scripting

Bash Script: Send files to SFTP using Expect

I have to send few gzipped files from local server to SFTP server. My Server Info Distributor ID: Ubuntu Description: Ubuntu 12.04.4 LTS Release: 12.04 Codename: precise Created a bash script and could able to send files to sftp, but i want to send email if transfer is successful. ... (1 Reply)
Discussion started by: krux_rap
1 Replies

3. Shell Programming and Scripting

Expect Script for SFTP Upload

I am attempting to utilize an expect script (that is called from a parent bash script) to perform a file transfer over sftp. The script works except I cannot catch timeouts. I need to be able to tell in the parent bash script when the expect script has timed out, or completed successfully. It... (3 Replies)
Discussion started by: thaller
3 Replies

4. Shell Programming and Scripting

Using expect to automate sftp

I am trying to use a for loop in my expect cmdFile that I am calling. I want to be able to call either one file name or a series of file names in the working directory (that I won't know the names before hand) and then pass the names to the sftp program. Something like for i in (ls *txt) do (0 Replies)
Discussion started by: vedder191
0 Replies

5. Shell Programming and Scripting

How to automate sftp without using expect script?

How to automate sftp with out using expect script? My batch file has the password but it is not taking. Please see below. I want to use this sftp connection in a loop for pushing new files in a directory one at a time. Hence I can not use an expect script. bash-2.05$... (5 Replies)
Discussion started by: Tuxidow
5 Replies

6. Shell Programming and Scripting

sftp expect timeout problem

hello All, I am doing SFTP using expect. We just change our server from sun solaris 8 to sun solaris 10. The script was working good on sun solaris 8. But it is giving problem on 10. from shell, SFTP is working fine.Please help me. What can be the problem. LIB_sftp_get() { ... (0 Replies)
Discussion started by: mindtee_abhi
0 Replies

7. Solaris

Issue with Expect Script for SFTP.

Hi Experts, I am learning expect and wrote the below script for automatic sftp into a server: #!/usr/local/bin/expect -f -d spawn sftp -v test@mumux503 # logs into mumux503 as test user expect "password:" sleep 20 send "test\r"; # sending the password for test... (3 Replies)
Discussion started by: Hari_Ganesh
3 Replies

8. Shell Programming and Scripting

Help with Expect SFTP script

Hi All, Here is my Expect script, I don't get any error message when I run it. But the file never goes to other system? I also paste the output screen below. When I run the script, the script runs so fast. But when I do it manually, it takes about 10 minutes for the file to transfer. ... (1 Reply)
Discussion started by: samnyc
1 Replies

9. Shell Programming and Scripting

expect + cronjob + SFTP

Hi all, i got a really strange problem i wrote a script, when i run this script manually everything works fine but when i make a cronjob for it, with the same user, the EXPECT script will not work. Only the first line will be executed this is the SHell bash script #!/bin/sh; php -q... (2 Replies)
Discussion started by: digitac
2 Replies

10. Shell Programming and Scripting

Automating SFTP with Expect

Hello all, I've written an automated SFTP script to work with the Expect command. It recently occurred to me however, that if the client side box does not have the known host entry for the server, it will not work correctly. So I have added an expect for the known host prompt, and that part... (2 Replies)
Discussion started by: sysera
2 Replies
Login or Register to Ask a Question