How to check whether the sftp script is successful??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check whether the sftp script is successful??
# 1  
Old 09-04-2013
How to check whether the sftp script is successful??

hi,

how can i check whether the sftp connectivity is successful or not??
i am using expect script to connect to sftp..

sftp_script
Code:
spawn /usr/bin/sftp abc@ftp.xyz.com
expect "abc@ftp.xyz.com's password:"
send "password\r"
expect "sftp>"
send "mput *.txt\r"
expect "sftp>"
send "bye\r"
expect eof

i am calling this sftp script from a shell script and checking the return code for 0.

calling_sftp.sh
Code:
/home/sftp/sftp_script

RC=$?
if [ $RC -eq 0 ]; then
   echo "successful"
else
   echo "failed"
fi

---------- Post updated at 03:41 PM ---------- Previous update was at 03:11 PM ----------

the sftp script gave this error

Code:
send: spawn id exp4 not open
    while executing
"send "mput *.txt\r""
    (file "/home/sftp/sftp_script" line 5)^M

# 2  
Old 09-04-2013
You should not be injecting insecure plaintext passwords into sftp with the expect brute-forcing tool. sftp allows you to use keys, which will make your life much, much simpler, since you can script it directly, run it and wait, instead of having to micromanage every line it prints and reads. Also far more secure, since your scripts will not need to contain your password in plaintext anymore!
# 3  
Old 09-04-2013
hope it helps but put your ssh keys to server as @Corona688 mentioned.

Code:
sftp $SFTP_USER@$SERVER <<EOF | grep -q 'No such file'
cd $SFTP_RDIR
ls $SFTP_RFILE
bye 
EOF
if [ $? -eq 1 ] ; then
  echo 'no such remote file'
else
sftp $SFTP_USER@$SERVER <<EOF 
cd $SFTP_RDIR
get $SFTP_RFILE
bye 
EOF
 
fi

# 4  
Old 09-04-2013
The | grep -q 'No such file' might be useless because the error likely goes to stderr, while the pipe reads from stdout.
You need some additional file descriptor magic such as
2>&1 | grep ... (merge stderr with stdout) or
2>&1 >/dev/null | grep ...(merge stderr with stdout then suppress stdout, i.e. capture only stderr).
Further the exit status is reverse: if grep finds something (an error), the exit status is zero!
The following runs directly on the get stuff, lets grep print an error,
and adds an "okay" otherwise.
Code:
sftp $SFTP_USER@$SERVER <<EOF 2>&1 | grep 'No such file'
cd $SFTP_RDIR
get $SFTP_RFILE
bye 
EOF
if [ $? -eq 1 ]; then
  # grep did not find an error
  echo "okay"
fi


Last edited by MadeInGermany; 09-04-2013 at 06:38 PM..
# 5  
Old 09-05-2013
is there any way to use ftp also without hardcoding the password in the script? currently i am using the below script for ftp

Code:
ftp -n remote_server << FTP
  quote USER user
  quote PASS passwrd
  ls -l
  bye
FTP

# 6  
Old 09-05-2013
@Little : try looking at the man page for netrc you can use this to set your ftp credentials
# 7  
Old 09-05-2013
Quote:
Originally Posted by spredwood
@Little : try looking at the man page for netrc you can use this to set your ftp credentials
For SFTP?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check email successful sent and receive

We have a unix/linux server , that send mass email from it , the emails will pass the smtp gateway , email server and sent to the client , but sometimes the client do not receive the mail , we do not know the reason and when it will happen . We would like to have a script that check when the... (2 Replies)
Discussion started by: ust4
2 Replies

2. Shell Programming and Scripting

How to check the status of sftp connection is successful or not in Linux?

Hi All, We are working on linux with putty terminal for file transferring using SFTP server... here we want to know /We have Urgent Requirement If SFTP connection is successfull then we should get .txt log file in target locaton as "Success/Failure" Please provide batch script for above... (7 Replies)
Discussion started by: sravanreddy
7 Replies

3. UNIX for Dummies Questions & Answers

Ftp - file transfer and check successful delivery

In shell script, I want to transfer files continuously and make sure transfer is successful. Please advise... how to make sure ftp transfer is successful? Also is there any option such as sftp -b where I can pass multiple put <file name> commands to ftp Thanks! (1 Reply)
Discussion started by: vegasluxor
1 Replies

4. Shell Programming and Scripting

FTP Status check(whether it is successful or not)

Hi Friends I need to check the status of FTP connection i.e. Whether it is successful or not I have tried this by assigning the FTP connection script to a variable and after that using this variable I tried to check the status. In the below code snippet I am trying to assign the FTP... (1 Reply)
Discussion started by: Kannannair
1 Replies

5. UNIX for Dummies Questions & Answers

Script to check for file size and then sftp

noted down (44 Replies)
Discussion started by: mirwasim
44 Replies

6. Shell Programming and Scripting

How to check if the merge of two files is successful?

Hi All, I have a requirement to check if the merge of files is successful or not. The script to merge files goes like this, cat Rewards_Header Rewards_Siebel_Notif_temp Rewards_Siebel_Remind105_temp > Rewards_Siebel Sometimes, the file Rewards_Siebel misses records from file... (5 Replies)
Discussion started by: anandek
5 Replies

7. Homework & Coursework Questions

Script to check transfer was successful

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a script that performs a post processing action: The run time environment of the script has the... (2 Replies)
Discussion started by: kenrowe
2 Replies

8. Shell Programming and Scripting

For loop scp transfers check if all iterations successful

All, I am using a for loop to SCP a bunch of files in a directory. I am having it then drop a .ready file name. Is there a way to check for the success of all iterations and then email upon fail or success? Example of part of my script: for file in $ORIGLOC/* do ] &&... (2 Replies)
Discussion started by: markdjones82
2 Replies

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

10. UNIX for Advanced & Expert Users

SCP / SFTP successful but locks out target account

Hi, We have an interesting problem with F-Secure SSH (v 3.1.0) running on HP-UX. It seems that when scp or sftp commands are issued they are successful but it counts as a 'strike' against the target user locking the account out after 3 attempts. When the user is re-enabled in SAM - it reports... (4 Replies)
Discussion started by: b0bbins
4 Replies
Login or Register to Ask a Question