Skip to content

Commit

Permalink
fping: add option to exit immeditely once N hosts have been found
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnw-sebast authored and schweikert committed Aug 27, 2023
1 parent e597c48 commit d45438c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ Print B<fping> version information.
Given a list of hosts, this mode checks if number of reachable hosts is >= N
and exits true in that case.

=item B<-X>, B<--fast-reachable>=I<N>

Given a list of hosts, this mode immediately exits true once N alive hosts
have been found.

=back

=head1 EXAMPLES
Expand Down
13 changes: 12 additions & 1 deletion src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ int generate_flag = 0; /* flag for IP list generation */
int verbose_flag, quiet_flag, stats_flag, unreachable_flag, alive_flag;
int elapsed_flag, version_flag, count_flag, loop_flag, netdata_flag;
int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag, rdns_flag;
int multif_flag, timeout_flag;
int multif_flag, timeout_flag, fast_reachable;
int outage_flag = 0;
int timestamp_flag = 0;
int random_data_flag = 0;
Expand Down Expand Up @@ -536,6 +536,7 @@ int main(int argc, char **argv)
{ "unreach", 'u', OPTPARSE_NONE },
{ "version", 'v', OPTPARSE_NONE },
{ "reachable", 'x', OPTPARSE_REQUIRED },
{ "fast-reachable", 'X', OPTPARSE_REQUIRED },
#if defined(DEBUG) || defined(_DEBUG)
{ NULL, 'z', OPTPARSE_REQUIRED },
#endif
Expand Down Expand Up @@ -750,6 +751,12 @@ int main(int argc, char **argv)
usage(1);
break;

case 'X':
if (!(min_reachable = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
fast_reachable = 1;
break;

case 'f':
filename = optparse_state.optarg;
break;
Expand Down Expand Up @@ -2409,6 +2416,9 @@ int wait_for_reply(int64_t wait_time)
/* print "is alive" */
if (h->num_recv == 1) {
num_alive++;
if (fast_reachable && num_alive >= min_reachable)
finish_requested = 1;

if (verbose_flag || alive_flag) {
printf("%s", h->host);

Expand Down Expand Up @@ -2944,5 +2954,6 @@ void usage(int is_error)
fprintf(out, " -u, --unreach show targets that are unreachable\n");
fprintf(out, " -v, --version show version\n");
fprintf(out, " -x, --reachable=N shows if >=N hosts are reachable or not\n");
fprintf(out, " -X, --fast-reachable=N exits true immediately when N hosts are found\n");
exit(is_error);
}

0 comments on commit d45438c

Please sign in to comment.