PERL   125

client

Guest on 3rd July 2022 05:19:23 PM

  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5. use Net::Domain;
  6.  
  7. if ($#ARGV != 1) {
  8.     die "Server and path not defined";
  9. }
  10.  
  11. my $SERVER=$ARGV[0];
  12. my $BASEDIR=$ARGV[1];
  13. my $UBERFTP;
  14.  
  15. my $START=63;
  16. my $END=1000 + $START;
  17. my $RETRY=3;
  18.  
  19. sub ftp {
  20.     my ($cmd) = shift @_;
  21.  
  22.     # retry in case of server failure
  23.     for my $try (1..$RETRY) {
  24.         # run command
  25.         print "+ $UBERFTP $SERVER \"$cmd\"\n";
  26.         my @output = `$UBERFTP $SERVER "$cmd"`;
  27.         return unless $? == 0;
  28.  
  29.         # extract message lines
  30.         my @msg = grep(/^\d\d\d /, @output);
  31.         if (!@msg) {
  32.             print @output;
  33.             return if $try == $RETRY;
  34.         }
  35.  
  36.         # check last message line
  37.         my $last = pop @msg;
  38.         push @msg, $last;
  39.         if ($last !~ m/^2\d\d /) {
  40.             print @output;
  41.             return if $try == $RETRY;
  42.         }
  43.  
  44.         # return output lines
  45.         return grep(!/^\d\d\d /, @output);
  46.  
  47.         # pause between retries
  48.         sleep $try;
  49.     }
  50. }
  51.  
  52. sub ls {
  53.     my ($dir) = @_;
  54.  
  55.     my @ret = ftp("ls $dir");
  56.  
  57.     # no such dir
  58.     return unless @ret;
  59.  
  60.     # extract filenames
  61.     my @files;
  62.     for my $line (@ret) {
  63.         my @part = split /\s+/, $line;
  64.         push @files, $part[8] unless $part[8] =~ /^\.\.?$/;
  65.     }
  66.  
  67.     return @files;
  68. }
  69.  
  70. sub mv {
  71.     my ($src, $dst) = @_;
  72.  
  73.     my $ret = ftp("rename $src $dst");
  74.  
  75.     return defined $ret;
  76. }
  77.  
  78. sub rm {
  79.     my ($dir, $file) = @_;
  80.  
  81.     my $ret = ftp("cd $dir; rm $file");
  82.  
  83.     return defined $ret;
  84. }
  85.  
  86. sub put {
  87.     my ($src, $dst) = @_;
  88.  
  89.     my $ret = ftp("put $src $dst");
  90.  
  91.     return defined $ret;
  92. }
  93.  
  94. sub get {
  95.     my ($src, $dst) = @_;
  96.  
  97.     my $ret = ftp("get $src $dst");
  98.  
  99.     return defined $ret;
  100. }
  101.  
  102. sub process_param {
  103.     my ($dir, $param) = @_;
  104.  
  105.     my $votes = "votes$param.txt";
  106.     my $command = "./parf_ifort_static -t data -r 1 -uu 1,$START-$END -c $param -n 10000 -tv $votes";
  107.     print "+ $command\n";
  108.     system($command);
  109.  
  110.     put($votes, "$dir/$votes");
  111.     rm($dir, "active$param");
  112.     unlink $votes;
  113. }
  114.  
  115. sub process_dir {
  116.     my ($dir) = @_;
  117.  
  118.     get("$dir/data.gz", "data.gz") or die "Could not get data file";
  119.     system("gunzip --force data.gz");
  120.  
  121.     open(FLAG, ">flag");
  122.     print FLAG Net::Domain::hostfqdn(), "\n";
  123.     close(FLAG);
  124.  
  125.     while (1) {
  126.         my @files = ls($dir);
  127.         last unless @files;
  128.  
  129.         my %done   = map { /^votes(\d+)/  ? ($1 => 1) : () } @files;
  130.         my %active = map { /^active(\d+)/ ? ($1 => 1) : () } @files;
  131.         my @free   = map { ($done{$_} or $active{$_}) ? () : $_ } ($START..$END);
  132.         # remove unused parameters
  133.         for my $id (keys %active) {
  134.             if ($id < $START || $id > $END) {
  135.                 delete $active{$id};
  136.             }
  137.         }
  138.  
  139.         if (@free) {
  140.             # select a free parameter
  141.             my $param = $free[int rand(@free)];
  142.             put("flag", "$dir/active$param")
  143.                 or die "Could not create active file";
  144.  
  145.             process_param($dir, $param);
  146.         } elsif (keys %active) {
  147.             # redo an active parameter
  148.             my @active = keys %active;
  149.             my $param = $active[int rand(@active)];
  150.  
  151.             process_param($dir, $param);
  152.         } else {
  153.             # dir is finished, move it out of the way
  154.             my @dirs = split /\//, $dir;
  155.             my $dir_name = pop @dirs;
  156.             mv($dir, "$dir/../../$dir_name");
  157.             # tar
  158.             last;
  159.         }
  160.     }
  161.  
  162.     unlink "data";
  163.     unlink "flag";
  164. }
  165.  
  166. # fix CA cert dir
  167. if (defined $ENV{'X509_CERT_DIR'}) {
  168.     $ENV{'X509_CERT_DIR'} = '/etc/grid-security/certificates'
  169.         unless -d $ENV{'X509_CERT_DIR'};
  170. }
  171.  
  172. # try to find a working uberftp and download parf
  173. my $ld_env = $ENV{'LD_LIBRARY_PATH'};
  174. $ld_env = "" unless defined $ld_env;
  175. for my $uberftp ('uberftp', glob '/home/globus/gt*/bin/uberftp') {
  176.     # setup globus lib location
  177.     my $gt_lib = $uberftp;
  178.     if ($gt_lib =~ s|bin/uberftp|lib|) {
  179.         $ENV{'LD_LIBRARY_PATH'} = "$gt_lib:$ld_env";
  180.     }
  181.     $UBERFTP = $uberftp;
  182.     get("$BASEDIR/../parf_ifort_static", "parf_ifort_static") and last;
  183. }
  184. chmod 0755, "parf_ifort_static" or die "chmod: $!";
  185.  
  186. # make sure never to enter the same dir twice
  187. my %done_dir = ();
  188. while (1) {
  189.     my @dirs = ls($BASEDIR);
  190.     @dirs = grep !/^\.\.?$/, @dirs;
  191.     @dirs = map { "$BASEDIR/$_" } @dirs;
  192.     @dirs = map { $done_dir{$_} ? () : $_ } @dirs;
  193.     last unless @dirs;
  194.  
  195.     my $dir = $dirs[0];
  196.     process_dir($dir);
  197.     $done_dir{$dir} = 1;
  198. }
  199.  
  200. # vim:et:sw=4:ts=4

Raw Paste


Login or Register to edit or fork this paste. It's free.