r5088 add a record for Fedora 16
[racktables] / gateways / ssh
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
10 my @orig_params = @ARGV;
11
12 # fetch command-line parameters
13 my $op_help;
14 my $op_proto;
15 my $op_port;
16 my $op_connect_timeout = 2;
17 my $op_user;
18 my $op_identity;
19 my $op_dont_sudo;
20 my $op_as_user;
21 GetOptions (
22 'h' => \$op_help,
23 'proto:i' => \$op_proto,
24 'port|p:i' => \$op_port,
25 'connect-timeout:i' => \$op_connect_timeout,
26 'dont-sudo' => \$op_dont_sudo,
27 'sudo-user:s' => \$op_as_user,
28 'username|l:s' => \$op_user,
29 'identity|i:s' => \$op_identity,
30 );
31 if ($op_help) {
32 &display_help;
33 exit;
34 }
35 if (defined $op_proto) {
36 $op_proto == 4 or $op_proto == 6 or die "ERROR: valid protocol version values are 4 and 6";
37 }
38 my $op_host = $ARGV[0];
39 defined $op_host or die "ERROR: please specify remote host (-h for help)";
40
41 &become_user;
42
43 sub display_help {
44 print <<END;
45 ssh batch client for RackTables.
46 Takes commands list in standard input and gives the responses via standard output.
47 Usage:
48 $0 <hostname> [-p <port>] [-l <username>] [-i <identity file>] [--connect-timeout=<seconds>] [--as-user=<username>] [--proto=<4|6>]
49 -p, --port TCP port number to connect to
50 --proto exclicitly specify IP protocol version
51 -l, --username remote username for ssh
52 -i, --identity identity file to authenticate
53 --connect-timeout timeout for giving up connecting process, seconds
54 --as-user sudo self as specified username
55
56 END
57 }
58
59 sub become_user {
60 if (defined $op_as_user && (my $uid = getpwnam($op_as_user)) != $>) {
61 if ($op_dont_sudo) {
62 die "Cant become user $op_as_user";
63 }
64 else {
65 exec ('sudo', '-u', "#$uid", $0, @orig_params, '--dont-sudo') or die "cant exec: $!";
66 }
67 }
68 }
69
70 my $port = $op_port || 22;
71 my @params;
72 push @params, '-T';
73 if (defined $op_proto) {
74 push @params, "-$op_proto"
75 }
76 if (defined $op_connect_timeout) {
77 push @params, '-o', "ConnectTimeout=$op_connect_timeout";
78 }
79 if ($port) {
80 push @params, '-p', $port;
81 }
82 if (defined $op_user) {
83 push @params, '-l', $op_user;
84 }
85 if (defined $op_identity) {
86 push @params, '-i', $op_identity;
87 }
88 push @params, '-o', 'StrictHostKeyChecking=no';
89 push @params, '-o', 'BatchMode=yes';
90 push @params, '-o', 'CheckHostIP=no';
91 push @params, '-o', 'LogLevel=ERROR';
92
93 exec ('ssh', @params, $op_host) or die "cant exec ssh: $!";