Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/perl
- use strict;
- use warnings;
- use Data::Dumper;
- my @known_cmd = ('ssh', 'scp'),
- my %config = (
- game => {
- host => 'bandit.labs.overthewire.org',
- port => '2220',
- users => [
- {
- user => 'bandit0',
- password => 'bandit0',
- },
- {
- user => 'bandit1',
- password => '~/tmp/bash_game/readme',
- is_file =>1,
- },
- {
- user => 'bandit2',
- password => '~/tmp/bash_game/-',
- is_file => 1,
- },
- {
- user => 'bandit3',
- password => '~/tmp/bash_game/spaces in this filename',
- is_file => 1,
- },
- {
- user => 'bandit4',
- password => '~/tmp/bash_game/.hidden',
- is_file => 1,
- },
- {
- user => 'bandit5',
- password => '~/tmp/bash_game/inhere/-file07',
- is_file => 1,
- },
- {
- user => 'bandit6',
- password => '~/tmp/bash_game/inhere/maybehere07/.file2',
- is_file => 1,
- },
- {
- user => 'bandit7',
- password => '~/tmp/bash_game/var/lib/dpkg/info/bandit7.password',
- is_file => 1,
- },
- {
- user => 'bandit8',
- password => 'TESKZC0XvTetK0S9xNwm25STk5iWrBvP',
- }
- ]
- }
- );
- sub get_passwd {
- my $user_cfg = shift @_;
- my ($pass, $is_file) = @$user_cfg{'password', 'is_file'};
- return () if ! $pass;
- my @cmd = ('sshpass');
- if ($is_file) {
- push @cmd, '-f';
- $pass =~ s#(?!\\)( )#\\ #g;
- push @cmd, $pass;
- } else {
- push @cmd, '-p', $pass;
- }
- return @cmd;
- }
- sub get_host_alias_by_user {
- my $user = shift @_;
- my $host_alias;
- while( my($host_alias_cfg, $data_cfg) = each %config) {
- for (@{$data_cfg->{'users'}}) {
- if ($_->{'user'} eq $user) {
- # print "find ----> $host_alias_cfg\n";
- return $host_alias_cfg;
- }
- }
- }
- }
- sub parse_args{
- my (@args) = @_;
- my ($cmd, $cmd_i, $is_port, $host_alias, $host_i, $user, $host_path);
- while (my ($i, $arg) = each @args) {
- OUTER:
- for my $known_cmd (@known_cmd) {
- if ($arg eq $known_cmd)
- {
- $cmd=$arg;
- $cmd_i=$i;
- last;
- }
- next OUTER;
- }
- if ($arg eq '-p' || $arg eq '-P') {
- $is_port = 1;
- last;
- }
- my ($arg_user, $arg_host, $arg_host_path)=$arg =~ m/([^\@:]+)?\@?([^:]+)?(:.+)?/;
- if ($arg_user && !$arg_host) {
- $arg_host = get_host_alias_by_user($arg_user);
- }
- if (!$arg_host) {
- $arg_host = $arg;
- $arg_user = undef;
- }
- # printf( "%s-->%s-->%s\n", $arg_user||'', $arg_host||'', $arg_host_path||'');
- for my $host_alias_cfg (keys %config) {
- next if $host_alias_cfg ne $arg_host;
- $host_alias = $arg_host; $host_i = $i; $host_path = $arg_host_path||'';
- my $are_spaces_escaped = $host_path =~ s#(?!\\)( )#\\ #g;
- if ($are_spaces_escaped && $host_path !~ /^\".+\"$/) {
- $host_path =~ s/(:)(.+)/$1\"$2\"/
- }
- $user = $config{$host_alias}{'users'}[0] if !$arg_user;
- last if $user;
- for my $user_cfg (@{$config{$host_alias}{'users'}}) {
- next if $user_cfg->{'user'} ne $arg_user;
- $user = $user_cfg;
- last;
- }
- $user //= {user => $arg_user};
- }
- }
- my %result = (
- cmd => $cmd,
- cmd_i => $cmd_i,
- host_alias => $host_alias,
- host_i => $host_i,
- user_cfg => $user,
- is_port => $is_port//0,
- host_path => $host_path,
- );
- # print Dumper(\%result);
- return %result;
- }
- sub main{
- my (@args) = @_;
- my %p_args = parse_args(@args);
- my ($cmd, $cmd_i, $host_alias, $host_i, $user_cfg, $is_port, $host_path) =
- @p_args{qw(cmd cmd_i host_alias host_i user_cfg is_port host_path)};
- $cmd //= '';
- if ($host_alias) {
- my $host = $config{$host_alias}{'host'};
- my $user = $user_cfg->{'user'};
- $args[$host_i] = "$user\@$host$host_path";
- }
- my $main_cmd = $args[0];
- if (!$is_port) {
- my $port = $config{$host_alias}{'port'};
- if ($port && $main_cmd eq 'rsync' && $cmd ne 'ssh') {
- splice @args, 1, 0, '-e', "'ssh -p $port'", '--info=progress2';
- } elsif ($port && $cmd eq 'ssh') {
- push @args, '-p', $port;
- } elsif ($port && $cmd eq 'scp') {
- splice @args, $cmd_i+1, 0, '-P', $port;
- $host_i+=2;
- }
- }
- my @password_cmd = get_passwd($user_cfg);
- splice @args, 0, 0, @password_cmd;
- return @args;
- }
- @ARGV = main(@ARGV);
- print join(" ", @ARGV);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement