我知道我可以从http://releases.ubuntu.com/因为它们现在在文件夹中提供,但有没有办法自动化检查过程,以便自动下载 SHA 和,检查部分也是如此?我知道你可以“半自动化”这个过程MD5 校验和但我不知道如何实现完全自动化,也不知道如何使用 SHA256 实现半自动化。
类似脚本的东西,您可以输入版本和风格(包括不受支持的版本),它会自动下载 SHA256 总和并进行检查。
答案1
这将自动获取指定风格/版本的正确“SHA256SUM”文件,并根据在指定目录中找到的图像(如果未指定目录,则在当前工作目录中)进行检查;
输入的口味必须是“统一”或列出的口味之一这里;在回答这个问题时,这些是:
edubuntu
kubuntu
lubuntu
mythubuntu
ubuntu-gnome
ubuntukylin
ubuntu-mate
ubuntustudio
xubuntu
输入版本必须是指定风格的有效版本/点版本。
它用于zenity
提供一个良好的 GUI 来获取输入;它将只打印匹配的结果(如果有)并让用户选择是否要保留下载的“SHA256SUM”文件;
#!/bin/bash
if [ ! -z "$1" ]; then
if [ -d "$1" ]; then
printf "Changing PWD to $1...\n"
cd "$1"
else
printf "$1 is not a valid directory.\n"
exit 1
fi
fi
flavour=$(zenity \
--entry \
--title "Insert the flavour to check the image against" \
--text "Insert the flavour to check the image against:" \
2>/dev/null | \
tr '[:upper:]' '[:lower:]')
printf "Flavour:\t$flavour\n"
version=$(zenity \
--entry \
--title "Insert the version to check the image against" \
--text "Insert the version to check the image against:" \
2>/dev/null | \
tr '[:upper:]' '[:lower:]')
printf "Version:\t$version\n"
[ "$flavour" = "unity" ] \
&& address="http://releases.ubuntu.com/$version/SHA256SUMS" \
|| address="http://cdimage.ubuntu.com/$flavour/releases/$version/release/SHA256SUMS"
printf "Downloading $address...\n"
wget -q -O SHA256SUMS "$address"
[ $? -ne 0 ] && printf "No SHA256SUMS file found on the server.\n" && rm SHA256SUMS && exit 1
printf "Checking SHA256SUMS...\n"
shasum -a 256 -c SHA256SUMS |& grep 'OK$'
[ $? -eq 0 ] || printf "No matching image found on the target directory.\n"
zenity --question --title "Remove SHA256SUMS?" --text "Remove SHA256SUMS?" 2>/dev/null
[ $? -eq 0 ] && rm SHA256SUMS
exit 0
Ubuntu Desktop 15.04 64 位映像上的示例:
答案2
包含 Ubuntu、Kubuntu、Edubuntu Xubuntu 和 Lubuntu 的 MD5 哈希值的官方页面是:
https://help.ubuntu.com/community/UbuntuHashes
选择相关分布并点击 MD5SUMS 文件。
现在检查您下载的 ISO:
md5sum ubuntu-*.iso
将你的机器计算出的哈希值(左侧的字母数字字符串)与Ubuntu哈希页。
半自动方法
Ubuntu 将 MD5 哈希值分发到名为 MD5SUMS 的文件中,该文件位于您发行版的下载页面底部附近http://releases.ubuntu.com。
首先将 MD5SUMS 文件下载到与 iso 相同的目录。然后在终端中运行以下命令。
cd 下载目录
md5sum -c MD5SUMS
md5sum 将生成一堆警告。别担心:OK 消息将隐藏在其中的某个地方!
在这种情况下,您想要的信息位于第七行。
ubuntu-*.iso: OK
答案3
为了完成这个特定的任务,我编写了自己的 Python 脚本。该脚本针对的是 iso 映像,可以通过以下网址下载 iso 映像:http://cdimage.ubuntu.com,但该网站的内容与您使用的完全相同http://releases.ubuntu.com/,因此它对两者都有效。两个网站都有SHA256SUMS
文件,我们将检查。此脚本的基本前提是
- 下载 iso
- 计算 iso 的 sha256sum
SHA256SUM
从同一页面下载文件- 将我们计算的结果与文件中的内容进行比较
脚本源代码
此脚本也可在我的个人 GitHub 仓库,这可能从来没有出现过。
#!/usr/bin/env python3
# Script for automatically downloading and verifying sha256 hashsum
# of iso images provided by http://cdimage.ubuntu.com
import urllib.request
import sys
import os
from hashlib import sha256
def download_file(url):
print(">>> Retrieving ",url)
save_as = url.split('/')[-1]
buffer_size=512
try:
with urllib.request.urlopen(url) as response, open(save_as,'wb') as out_file:
print(response.info())
print(">>> Writing data:")
has_data=True
retrieved = 0
while has_data:
data = response.read(buffer_size)
retrieved += len(data)
# simple progress message which overwrites itself
message = "Retrieved "+str(retrieved)+" bytes"
print("\r"+" "*len(message)+"\r",end="")
print(message,end="")
sys.stdout.flush()
if data:
out_file.write(data)
else:
has_data=False
except Exception as e:
sys.stderr.write('\n>>> Something went wrong\n')
sys.stderr.write(str(e))
else:
print('\n>>> URL retrieved successfully')
return(save_as)
def get_sha256sum(file_path):
sha256sum = sha256()
with open(file_path, 'rb') as fd:
data_chunk = fd.read(1024)
while data_chunk:
sha256sum.update(data_chunk)
data_chunk = fd.read(1024)
return str(sha256sum.hexdigest())
def compare_sha256sums(local_file,sha256sum,hashsum_file):
remote_hashsum = ""
with open(hashsum_file) as fd:
for line in fd:
words = line.strip().split()
if words[1].replace('*','') == local_file:
remote_hashsum = words[0]
if not remote_hashsum:
sys.stderr.write("\n>>> Error: local file not found in list of SHA256SUMS\n")
sys.exit(1)
if remote_hashsum == sha256sum:
print("Local file ",local_file," with sha256 hashsum ",sha256sum,"matches with sha256sum in remote. All OK.")
def main():
saved_filename = download_file(sys.argv[1])
sha256sum = get_sha256sum(saved_filename)
sha256sums_file_url = "/".join( sys.argv[1].split('/')[:-1] + ['SHA256SUMS'] )
sha256sum_file = download_file( sha256sums_file_url )
compare_sha256sums(saved_filename,sha256sum,sha256sum_file)
if __name__ == '__main__': main()
测试运行:
bash-4.3$ ./get_iso_and_verify.py http://cdimage.ubuntu.com/releases/16.04.2/release/ubuntu-16.04.2-preinstalled-server-armhf+raspi2.img.xz
>>> Retrieving http://cdimage.ubuntu.com/releases/16.04.2/release/ubuntu-16.04.2-preinstalled-server-armhf+raspi2.img.xz
Date: Fri, 07 Jul 2017 21:55:20 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Thu, 16 Feb 2017 20:16:12 GMT
ETag: "ee62708-548ab77ea3b00"
Accept-Ranges: bytes
Content-Length: 249964296
Connection: close
Content-Type: application/x-xz
>>> Writing data:
Retrieved 249964296 bytes
>>> URL retrieved successfully
>>> Retrieving http://cdimage.ubuntu.com/releases/16.04.2/release/SHA256SUMS
Date: Fri, 07 Jul 2017 22:09:47 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Fri, 17 Feb 2017 00:06:46 GMT
ETag: "205-548aeb07c5180"
Accept-Ranges: bytes
Content-Length: 517
Connection: close
>>> Writing data:
Retrieved 517 bytes
>>> URL retrieved successfully
Local file ubuntu-16.04.2-preinstalled-server-armhf+raspi2.img.xz with sha256 hashsum 60156f9238360dc84267dbde4f334516d580fe540dd523d12d4837c4647d6d8f matches with sha256sum in remote. All OK.
bash-4.3$ cat SHA256SUMS
60156f9238360dc84267dbde4f334516d580fe540dd523d12d4837c4647d6d8f *ubuntu-16.04.2-preinstalled-server-armhf+raspi2.img.xz
35c9a6b7536e41c19f18033ac5a9b095130d17848126160d6b66cbd09be48f17 *ubuntu-16.04.2-server-arm64.iso
a00d88107eebadf0dde86087ad746d372d33ebdd29ac5cd4fae42a2e031d2b8f *ubuntu-16.04.2-server-powerpc.iso
0a10bada74112c58412ac8778df05abbb69d5983b672e6bbe74fa794cf002a2a *ubuntu-16.04.2-server-ppc64el.iso
253fd0eb5e529c3434729f475c7855463ba87ed7dea4321182b54c5416523897 *ubuntu-16.04.2-server-s390x.iso