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

おすすめ記事