1834855 Members
2317 Online
110070 Solutions
New Discussion

cksum script

 
SOLVED
Go to solution
Duffs
Regular Advisor

cksum script

Hi,

I am experiencing a truncation issue with some files that I ftp from one server to another. I know that I can resolve this issue by writing a cksum script which will resend the file if the cksum doesn't match.

Does anyone have a convenient script that can do this using 'cksum'?

Cheers,
D
8 REPLIES 8
Muthukumar_5
Honored Contributor
Solution

Re: cksum script

You can try like,

#!/bin/ksh
index=0

get_file()
{
ftp -i -n localhost < user
bin
get $1
bye
EOF
}

for file in `ls`
do
cksum $file >/dev/null
if [[ $? -ne 0 ]]
then
echo "$file is having problem. Have to be refetched again"
ERROR_FILE[$index]=$file;
let index=index+1
fi
done

index=0
while [[ $index -lt ${#ERROR_FILE[*]} ]]
do
get_file ${ERROR_FILE[$index]}
let index=index+1
done

Organize and change this script to your requirement.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: cksum script

A complete script that can be used for your requirement.

#!/bin/ksh

# Variable
SUCCESS=0

# USER Input: Give correct input for this
REMOTE_MACHINE="localhost"
USERNAME="root"
PASSWD="emclegato"
REMOTE_DIRECTORY=""
LOCAL_DIRECTORY=""


# Function part
get_file()
{

ftp -i -n $REMOTE_MACHINE<user $USERNAME $PASSWD
bin
cd $REMOTE_DIRECTORY
lcd $LOCAL_DIRECTORY
mget *
bye
EOF

}
# END get_file

check_status()
{

index=0
set -A ERROR_FILE ""


for file in `ls $LOCAL_DIRECTORY/*`
do
cksum $file 1>/dev/null 2>&1
if [[ $? -ne 0 ]]
then
ERROR_FILE[$index]=$file
let index=index+1
fi
done

if [[ -z ${ERROR_FILE} ]]
then
let SUCCESS=1
fi
}

# Main code #
while [[ $SUCCESS -ne 1 ]]
do
get_file
check_status
done

exit 0
# END #

hth.
Easy to suggest when don't know about the problem!
Duffs
Regular Advisor

Re: cksum script

Muthukumar,

I have noticed that this script only performs the checksum at the source.

I require the source checksum to be ftp'ed to the destination server along with the file and another checksum performed on the ftp'ed file at the destination server before comparing the two checksums and sending an err msg if the 2 cksum's are not identical.

Dermot.
vasundhara
Frequent Advisor

Re: cksum script

I can give you a algorithm. Not the exact script

1. cksum file > source.txt
result = 1
while result = 0
do
2. ftp the file
3. remsh destinationhost "cksum file" > destination.txt
4. diff source.txt destination.txt
5. result = $?
end

Muthukumar_5
Honored Contributor

Re: cksum script

Do you have remsh / rexec support?

If you have telnet support to remote machine, we can do it with that also as,

(
sleep 1;
printf "$USERNAME\n";
sleep 1;
printf "$PASSWD\n";
sleep 1;
<script can be executed to get cksum in remote machine>
sleep 1;
printf "exit;"
) | telnet

It will work. give time to make script with this.

hth.
Easy to suggest when don't know about the problem!
Duffs
Regular Advisor

Re: cksum script

Hi,

Thanks for the suggestions lads but unfortunately the only connection that is permitted between the 2 servers is ssh and ftp.

D
Muthukumar_5
Honored Contributor

Re: cksum script

You can try this script.

#!/bin/ksh

# Variable
SUCCESS=0

# USER Input: Give correct input for this
REMOTE_MACHINE="localhost"
USERNAME="root"
PASSWD="emclegato"
REMOTE_DIRECTORY=""
LOCAL_DIRECTORY=""


# Authentication
auth_server()
{
(
sleep 1;
printf "$USERNAME\n";
sleep 1;
printf "$PASSWD\n";
sleep 1;
printf "cd $REMOTE_DIRECTORY;find . -type f -exec cksum {} \; 1>cksum.log\n";
sleep 1;
printf "exit;"
) | telnet ${REMOTE_MACHINE}

# Function part
get_file()
{

ftp -i -n $REMOTE_MACHINE<user $USERNAME $PASSWD
bin
cd $REMOTE_DIRECTORY
lcd $LOCAL_DIRECTORY
mget *
bye
EOF

}
# END get_file

# Main
auth_server
get_file

# Checksum comparision
cd $LOCAL_DIRECTORY
find . ! -name cksum.log -type f -exec cksum {} \; 1>cksum.log.new
diff -q cksum.log.new cksum.log | grep -q 'differ'
if [[ $? -eq o ]]
then
echo "File transfer was successful"
else
echo "File transfer was not successful"
fi

exit 0
# END #

hth.
Easy to suggest when don't know about the problem!
Kasper Hedensted
Trusted Contributor

Re: cksum script

Hi,

You could use ssh to get the cksum from the source:
cksum=`ssh source "cksum /path/to/file"`

You'll need to setup SSH publickey-authorization in order to use it without interaction.
http://newfdawg.com/SHP-SSHpart2.htm

Cheers