SFTP bash put/get/rm check


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SFTP bash put/get/rm check
# 1  
Old 11-03-2011
SFTP bash put/get/rm check

Hello, i'm doing something like that


Code:
#!/bin/sh
sftp 172.18.255.140 <<End-Of-Session >> /usr/batch/logsftp.txt
user batch
ascii
lcd /usr/batch
get olaola.txt
bye
End-Of-Session
 
if [ $? != 0 ]
then
echo MAL >> /usr/batch/logsftp.txt
echo $? >> /usr/batch/logsftp.txt
exit $?
else
echo BIEN >> /usr/batch/logsftp.txt
echo $? >> /usr/batch/logsftp.txt
exit $?
fi


I want to check if the file that i want to get of the remote server exist or not.
then i want to check if one file that i want to put in the remote server exist after the put command, and if i try to do a rm in the remote control i want to check if the rm command itīs OK or not.

Ultimately, i want to check after i do a put/get/rm command if the command really had do this actions or not.

how i can do this??

thank you for advance

Last edited by pludi; 11-03-2011 at 09:19 AM..
# 2  
Old 11-03-2011
I guess you'll have to have a series of sftp actions where you will have to grep the messages, as even if sftp encounters an error in any transfer action, the program sftp works fine still for itself and will give you a 0 as return code.

Maybe it will be easier, if possible in your environment, to use ssh for checking and scp for transfers.
# 3  
Old 11-03-2011
Yes, i use scp and ssh into unix enviroment, but if i have to do a put command on a unix enviroment to a windows enviroment how i can to check if the files arrive on a win server??
# 4  
Old 11-03-2011
You can do:
Code:
scp ./TestFile.txt user@host:./remotepath
if [ ${?} -ne 0 ]
then
     echo "Failed to send file to remote host!"
     exit 1
fi

remoteFile=`echo "ls -1 ./remotepath/TestFile.txt | wc -l"  | ssh -C -T -l user host`
if [ ${remoteFile} -ge 1 ]
then
     echo "File successfully sent!"
fi

Untested, but it should work!
# 5  
Old 11-03-2011
the proble is that i only have a ftp user, i can connect only to a 21 port. I can't do a scp into unix to de win server.
# 6  
Old 11-03-2011
FTP has the "dir" command, you can send the file (put) and after do a "dir <filename>", but you will need to parse the FTP output.

Or another option is to try to transfer the file back to you, to see if it was properly sent, but this is not a good option if you have a big file.

Let see if someone else has any other option for this.
# 7  
Old 11-03-2011
FTP is very simple and stupid. It's also not always the same, which makes parsing 'DIR' output annoying. It's also very insecure -- passwords transmitted as plaintext. Trying to script with it is annoying at best.

Is the server on the same network as you? Would it be possible to export the directory as a windows file share, then mount it on your UNIX system? There's various ways to do that on different systems. Linux can handle them natively with the cifs driver.

Then it'd be as simple as making sure the share is mounted, copying the file, looking for other files, removing, etc. No FTP baggage.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sftp,[put] all files that are .csv to server. Linux Ubuntu

Hello all, I am trying to sftp files to a server, I am using keys as authentication and I can connect with no problems. I can also put a sample file on the server. When I tried to automate this process with a .sh file I get the "couldn't canonicalize: Failure". This is my script: sftp... (5 Replies)
Discussion started by: KidKoder
5 Replies

2. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Linux

Put -r is not working in SFTP

Hi Folks, I am trying to copy a directory along with sub-directories to SFTP server. I have connected to SFTP and tried to run the below command: sftp> get -r abc/* It is throwing the error Invalid flag -r Can you please let me know the ways to copy a directory from Local to SFTP ... (1 Reply)
Discussion started by: kirans.229
1 Replies

4. Shell Programming and Scripting

./CARE_SM_SFTP.sh[27]: put: not found error while doing sftp

Hi, I am getting the below error while doing sftp in a script file but the same is getting executed when i run from command prompt ./CARE_SM2_SFTP.sh: put: not found Code :- sftp ${USER_ID}@${FTPHOST4}:/opt/path put <Filename with path> chmod 777 <FileName with Path>... (2 Replies)
Discussion started by: satishmallidi
2 Replies

5. Shell Programming and Scripting

In put password automatically to sftp command

Hi all, i have to sftp a file to another server I don't have "expect" or sshpass on my machine its solaris 10, i want to pass password in command line or after doing this sftp user@server how to automatically provide the password as input ( stored in some variable or so) ... (1 Reply)
Discussion started by: zozoo
1 Replies

6. Shell Programming and Scripting

Need to check the value greater than or less than and give out put to a file

HI, I have one file which is as below cat /var/tmp/test1 | awk '{ print $3}'|grep -v affected Data ---------- 200.4 . The above 200 value is changable by the database script. Now I need a script that checks the value 200.4 and the script shoud give out put if value is more than 225 (2 Replies)
Discussion started by: phani4u
2 Replies

7. Shell Programming and Scripting

SFTP PUT

Hi, ServerA (SFTP server) ServerB (SFTP client) ServerB:> sftp user@ServerA ServerB:> put sample.txt ServerB:> get sample2.txt My question is that which machine will encrypt the sample.txt and sample2.txt files. Thanks (8 Replies)
Discussion started by: Arpit Narula
8 Replies

8. UNIX for Advanced & Expert Users

SFTP - Overwrite [Put -P <file>] option

Hi All Experts, I hope you are all ok! :D My question is simple but I was not able to find an answer in the internet, and that's why I am here! My question is: when I sftp to a server and use "put -P" option to put a file, it keeps the file's full permission and access time. Check below... (5 Replies)
Discussion started by: felipe.vinturin
5 Replies

9. UNIX and Linux Applications

sftp -> put : How to uplaod only newer files

I have tried winscp, filezilla to copy data from windows xp SP2 machine to the sun sparc unix machine. However speed is around 120 to 130 kb/sec. I do have lot of files & with large size. So I switched to windows command prompt & transfered few files using sftp command. Its very fast as compared... (0 Replies)
Discussion started by: _prasad
0 Replies

10. Shell Programming and Scripting

SFTP - 'put' doing an 'mput'

Hi guys, I'm having a problem with F-Secure Unix. When I try a simple command; sftp> put local.test remote.test I expect that the local file local.test would be copied to the remote file remote.test However sftp is attempting to copy local.test & remote.test from the local machine, as if... (2 Replies)
Discussion started by: b0bbins
2 Replies
Login or Register to Ask a Question