Operating System - Linux
1753925 Members
8865 Online
108810 Solutions
New Discussion юеВ

Re: sftp script assistance

 
David Bell_1
Honored Contributor

sftp script assistance

First, I know this should be done with RSA but i need to perform some testing first. Here's what I'm hoping to accomplish:

I'm attempting to automate a process that checks for the existence of files in a top level directory with 4 subdirectories, if it exists, it launches sftp to transfer the file to some location, it validates the send, then deletes the file. The structure would be something like this:

/scproot/dir_out
/scproot/dir_out/dir1
/scproot/dir_out/dir2
/scproot/dir_out/dir3
/scproot/dir_out/dir4

Each subdirectory may contain *.xml file.

I know this is not secure but it is for testing only at this time:

the sftp portion would be something like

#!/usr/bin/bash

HOST='myhost.com'
USER='username'
PASSWD='password'

sftp -i -n $HOST <user ${USER} ${PASSWD}

binary
cd /dir1/dir2/
put some_file

Can someone help me script this appropriately?

Thanks,

Dave
5 REPLIES 5
Ivan Ferreira
Honored Contributor

Re: sftp script assistance

I think that sftp may not be needed, you can use ssh an scp to do the job. You can also configure a public/private key without password to automate the process (also insecure).

Why don't just run scp of the directories, if the file exists will be transferred, if not, won't do anything. To delete the file you can use ssh remotehost rm *.

Something like this, is just a help, this wont work:

FILES=`ssh remotehost find /scproot/ -type f | wc -l`

if [ $FILES -gt 0 ]; then

for FILES in =`ssh remotehost find /scproot -type f`; do

REMOTE_CHECKSUM=`ssh remotehost md5sum $FILE`
scp remotehost:$FILES /localdir
LOCAL_CHECKSUM`md5sum /localdir/$FILE`

if [ $REMOTE_CHECKSUM = $LOCAL_CHECKSUM ];then

ssh remotehost rm $FILE

fi
done
fi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
David Bell_1
Honored Contributor

Re: sftp script assistance

Ivan,

I like what you have there with the MD5 Checksums, however, I need to simply look for the files on the "local" server, then if they exist, sftp them to a "remote" server. I do need to use sftp in this instance (not my choice). I would still like to use the incorporation of the MD5 that you listed but I'm not sure how to do that with sftp.

Thanks,

Dave

Ivan Ferreira
Honored Contributor

Re: sftp script assistance

I understand, i though that was from the remote server to the local server.

You won't be able to do checksum comparations using sftp, but you can do an ls -l after the transfer. All sftp transaction output should be redirect to a file. Then using awk or grep, get the file size and compare it with the local file.

I still think that sshd and scp will do a better job. I know that is not about you, but you should explain that will be more reliable this way.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
David Bell_1
Honored Contributor

Re: sftp script assistance

Ivan,

Thanks for the assistance. I'll keep trying to get the information put together for the script.

Dave
David Bell_1
Honored Contributor

Re: sftp script assistance

I'm going to continue to search other locations for response. Dave