Capturing error codes in SFTP commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing error codes in SFTP commands
# 1  
Old 03-11-2015
Capturing error codes in SFTP commands

Hi,
I have been using the SFTP commands in my reusable shell scripts to perform Get/Put operation. The script has a list of 6 errors which i am capturing through the log file using grep command.

But I feel there might me more errors which the script might need to capture. I tried to capture the error code in the below script but its not helping me.
I had seen many forums where it was suggested to install password authentication and run the sftp commands through batch mode to capture the error codes dynamically. As this script is to go live in a couple of days, it won't be possible for me to install the password authentication now.

Can anyone suggest how to capture the error code for the below commands?
The inputs for the sftp commands are parameterized.

HTML Code:
  /usr/local/bin/expect 2>&1 >> ${Log_File_Name} <<EOF
  set timeout 200
  spawn /usr/bin/sftp $Ftpusr@$FtpSrvrNme
  expect "assword:"
  send "$(</path/password.usr)\n"
  expect "sftp>"
  send "cd ${Dir}\r"
  expect "sftp>"
  send "pwd\r"
  expect "sftp>"
  send "${FtpAction} ${FileName}\r"
  expect "sftp>"
  send "bye\r"
EOF
# 2  
Old 03-11-2015
Try replacing
Code:
/usr/local/bin/expect 2>&1 >> ${Log_File_Name} <<EOF

with
Code:
 /usr/local/bin/expect <<EOF  >> ${Log_File_Name} 2>&1

# 3  
Old 03-11-2015
First of all, and for lots of good reasons, might I suggest generating SSH-keys and setting up password-less authentication. This way you can dispose of expect and remove the risk of this code being read and someone else finding your credentials in plain text.

Additionally, this will ease your requirement. If you put the sftp subcommands you want into a batch file, you can call it like this:-
Code:
sftp -b batch_file user@server >logfile 2>&1

Should errors occur, the sftp should exit with a non-zero return code which you can easily test for.


I hope that this helps,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capturing Oracle SQL Error Codes

My issue pertains to earlier posts like exit unix script after sqlerror in procedure - dBforums SQLPLUS Error Capturing | Unix Linux Forums | Shell Programming and Scripting We are executing PL/SQL blocks from shell scripts using SQLPLUS. The error code has to be captured into a logfile that... (3 Replies)
Discussion started by: jerome_rajan
3 Replies

2. Shell Programming and Scripting

sftp return codes

sftp -v b $putlist $SFTP_ID@TARGET_SERVER How can I get a return code if fails to put the file? sftp -v b $getlist $SFTP_ID@TARGET_SERVER How can I get a return code if fails to put the file? (1 Reply)
Discussion started by: TimHortons
1 Replies

3. UNIX for Dummies Questions & Answers

return code capturing for all commands connected by "|" ...

Hi, While I am using "|" to join multiple commands, I am not getting the return code when there is error in one of the commnads. Eg: b=`find /path/a*.out | xargs basename` if ; then echo "Error" fi if there is error while finding the file or getting the basename, the $? is... (6 Replies)
Discussion started by: new_learner
6 Replies

4. Shell Programming and Scripting

Any suggestion on Capturing SFTP error in log ???

Hello All, I have searched the thread for capturing and got some suggestion but that is not working for me. I am trying to sftp to a window server from unix server and getting file there with out any issue .But i wan't to capture the error --in case a file is not exist in remote. Example :... (1 Reply)
Discussion started by: jambesh
1 Replies

5. Shell Programming and Scripting

commands to remote sftp

Hello, Is there a way to use wc, sed or other commands against remote sftp in a regular shell script? wc -c *.* | grep total | sed s/total// ? thanks in advance (0 Replies)
Discussion started by: chm0dvii
0 Replies

6. UNIX for Advanced & Expert Users

FTP commands in SFTP

Hi, I am in the process of migrating all my FTP data flows into SFTP to make data more secure... I have used many quote site commands in our FTP sesssion. In SFTP i found that there is no option to do such commands. Does any body here know to overcome the current situation. Regards,... (2 Replies)
Discussion started by: Astra
2 Replies

7. Shell Programming and Scripting

Exit codes in SFTP

HI All, I have created a unix script which takes 2 parameters and using sftp tranfers files to remote location following is the script #!/bin/ksh # # # Parameter 1 is the complete path of the destination server # Parameter 2 is the complete path of the file which is to be FTP... (1 Reply)
Discussion started by: vikramsnest
1 Replies

8. UNIX for Advanced & Expert Users

Capturing commands executed by user

Hello Unix Champs, For keeping audit trail, I want to log the commands entered by the normal users, on their terminal into a text file. I tried putting a "script -a username.timestamp.txt" in the user profile file, but script command stops execution when user types exit or presses CTRL+D... (3 Replies)
Discussion started by: bhaven.haria
3 Replies

9. UNIX for Dummies Questions & Answers

return codes from rsh commands...

I have a Korn shell script that executes a number of commands on a remote server. Is it possible to feed in the last exit code of the rsh commands (i.e. something like $?) to a variable within the local shell script? I tried the following: returncode=$(rsh spns31 ".... (1 Reply)
Discussion started by: bbouch
1 Replies
Login or Register to Ask a Question