Adding a timeout when using sftp in a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a timeout when using sftp in a script?
# 1  
Old 10-30-2018
Adding a timeout when using sftp in a script?

Hello guys.

I need some help. First of all, sorry about my english, that is not my native languaje.

I have a bash script in Solaris with the next lines:

Quote:
#!/bin/sh

echo "Running stfp"`date`
/export/user1/sftp.sh

echo "Adjustments" `date`
/export/user1/last_date/fixes.sh

cd /export/user1/

echo "Processing: " `date`
/export/user1/addtoBD.sh

echo "End : "`date`
And the sftp.sh has this:

Quote:
#!/bin/sh
# sample automatic ftp script to dump a file
USER="exercise"
HOST="182.21.225.36"
sftp $USER@$HOST << EOF
cd downs
get * /export/user1/inv
quit
EOF

The problem is that sometimes the sftp takes a long of time. I mean, 2 or 3 hours when it should be no more than 1 minute. I think there is a problem in the network, but i do not care about that in this moment.

So, i need to "cancel" that sftp and continue to the next line. It does not matter if the stfp is not finished.

I mean, can i add some kind of timeout to the sftp? I.e. 5 minutes. If it takes more than 5 minutes, continue to the next line.

Is it possible?



Thanks in advance.
Regards!
# 2  
Old 10-30-2018
Hi,

I'm not aware of a way of timing out a single file transfer and moving to the next, however I think I would attempt to resolve the overall time.

So you could make the logging more verbose and see if there is an issue, perhaps the "cd" is failing?

Try sftp -v in the shell or even sftp -v -v -v which is the maximum level of reporting and check the output for any errors.

In the event that the network has a problem, which you do suggest - involve the network team if you have one.

Regards

Gull04
# 3  
Old 10-30-2018
Quote:
Originally Posted by gull04
Hi,

I'm not aware of a way of timing out a single file transfer and moving to the next, however I think I would attempt to resolve the overall time.

So you could make the logging more verbose and see if there is an issue, perhaps the "cd" is failing?

Try sftp -v in the shell or even sftp -v -v -v which is the maximum level of reporting and check the output for any errors.

In the event that the network has a problem, which you do suggest - involve the network team if you have one.

Regards

Gull04

Hello. That script runs every day in the morning, and the problem happens only a few times. Maybe 1 one time in the entire week.

I know we have to check where is the problem of the delay but in this moment i need to forget that and find a way to continue to the next line when the sftp is taking too long but i canīt find the solution.

Thanks anyway, you are very kind!
# 4  
Old 10-30-2018
Hi,

A little bit of an update to add to this,

You can it would seem just use one of the ssh options, the change required would probably be straight forward - as follows;

Code:
sftp $USER@$HOST -o ConnectTimeout=nn -o BatchMode=yes << EOF

The Connect Timeout value in seconds, you should be able to investigate with any error codes.

Regards

Gull04
# 5  
Old 10-30-2018
Quote:
Originally Posted by gull04
Hi,

A little bit of an update to add to this,

You can it would seem just use one of the ssh options, the change required would probably be straight forward - as follows;

Code:
sftp $USER@$HOST -o ConnectTimeout=nn -o BatchMode=yes << EOF

The Connect Timeout value in seconds, you should be able to investigate with any error codes.

Regards

Gull04
Thanks, but unfortunately it does not work.

Quote:
./test.sh
Usage: sftp [-vC1] [-b batchfile] [-o option] [-s subsystem|path]
[-F config] [-P direct server path] [-S program] [-B buffer_size]
[-R num_requests] [user@]host[:file [file]]
And when i deletes the "-o ConnectTimeout=20 -o BatchMode=yes" it works. It seems that is only for the ssh command.

Thanks again.

Last edited by giolita25; 10-30-2018 at 12:12 PM..
# 6  
Old 10-30-2018
sftp is ssh. But only a few implementations of ssh bother supporting that option.
# 7  
Old 10-30-2018
Hi Sorry,

You can try using;

Code:
sftp -o ConnectTimeout=10 -o BatchMode=yes -o StrictHostKeyChecking=yes $USER@$HOST << EOF

This is working on one of my Solaris installations SunOS cathsunvs04 5.10 Generic_150400-46 sun4v sparc sun4v haven't tested anywhere else.

Regards

Gull04
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command timeout from inside script.

Hi, I've written a very robust script to get an external IP address from 'behind' a router. It uses many web pages randomly choosing which one/ones to use at run time. The "fetch the web page containing the IP address" is handled by either wget or curl both of which have their 'max time for the... (6 Replies)
Discussion started by: gencon
6 Replies

2. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

3. Shell Programming and Scripting

Python script help. Kill binary on timeout

This is part of my script, as you can see when the alarm is raised it shows some text, what I need it to do is to actually kill the binary "test" (TEST_PATH) when the timeout happens. #!/usr/bin/python26 from optparse import OptionParser import nagios import re import subprocess... (0 Replies)
Discussion started by: erick_tuk
0 Replies

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

5. Solaris

SFTP is successful but still shows timeout error

Hi, I am doing sftp from remote server1 to remote server2. This is done through a script. This script was working fine. But if i am tranfer files of 120 MB only some part of the file gets transferred (around 9 MB). Incase i put the same file manually it gets uploaded successfully. Can... (1 Reply)
Discussion started by: subiksha
1 Replies

6. Shell Programming and Scripting

session timeout for shell script

I am executing test.sh script. But this script takes lot of time and in the meantime the shell timeouts without completing the script. Is there any command which will continue processing the script. Thanks (3 Replies)
Discussion started by: infyanurag
3 Replies

7. Shell Programming and Scripting

KSH script SQL timeout issue

Hi all, I have a KSH script which is kicking off an sql scripts as follows: /usr/local/installs/instantclient_10_2/sqlplus -s username/password @$sql_path/sql_query.sql > $tmp_path/sql_query_results The problem I have is that sometimes the 10g Oracle Database spits out an error saying... (4 Replies)
Discussion started by: Donkey25
4 Replies

8. Shell Programming and Scripting

read from std i/p with timeout within a script

hello every one , this is my first participation in the forum , I hope it'll be a good start within a script I would like to put some code to read i\p from standard i\p using read command if it reads Y it will terminate the script if it reads N it will continue execution , if no i\p is... (2 Replies)
Discussion started by: Blue_shadow
2 Replies

9. UNIX for Dummies Questions & Answers

wget --dns-timeout script

#!/bin/sh # 'clear' for i in $(seq -w 15 37); do echo $i wget --dns-timeout=0.0001 http://napdweb${i}.eao.abn-iad.ea.com:8000/webcore/test/test.jsp -o 1 A=`cat 1` C=$(expr "$A" :... (1 Reply)
Discussion started by: veerumahanthi41
1 Replies

10. Shell Programming and Scripting

Can a timeout be specified in a shell script?

Hi, I have to develop a shell script which exits after <n> minutes. For e.g. The user is taking too much time to enter an input, and the script will terminate after 10 minutes pass by. Is this possible ? Thanks, Puneet (3 Replies)
Discussion started by: puneetarora_12
3 Replies
Login or Register to Ask a Question