tangetools/wssh/wssh

127 lines
2.6 KiB
Perl
Executable file

#!/usr/bin/perl
=head1 NAME
wssh - wait for ssh port to open and then ssh
=head1 SYNOPSIS
B<wssh> [sshoptions] I<host>
=head1 DESCRIPTION
B<wssh> waits for port 22 to open and then B<ssh>s to the given I<host>.
=head1 EXAMPLE
B<wssh example.com>
=head1 EXIT STATUS
Returns the same as B<ssh>.
=head1 REPORTING BUGS
Contact Ole Tange <ole@tange.dk>.
=head1 AUTHOR
Copyright (C) 2014-2020 Ole Tange <ole@tange.dk>.
=head1 LICENSE
Copyright (C) 2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 DEPENDENCIES
B<wssh> uses Perl, and B<w4it-for-port-open>.
=head1 SEE ALSO
B<ssh>(1)
=cut
use Getopt::Long;
Getopt::Long::Configure("bundling","require_order");
my @argv = @ARGV;
get_options_from_array(\@ARGV,[]);
my $host = shift @ARGV;
@ARGV=("ssh",@argv);
$host =~ s/.*\@//;
print $host;
system("w4it-for-port-open $host 22");
exec @ARGV;
sub options_hash {
# Returns a hash of the GetOptions config
return
("1|2|4|6|A|a|C|f|g|K|k|M|N|n|q|s|T|t|V|v|X|x|Y|y" => \$opt::one,
"b|c|D|E|e|F|I|i|L|l|m|O|o|p|Q|R|S|W|w=s" => \$opt::arg,
);
}
sub get_options_from_array {
# Run GetOptions on @array
# Returns:
# true if parsing worked
# false if parsing failed
# @array is changed
my ($array_ref, @keep_only) = @_;
if(not @$array_ref) {
# Empty array: No need to look more at that
return 1;
}
# A bit of shuffling of @ARGV needed as GetOptionsFromArray is not
# supported everywhere
my @save_argv;
my $this_is_ARGV = (\@::ARGV == $array_ref);
if(not $this_is_ARGV) {
@save_argv = @::ARGV;
@::ARGV = @{$array_ref};
}
# If @keep_only set: Ignore all values except @keep_only
my %options = options_hash();
if(@keep_only) {
my (%keep,@dummy);
@keep{@keep_only} = @keep_only;
for my $k (grep { not $keep{$_} } keys %options) {
# Store the value of the option in @dummy
$options{$k} = \@dummy;
}
}
my $retval = GetOptions(%options);
if(not $this_is_ARGV) {
@{$array_ref} = @::ARGV;
@::ARGV = @save_argv;
}
return $retval;
}