OpenWrt 下载脚本:使用 curl、wget 或 aria2c 下载文件并验证哈希值
#该脚本用于下载指定的文件/n#需要在命令行中指定目标目录、文件名、文件哈希值和下载链接等参数/n#也可以指定多个镜像地址,程序会按顺序尝试下载/n#如果下载成功,会将文件保存到目标目录中,并进行哈希值校验/n#如果哈希值不匹配,会删除下载的文件并报错/n#如果已经存在相同文件名的文件且哈希值匹配,程序会直接退出/n#如果哈希值匹配但文件名不同,程序会将下载的文件重命名为目标文件名/n/nuse strict;/nuse warnings;/nuse File::Basename; #用于获取文件名和目录名/nuse File::Copy; #用于复制和移动文件/nuse Text::ParseWords; #用于解析命令行参数/n/n#检查是否指定了足够的参数/n@ARGV > 2 or die 'Syntax: $0 command -v $prog;/n/t$res or return undef;/n/treturn $res;/n}/n/n#定义函数,用于获取哈希命令/nsub hash_cmd() {/n/tmy $len = length($file_hash);/n/tmy $cmd;/n/n/t$len == 64 and return '$ENV{MKHASH} sha256';/n/t$len == 32 and return '$ENV{MKHASH} md5';/n/treturn undef;/n}/n/n#定义函数,用于判断工具是否存在/nsub tool_present {/n/tmy $tool_name = shift;/n/tmy $compare_line = shift;/n/tmy $present = 0;/n/n/tif (open TOOL, '$tool_name --version 2>/dev/null |') {/n/t/tif (defined(my $line = readline TOOL)) {/n/t/t/t$present = 1 if $line =~ /^$compare_line /;/n/t/t}/n/t/tclose TOOL;/n/t}/n/n/treturn $present/n}/n/n#定义函数,用于选择下载工具/nsub select_tool {/n/t$custom_tool =~ tr//'//d;/n/tif ($custom_tool) {/n/t/treturn $custom_tool;/n/t}/n/n/t# 尝试使用 curl/n/tif (tool_present('curl', 'curl')) {/n/t/treturn 'curl';/n/t}/n/n/t# 未找到工具,使用 wget/n/treturn 'wget';/n}/n/n#定义函数,用于生成下载命令/nsub download_cmd {/n/tmy $url = shift;/n/tmy $filename = shift;/n/n/tif ($download_tool eq 'curl') {/n/t/treturn (qw(curl -f --connect-timeout 20 --retry 5 --location)),/n/t/t/t$check_certificate ? () : '--insecure',/n/t/t/tshellwords($ENV{CURL_OPTIONS} || ''),/n/t/t/t$url;/n/t} elsif ($download_tool eq 'wget') {/n/t/treturn (qw(wget --tries=5 --timeout=20 --output-document=-)),/n/t/t/t$check_certificate ? () : '--no-check-certificate',/n/t/t/tshellwords($ENV{WGET_OPTIONS} || ''),/n/t/t/t$url;/n/t} elsif ($download_tool eq 'aria2c') {/n/t/tmy $additional_mirrors = join(' ', map '$cat '$target/$filename.hash';/n/t/t$sum =~ /^(/w+)/s*/ or die 'Could not generate file hash//n';/n/t/t$sum = $1;/n/n/t/tif ($sum ne $file_hash) {/n/t/t/tprint STDERR 'Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.//n';/n/t/t/tcleanup();/n/t/t/treturn;/n/t/t}/n/t};/n/n/tunlink '$target/$filename';/n/tsystem('mv', '$target/$filename.dl', '$target/$filename');/n/tcleanup();/n}/n/n#定义函数,用于清理临时文件/nsub cleanup/n{/n/tunlink '$target/$filename.dl';/n/tunlink '$target/$filename.hash';/n}/n/n#获取本地镜像列表/n@mirrors = localmirrors();/n/n#遍历所有镜像地址/nforeach my $mirror (@ARGV) {/n/tif ($mirror =~ /^//@SF///(.+)$/) {/n/t/t# 尝试从 SourceForge 下载/n/t/tfor (1 .. 5) {/n/t/t/tpush @mirrors, 'https://downloads.sourceforge.net/$1';/n/t/t}/n/t} elsif ($mirror =~ /^//@OPENWRT$/) {/n/t/t# 使用 OpenWrt 源服务器直接下载/n/t} elsif ($mirror =~ /^//@DEBIAN///(.+)$/) {/n/t/t# 尝试从 Debian 镜像下载/n/t/tpush @mirrors, 'https://ftp.debian.org/debian/$1';/n/t/tpush @mirrors, 'https://mirror.leaseweb.com/debian/$1';/n/t/tpush @mirrors, 'https://mirror.netcologne.de/debian/$1';/n/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/debian/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/debian/$1'/n/t} elsif ($mirror =~ /^//@APACHE///(.+)$/) {/n/t/t# 尝试从 Apache 镜像下载/n/t/tpush @mirrors, 'https://dlcdn.apache.org/$1';/n/t/tpush @mirrors, 'https://mirror.netcologne.de/apache.org/$1';/n/t/tpush @mirrors, 'https://mirror.aarnet.edu.au/pub/apache/$1';/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/apache/$1';/n/t/tpush @mirrors, 'https://archive.apache.org/dist/$1';/n/t/tpush @mirrors, 'http://mirror.cogentco.com/pub/apache/$1';/n/t/tpush @mirrors, 'http://mirror.navercorp.com/apache/$1';/n/t/tpush @mirrors, 'http://ftp.jaist.ac.jp/pub/apache/$1';/n/t/tpush @mirrors, 'ftp://apache.cs.utah.edu/apache.org/$1';/n/t/tpush @mirrors, 'ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1';/n/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/apache/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/apache/$1';/n/t} elsif ($mirror =~ /^//@GITHUB///(.+)$/) {/n/t/t# 尝试从 GitHub 下载/n/t/tfor (1 .. 5) {/n/t/t/tpush @mirrors, 'https://raw.githubusercontent.com/$1';/n/t/t}/n/t} elsif ($mirror =~ /^//@GNU///(.+)$/) {/n/t/t# 尝试从 GNU 镜像下载/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/gnu/$1';/n/t/tpush @mirrors, 'https://mirror.netcologne.de/gnu/$1';/n/t/tpush @mirrors, 'http://ftp.kddilabs.jp/GNU/gnu/$1';/n/t/tpush @mirrors, 'http://www.nic.funet.fi/pub/gnu/gnu/$1';/n/t/tpush @mirrors, 'http://mirror.internode.on.net/pub/gnu/$1';/n/t/tpush @mirrors, 'http://mirror.navercorp.com/gnu/$1';/n/t/tpush @mirrors, 'ftp://mirrors.rit.edu/gnu/$1';/n/t/tpush @mirrors, 'ftp://download.xs4all.nl/pub/gnu/$1';/n/t/tpush @mirrors, 'https://ftp.gnu.org/gnu/$1';/n/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/gnu/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/gnu/$1';/n/t} elsif ($mirror =~ /^//@SAVANNAH///(.+)$/) {/n/t/t# 尝试从 Savannah 镜像下载/n/t/tpush @mirrors, 'https://mirror.netcologne.de/savannah/$1';/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/nongnu/$1';/n/t/tpush @mirrors, 'http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1';/n/t/tpush @mirrors, 'http://nongnu.uib.no/$1';/n/t/tpush @mirrors, 'http://ftp.igh.cnrs.fr/pub/nongnu/$1';/n/t/tpush @mirrors, 'ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1';/n/t/tpush @mirrors, 'ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1';/n/t} elsif ($mirror =~ /^//@KERNEL///(.+)$/) {/n/t/tmy @extra = ( $1 );/n/t/tif ($filename =~ /linux-//d+//.//d+(?://.//d+)?-rc/) {/n/t/t/tpush @extra, '$extra[0]/testing';/n/t/t} elsif ($filename =~ /linux-(//d+//.//d+(?://.//d+)?)/) {/n/t/t/tpush @extra, '$extra[0]/longterm/v$1';/n/t/t}/n/t/tforeach my $dir (@extra) {/n/t/t/tpush @mirrors, 'https://cdn.kernel.org/pub/$dir';/n/t/t/tpush @mirrors, 'https://download.xs4all.nl/ftp.kernel.org/pub/$dir';/n/t/t/tpush @mirrors, 'https://mirrors.mit.edu/kernel/$dir';/n/t/t/tpush @mirrors, 'http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir';/n/t/t/tpush @mirrors, 'http://www.ring.gr.jp/archives/linux/kernel.org/$dir';/n/t/t/tpush @mirrors, 'ftp://ftp.riken.jp/Linux/kernel.org/$dir';/n/t/t/tpush @mirrors, 'ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir';/n/t/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/kernel/$dir';/n/t/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/kernel.org/$dir';/n/t/t}/n/t} elsif ($mirror =~ /^//@GNOME///(.+)$/) {/n/t/t# 尝试从 GNOME 镜像下载/n/t/tpush @mirrors, 'https://download.gnome.org/sources/$1';/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/gnome/sources/$1';/n/t/tpush @mirrors, 'http://ftp.acc.umu.se/pub/GNOME/sources/$1';/n/t/tpush @mirrors, 'http://ftp.kaist.ac.kr/gnome/sources/$1';/n/t/tpush @mirrors, 'http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1';/n/t/tpush @mirrors, 'http://mirror.internode.on.net/pub/gnome/sources/$1';/n/t/tpush @mirrors, 'http://ftp.belnet.be/ftp.gnome.org/sources/$1';/n/t/tpush @mirrors, 'ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1';/n/t/tpush @mirrors, 'ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/gnome/sources/$1';/n/t} else {/n/t/tpush @mirrors, $mirror;/n/t}/n}/n/n# 添加 OpenWrt 源服务器地址/npush @mirrors, 'https://sources.cdn.openwrt.org';/npush @mirrors, 'https://sources.openwrt.org';/npush @mirrors, 'https://mirror2.openwrt.org/sources';/n/n# 检查目标目录中是否存在目标文件/nif (-f '$target/$filename') {/n/t$hash_cmd and do {/n/t/tif (system('cat /'$target/$filename/' | $hash_cmd > /'$target/$filename.hash/'')) {/n/t/t/tdie 'Failed to generate hash for $filename//n';/n/t/t}/n/n/t/tmy $sum = cat '$target/$filename.hash';/n/t/t$sum =~ /^(/w+)/s*/ or die 'Could not generate file hash//n';/n/t/t$sum = $1;/n/n/t/tcleanup();/n/t/texit 0 if $sum eq $file_hash;/n/n/t/tdie 'Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.//n';/n/t/tunlink '$target/$filename';/n/t};/n}/n/n# 选择下载工具/n$download_tool = select_tool();/n/n# 循环尝试从镜像下载文件,直到下载成功/nwhile (!-f '$target/$filename') {/n/tmy $mirror = shift @mirrors;/n/t$mirror or die 'No more mirrors to try - giving up.//n';/n/n/tdownload($mirror, $url_filename, @mirrors);/n/tif (!-f '$target/$filename' && $url_filename ne $filename) {/n/t/tdownload($mirror, $filename, @mirrors);/n/t}/n}/n/n# 设置 Ctrl+C 信号处理函数/n$SIG{INT} = //&cleanup;/n
AI 优化版
原文地址:https://www.cveoy.top/t/topic/nvvV 著作权归作者所有。请勿转载和采集!