Unable to do SFTP using expect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to do SFTP using expect
# 1  
Old 06-01-2015
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
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:

# 2  
Old 06-01-2015
Hello siramitsahrma,

Could you please try following for same.
Code:
expect "medcdr@10.130.254.50's password:"

Thanks,
R. Singh
# 3  
Old 06-01-2015
Tries the same but still getting prompted for password. Please help
Moderator's Comments:
Mod Comment Please just post replies once. Two duplicates to this post have been removed


---------- 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
Code:
         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:
Code:
 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
Code:
 #!/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
~


Last edited by Don Cragun; 06-01-2015 at 01:51 AM.. Reason: Remove duplicate posts.
# 4  
Old 06-01-2015
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)
# 5  
Old 06-01-2015
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?
# 6  
Old 06-01-2015
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.
# 7  
Old 06-01-2015
Quote:
Originally Posted by RudiC
.
.
.
Try embedding it in a here document (c.f. man bash)
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.
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