spell "natural" and "unsigned" in some more files
[racktables] / gateways / netcat
1 #!/usr/bin/perl
2
3 # This file is a part of RackTables, a datacenter and server room management
4 # framework. See accompanying file "COPYING" for the full copyright and
5 # licensing information.
6
7 use strict;
8 use Getopt::Long;
9 use POSIX ":sys_wait_h";
10
11 # fetch command-line parameters
12 my $op_help;
13 my $op_port = 23;
14 my $op_ncbin = 'nc';
15 my $op_timeout = 30;
16 GetOptions (
17 'h' => \$op_help,
18 'port|p:i' => \$op_port,
19 'ncbin|b:s' => \$op_ncbin,
20 'timeout|w:i' => \$op_timeout,
21 );
22 if ($op_help) {
23 &display_help;
24 exit;
25 }
26 my $op_host = $ARGV[0];
27 defined $op_host or die "ERROR: missing hostname argument (-h for help)";
28
29 sub display_help {
30 print <<END;
31 netcat wrapper script for RackTables
32 Usage:
33 $0 <hostname> [-b </path/to/netcat/binary>] [-p <port>] [--connect-timeout=<seconds>]
34 -p, --port TCP port number to connect to (defaults to 23)
35 -b, --ncbin Full path to netcat binary (defaults to $op_ncbin)
36 -w, --connect-timeout netcat timeout (defaults to $op_timeout)
37
38 END
39 }
40
41 my @params = ('-w', $op_timeout, '-v');
42
43 pipe (RD, WR);
44 my $child_pid = fork();
45 defined $child_pid or die "fork: $!";
46 if (! $child_pid) {
47 close RD;
48 open (STDERR, ">&WR") or die "open: $!";
49 exec ($op_ncbin, $op_host, $op_port, @params) or die "exec $op_ncbin: $!";
50 }
51 close WR;
52
53 # suppress error output if nc returns 0
54 my $error_buff = '';
55 while (<RD>) {
56 $error_buff .= $_;
57 }
58 waitpid $child_pid, 0;
59 if ($?) {
60 print STDERR $error_buff;
61 $? = $? >> 8;
62 exit $? ? $? : 1;
63 }