Skip to content

Commit

Permalink
feat: add option to wait for a specific port open
Browse files Browse the repository at this point in the history
  • Loading branch information
louisLaporte authored and Louis Laporte committed Apr 2, 2024
1 parent dad78fb commit cc41bff
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions bin/shell/osh.pl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ sub main_exit {
"interactive|i" => \my $interactive,
"netconf" => \my $netconf,
"wait" => \my $wait,
"wait-port=i" => \my $wait_port,
"ssh-as=s" => \my $sshAs,
"use-key=s" => \my $useKey,
"kbd-interactive" => \my $userKbdInteractive,
Expand Down Expand Up @@ -1504,6 +1505,25 @@ sub main_exit {
}
}

# if --wait-port is specified, we wait for the host specific port to be open before connecting
if ($wait_port) {
my $startedat = time();
osh_info "Testing $host port $wait_port, will connect as soon as it's open...";
while (1) {
my @testportcmd = qw{ nc -w 1 -z -v -n };
push @testportcmd, $host, $wait_port;

my $fnretexec = OVH::Bastion::execute(cmd => \@testportcmd, noisy_stdout => 1, noisy_stderr => 1);
$fnretexec or exit(OVH::Bastion::EXIT_EXEC_FAILED);
if ($fnretexec->value->{'sysret'} == 0) {
osh_info "Open after waiting for " . (time() - $startedat) . " seconds, connecting...";
sleep 2 if (time() > $startedat + 1); # so that ssh has the time to startup... hopefully
last;
}
sleep 1;
}
}

my $logret = OVH::Bastion::log_access_insert(
account => $self,
cmdtype => $telnet ? 'telnet' : 'ssh',
Expand Down Expand Up @@ -2001,6 +2021,7 @@ sub long_help {
--always-escape Bypass config and force the bugged behavior of old bastions for REMOTE_COMMAND escaping. Don't use.
--never-escape Bypass config and force the new behavior of new bastions for REMOTE_COMMAND escaping. Don't use.
--wait Ping the host before connecting to it (useful to ssh just after a reboot!)
--wait-port Test the host specific port to be open before connecting to it
--long-help Print this
[REMOTE_COMMAND]
Expand Down

0 comments on commit cc41bff

Please sign in to comment.