SSH를 통해 원격 시스템에서 파일의 md5sum을 찾는 방법은 무엇입니까?

SSH를 통해 원격 시스템에서 파일의 md5sum을 찾는 방법은 무엇입니까?

나는 machineC에서 다음 쉘 스크립트를 실행하고 있으며 machineC 자체의 PRIMARY 디렉토리에 있는 파일의 md5sum을 가져옵니다.

#!/bin/bash

export PRIMARY=/data01/primary

for entry in "$PRIMARY"/*
do
    local_md5sum=$(/usr/bin/md5sum "$entry" | awk '{print $1}')
    echo $local_md5sum
done

위의 쉘 스크립트를 실행하면 machineC에서 내 파일의 md5sum이 인쇄되고 제대로 작동합니다.

이제 md5sum을 계산하는 동일한 파일이 machineA 또는 machineB에도 있을 수 있으므로 machineA와 machineB 모두에서 ssh를 실행하고 동일한 파일에서 동일한 md5sum을 수행하여 remote_md5sum변수에 저장해야 합니다.

파일이 시스템 A에 없으면 시스템 B에 확실히 존재해야 하며 파일은 시스템 A와 시스템 B 모두의 이 디렉토리에 있습니다.

/bat/test/data/snapshot/20140918

그래서 나는 machineC에서 실행하고 machineA 또는 machineB에서 파일의 md5sum을 찾으려고 시도하는 다음 쉘 스크립트를 얻었습니다.

#!/bin/bash

# folder in machineC
export PRIMARY=/data01/primary

readonly SERVERS=(machineA machineB)
export SERVERS_1=${SERVERS[0]}
export SERVERS_2=${SERVERS[1]}

export FILES_LOCATION=/bat/test/data/snapshot/20140918  

for entry in "$PRIMARY"/*
do
    # find local md5sum on machineC
    local_md5sum=$(/usr/bin/md5sum "$entry" | awk '{print $1}')
    echo $local_md5sum

    # find remote md5sum of the file which will be on machineA or machineB
    remote_md5sum=$(ssh user@$SERVERS_1 /usr/bin/md5sum "$entry" | awk '{print $1}' || ssh bullseye@$SERVERS_2 /usr/bin/md5sum "$entry" | awk '{print $1}')
    echo "Remote Checksum: $remote_md5sum"

    # now compare local_md5sum and remote_md5sum
done

그러나 위의 쉘 스크립트를 실행할 때마다 ssh 명령이 실패하고 파일의 md5sum 값을 저장하지 않습니다 remote_md5sum. 이 구문에 문제가 있습니까?

remote_md5sum=$(ssh user@$SERVERS_1 /usr/bin/md5sum "$entry" | awk '{print $1}' || ssh user@$SERVERS_2 /usr/bin/md5sum "$entry" | awk '{print $1}')

답변1

귀하의 스크립트를 수정했으며 이제 작동합니다. 이해하기 쉽도록 스크립트에 몇 가지 설명을 추가했습니다. 도움이 더 필요하면 알려주시기 바랍니다.

#!/bin/bash

#The export path which we set here.
export PRIMARY=/home/ramesh

#The main for loop execution starts here. 
for entry in "$PRIMARY"/*
do
    #Get the base name of the file which we check in the remote servers.
    #Get just the filenames without the path.
    #I am going to use the filename in the remote server to check. 

    filename=$(basename "$entry")
    echo "File Name: $filename"
    #Calculate the MD5Sum locally.
    local_md5sum=$(md5sum "$entry")
    echo "Local MD5Sum: $local_md5sum"

    #Check if the file exists in server1. 
    #Otherwise I can check in the other server.

    if ssh ramesh@server1 stat /home/ramesh/'$filename' \> /dev/null 2\>\&1 then

        #I have the file in server1 and so I get the md5sum from server1.
        #I store the md5sum inside remote_md5sum variable.
        remote_md5sum=$(ssh ramesh@server1 "cd /home/ramesh/; find -name '$filename'  -exec md5sum {} \;")
    else
        #Now, I know the file is in server2 as it is not present in server1. 
        remote_file=$(ssh ramesh@server2 "cd /home/ramesh/; find -name '$filename'  -exec md5sum {} \;")
    fi
    echo "Remote MD5Sum: $remote_file"
done

시험

파일 이름에 공백이 포함되어 있으면 위 스크립트를 테스트하고 싶습니다. 잘 실행되고 이것이 스크립트를 실행할 때 얻는 출력입니다.

File Name: file1
Local MD5Sum:  39eb72b3e8e174ed20fe66bffdc9944e  /home/ramesh/file1
Remote MD5Sum: b5fc751f836c5430b617bf90a8c4725d  ./file1

File Name: file with spaces
Local MD5Sum:  36707e275264f4ac25254e2bbe5ef041  /home/ramesh/file with spaces
Remote MD5Sum: 36707e275264f4ac25254e2bbe5ef041  ./file with spaces

답변2

첫째, 변수를 절대 사용하지 마십시오 FILES_LOCATION. 이것은 쓸모 없게 만듭니다. 둘째, ||쉘에서처럼 사용할 수 없습니다 .

다음과 같이 시도해 보세요:

entry="$FILES_LOCATION/$(basename "$entry")"

remote_md5sum=$(ssh user@$SERVERS_1 /usr/bin/md5sum "$entry" | awk '{print $1}')
if [ -z $remote_md5sum ] ; then 
    remote_md5sum=$(ssh user@$SERVERS_2 /usr/bin/md5sum "$entry" | awk '{print $1}')
fi

답변3

빠르고 쉬운 방법은 다음과 같습니다(모든 명령은 "machine2"에서 "user1"으로 실행됨).

[user1@machine2]$ cd /home/user1/src

[user1@machine2]$ ssh user1@machine1 "cd src;find . -type f -exec md5sum {} \;" | md5sum --check | grep -v "OK"

시나리오에 맞게 디렉터리를 변경합니다.

2007년에 온라인에서 이 기사를 발견했습니다. 많은 도움이 됩니다.

관련 정보