Expect Script for SFTP Upload


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect Script for SFTP Upload
# 1  
Old 11-16-2011
Java 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 is called in my bash script with:
Code:
expect -f ./sftp.exp

Here is what I have:

Code:
set timeout 30
spawn sftp user@serverip
  expect "password: "
  send "password"
  expect "sftp> "
  send "lcd /data/\n"
  expect "sftp> "
  send "put file.txt\n"
  expect "*100%*"
  expect "sftp> "
  send "quit\n"
  expect eof
timeout { exit 2 }

We have this running in production and normally it does not cause any problems. However lately it has been timing out and checking $? in the parent bash script only returns 0. We are running AIX 5.3 if that makes any difference.

Any help would be appreciated.

Thanks
# 2  
Old 11-16-2011
Do you really need SFTP? Why don't you use SCP? It is over the same protocol, SSH!

I had the same problem with FTP, the return code: $? was always 0, so I tried to map all the errors that could happen: "connection timedout", "lost connection", ... and parsed the output of the verbose execution: -vvv.

Something like:
Code:
sftpLogFile="./SFTP_Exec.log"
sftp -vvv user@serverip... >> "${sftpLogFile}"
# After, validate the ./SFTP_Exec.log

Also, why do you need a expect script to do a SFTP? Why don't you use a passwordless ssh

Anyway, I hope it helps!
# 3  
Old 11-16-2011
The requirement for SFTP comes from this transfer going to a third party. We are forced to use that.

Also since they are a third party the use of shared keys is not available.
# 4  
Old 11-16-2011
So, I think the option you have is to use the "-vvv" option and parse sftp's output, as I commented!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File upload message in SFTP

Hi, Below script is running on AIX 7.1 ( 7100-04-05-1720 version ) server. Recently OpenSSH version installed on server got updated from OpenSSH_6.0p1 to OpenSSH_7.5p1 version. After this update we do not receive any file upload message after put/mput command in SFTP. sftp -b - user@server... (1 Reply)
Discussion started by: Juggernaut
1 Replies

2. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

3. 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

4. UNIX for Dummies Questions & Answers

[Solved] ls -l in CentOS 6.4 after upload using sftp

Hi everyone, Something rather interesting just happened to me. I uploaded a file to a server through sftp. I closed the connection and then logged on to the server via ssh. So far so good. When I typed ls -l in the remote server to retrieve a directory listing, the file that was previously... (2 Replies)
Discussion started by: gacanepa
2 Replies

5. Shell Programming and Scripting

help to upload multiple files through SFTP

Hi Experts, Please help me to write the expect script for uploading multiple files in one shot . Below is my program that I have written. #!/usr/local/bin/expect -f #/home/kulbhushan/sftp_prog.sh # procedure to attempt connecting; result 0 if OK, 1 otherwise proc connect {passw} { expect... (1 Reply)
Discussion started by: kulbhushan
1 Replies

6. Shell Programming and Scripting

How to Write sftp through "expect" for file upload ?

Hi Experts , I am new to unix programming please tell me how to write expect and hoe to call it for automated file upload process. help me really ! (0 Replies)
Discussion started by: kulbhushan
0 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

sftp file upload problem

Hi All, I am trying to upload a text file from HP unix to Windows tectia server using sftp, the text file shows with new line character after upload. For EG : abc.txt file contains 123 456 aftre upload it shows as 123 456 i am using sftp version 2.0 TQ, (4 Replies)
Discussion started by: phani1312
4 Replies
Login or Register to Ask a Question