Checksum calculation via SFTP

Solved
drumjoel Posted messages 56 Status Member -  
drumjoel Posted messages 56 Status Member -
Hello,
I created a script that downloads a file list.txt which contains names of files to be downloaded via SFTP.
It checks if the file listed in list.txt is local; if yes, it calculates its checksum and compares it with the one on the remote server. If they are different, it downloads the one from the server.
If the file does not exist locally, it downloads it.

My problem is that I am using SSH to calculate the checksum of the file on the remote server, whereas I would like to allow only SFTP connections.

#!/usr/bin/bash downfile=list.txt sftp user@192.168.0.40:$downfile #downloads the list.txt file ls | grep $downfile > /dev/null 2>&1 if [ $? -eq 0 ]; then #check presence of the file $downfile cat "$downfile" | while read line do ls | grep $line > /dev/null 2>&1 if [ $? -eq 0 ]; then #check presence of the file $line md5local=$(md5sum $line) #store local file checksum md5distant=$(ssh user@192.168.0.40 -n md5sum $line) #store remote file checksum (sftp server) while [ "$md5local" != "$md5distant" ] do sftp user@192.168.0.40:$line #if different download the remote file md5local=$(md5sum $line) #store local file checksum done else sftp user@192.168.0.40:$line #if the file is not present locally download it fi done else exit 1; fi exit 0; 

3 answers

zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
 
Hi,

Since SFTP cannot execute commands other than those implemented by default, I don't think it's possible.

The only feasible solution is to have an MD5 file on the site associated with each file, which you will retrieve at the same time as the source file, and from there you can do your verification without even needing the variables...

--
Zen my nuggets ;-)
Make a gesture for the environment, close your windows and adopt a penguin.
0
drumjoel Posted messages 56 Status Member 2
 
Alright, thank you for the quick response.
I will mark them as resolved.
0
zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
 
Wait for other opinions, I had clearly stated: "I don't think it's possible.", without being absolutely categorical; -\
0
drumjoel Posted messages 56 Status Member 2
 
Well, my thorough research leaned in your direction, so I concluded that you were right.
0
drumjoel Posted messages 56 Status Member 2
 
Here is the script I've created and that works
 #!/usr/bin/bash #This script downloads a file whose name is stored in $downfile #the file $downfile contains the md5 and the name of the file to download #if the file listed in $downfile doesn't exist locally or doesn't have the same checksum (md5) then we download it from the server downfile=list_onco.txt sftp usersftp@192.168.0.40:$downfile #download the file list_onco.txt ls | grep $downfile > /dev/null 2>&1 if [ $? -eq 0 ]; then #test the presence of the file $downfile cat "$downfile" | while read line do md5distant=$(echo $line | cut -b1-32) #retrieve the md5 of the remote file stored in list_onco.txt nomFich=$(echo $line | cut -b34-) #retrieve the name of the file stored in list_onco.txt ls | grep $nomFich > /dev/null 2>&1 if [ $? -eq 0 ]; then #test the presence of the file $nomFich md5local=$(md5sum $nomFich | cut -b1-32) #if the file is present store the checksum of the local file while [ "$md5local" != "$md5distant" ] #as long as the local checksum is different from the remote checksum do sftp usersftp@192.168.0.40:$nomFich #download the remote file md5local=$(md5sum $nomFich | cut -b1-32) #store the checksum of the downloaded file in the checksum of the local file done else #if the file is not stored locally sftp usersftp@192.168.0.40:$nomFich #we download it md5local=$(md5sum $nomFich | cut -b1-32) #calculate the local md5sum of the downloaded file while [ "$md5local" != "$md5distant" ] #as long as the local checksum is different from the remote checksum do sftp usersftp@192.168.0.40:$nomFich #download the remote file md5local=$(md5sum $nomFich | cut -b1-32) #store the checksum of the downloaded file in the checksum of the local file done fi done else exit; #error fi exit 0; #no error 
0