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