Advertisement
Shuraken007

host_sub.pl

May 18th, 2023 (edited)
1,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.84 KB | None | 0 0
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my @known_cmd = ('ssh', 'scp'),
  8. my %config = (
  9.    game => {
  10.       host => 'bandit.labs.overthewire.org',
  11.       port => '2220',
  12.       users => [
  13.          {
  14.             user => 'bandit0',
  15.             password => 'bandit0',
  16.          },
  17.          {
  18.             user => 'bandit1',
  19.             password => '~/tmp/bash_game/readme',
  20.             is_file =>1,
  21.          },
  22.          {
  23.             user => 'bandit2',
  24.             password => '~/tmp/bash_game/-',
  25.             is_file => 1,
  26.          },
  27.          {
  28.             user => 'bandit3',
  29.             password => '~/tmp/bash_game/spaces in this filename',
  30.             is_file => 1,
  31.          },
  32.          {
  33.             user => 'bandit4',
  34.             password => '~/tmp/bash_game/.hidden',
  35.             is_file => 1,
  36.          },
  37.          {
  38.             user => 'bandit5',
  39.             password => '~/tmp/bash_game/inhere/-file07',
  40.             is_file => 1,
  41.          },
  42.          {
  43.             user => 'bandit6',
  44.             password => '~/tmp/bash_game/inhere/maybehere07/.file2',
  45.             is_file => 1,
  46.          },
  47.          {
  48.             user => 'bandit7',
  49.             password => '~/tmp/bash_game/var/lib/dpkg/info/bandit7.password',
  50.             is_file => 1,
  51.          },
  52.          {
  53.             user => 'bandit8',
  54.             password => 'TESKZC0XvTetK0S9xNwm25STk5iWrBvP',
  55.          }
  56.       ]
  57.    }
  58. );
  59.  
  60. sub get_passwd {
  61.    my $user_cfg = shift @_;
  62.    my ($pass, $is_file) = @$user_cfg{'password', 'is_file'};
  63.    return () if ! $pass;
  64.  
  65.    my @cmd = ('sshpass');
  66.    if ($is_file) {
  67.       push @cmd, '-f';
  68.       $pass =~ s#(?!\\)( )#\\ #g;
  69.       push @cmd, $pass;
  70.    } else {
  71.       push @cmd, '-p', $pass;
  72.    }
  73.    return @cmd;
  74. }
  75.  
  76. sub get_host_alias_by_user {
  77.    my $user = shift @_;
  78.    my $host_alias;
  79.  
  80.    while( my($host_alias_cfg, $data_cfg) = each %config) {
  81.       for (@{$data_cfg->{'users'}}) {
  82.          if ($_->{'user'} eq $user) {
  83.             # print "find ----> $host_alias_cfg\n";
  84.             return $host_alias_cfg;
  85.          }
  86.       }
  87.    }
  88. }
  89.  
  90. sub parse_args{
  91.    my (@args) = @_;
  92.    my ($cmd, $cmd_i, $is_port, $host_alias, $host_i, $user, $host_path);
  93.  
  94.    while (my ($i, $arg) = each @args) {
  95.       OUTER:
  96.       for my $known_cmd (@known_cmd) {
  97.          if ($arg eq $known_cmd)
  98.          {
  99.             $cmd=$arg;
  100.             $cmd_i=$i;
  101.             last;
  102.          }
  103.          next OUTER;
  104.       }
  105.  
  106.       if ($arg eq '-p' || $arg eq '-P') {
  107.          $is_port = 1;
  108.          last;
  109.       }
  110.  
  111.       my ($arg_user, $arg_host, $arg_host_path)=$arg =~ m/([^\@:]+)?\@?([^:]+)?(:.+)?/;
  112.       if ($arg_user && !$arg_host) {
  113.          $arg_host = get_host_alias_by_user($arg_user);
  114.       }
  115.       if (!$arg_host) {
  116.          $arg_host = $arg;
  117.          $arg_user = undef;
  118.       }
  119.       # printf( "%s-->%s-->%s\n", $arg_user||'', $arg_host||'', $arg_host_path||'');
  120.       for my $host_alias_cfg (keys %config) {
  121.          next if $host_alias_cfg ne $arg_host;
  122.  
  123.          $host_alias = $arg_host; $host_i = $i; $host_path = $arg_host_path||'';
  124.          my $are_spaces_escaped = $host_path =~ s#(?!\\)( )#\\ #g;
  125.          if ($are_spaces_escaped && $host_path !~ /^\".+\"$/) {
  126.             $host_path =~ s/(:)(.+)/$1\"$2\"/
  127.          }
  128.          $user = $config{$host_alias}{'users'}[0] if !$arg_user;
  129.          last if $user;
  130.  
  131.          for my $user_cfg (@{$config{$host_alias}{'users'}}) {
  132.             next if $user_cfg->{'user'} ne $arg_user;
  133.             $user = $user_cfg;
  134.             last;
  135.          }
  136.          $user //= {user => $arg_user};
  137.       }
  138.    }
  139.  
  140.    my %result = (
  141.       cmd => $cmd,
  142.       cmd_i => $cmd_i,
  143.       host_alias => $host_alias,
  144.       host_i => $host_i,
  145.       user_cfg => $user,
  146.       is_port => $is_port//0,
  147.       host_path => $host_path,
  148.    );
  149.    # print Dumper(\%result);
  150.    return %result;
  151. }
  152.  
  153. sub main{
  154.    my (@args) = @_;
  155.    my %p_args = parse_args(@args);
  156.    my ($cmd, $cmd_i, $host_alias, $host_i, $user_cfg, $is_port, $host_path) =
  157.       @p_args{qw(cmd cmd_i host_alias host_i user_cfg is_port host_path)};
  158.    $cmd //= '';
  159.  
  160.    if ($host_alias) {
  161.       my $host = $config{$host_alias}{'host'};
  162.       my $user = $user_cfg->{'user'};
  163.       $args[$host_i] = "$user\@$host$host_path";
  164.    }
  165.  
  166.    my $main_cmd = $args[0];
  167.    if (!$is_port) {
  168.       my $port = $config{$host_alias}{'port'};
  169.       if ($port && $main_cmd eq 'rsync' && $cmd ne 'ssh') {
  170.          splice @args, 1, 0, '-e', "'ssh -p $port'", '--info=progress2';
  171.       } elsif ($port && $cmd eq 'ssh') {
  172.          push @args, '-p', $port;
  173.       } elsif ($port && $cmd eq 'scp') {
  174.          splice @args, $cmd_i+1, 0, '-P', $port;
  175.          $host_i+=2;
  176.       }
  177.    }
  178.  
  179.    my @password_cmd = get_passwd($user_cfg);
  180.    splice @args, 0, 0, @password_cmd;
  181.    return @args;
  182. }
  183.  
  184. @ARGV = main(@ARGV);
  185. print join(" ", @ARGV);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement