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