
Ich muss den MD5-Hash einer Onlinedatei abrufen und ihn dann mit einer Datei auf dem lokalen Computer vergleichen.
Wie kann ich das in Bash machen?
Antwort1
curl
Zum Abrufen der Online-Datei können Sie Folgendes verwenden :
curl -sL http://www.your.fi/le | md5sum | cut -d ' ' -f 1
Um es mit einem anderen zu vergleichen, speichern Sie es in einer Variablen und fahren Sie dann fort:
online_md5="$(curl -sL http://www.your.fi/le | md5sum | cut -d ' ' -f 1)"
local_md5="$(md5sum "$file" | cut -d ' ' -f 1)"
if [ "$online_md5" = "$local_md5" ]; then
echo "hurray, they are equal!"
fi
Antwort2
wget
kann mit in die Standardausgabe heruntergeladen werden -O-
.
wget http://example.com/some-file.html -O- \
| md5sum \
| cut -f1 -d' ' \
| diff - <(md5sum local-file.html | cut -f1 -d' ')
md5sum
hängt den Dateinamen nach dem MD5 an, Sie können es mit entfernen cut
.
Antwort3
wget -q -O- http://example.com/your_file | md5sum | sed 's:-$:local_file:' | md5sum -c
Ersetzen Sie http://example.com/your_file
durch die URL Ihrer Online-Datei und local_file
durch den Namen Ihrer lokalen Datei
Antwort4
Über wget
und md5sum
und awk
als langer Einzeiler=)
awk 'FNR == NR {a[0]=$1; next} {if (a[0]==$1) {print "match"; exit} {print "no match"}}'\
<(wget -O- -q URL | md5sum)\
<(md5sum local_file)
Beispiel
$ awk 'FNR == NR {a[0]=$1; next} {if (a[0]==$1) {print "match"; exit} {print "no match"}}' <(wget -O- -q http://security.ubuntu.com/ubuntu/pool/main/h/hunspell/libhunspell-1.2-0_1.2.8-6ubuntu1_i386.deb | md5sum) <(md5sum libhunspell-1.2-0_1.2.8-6ubuntu1_i386.deb)
match
$ awk 'FNR == NR {a[0]=$1; next} {if (a[0]==$1) {print "match"; exit} {print "no match"}}' <(wget -O- -q http://security.ubuntu.com/ubuntu/pool/main/h/hunspell/libhunspell-1.2-0_1.2.8-6ubuntu1_i386.deb | md5sum) <(md5sum foo)
no match