Cleanup in files that is not php
[racktables] / gateways / telnet
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;
9use Net::Telnet;
10
11# fetch command-line parameters
12my $op_help;
13my $op_port;
14my $op_connect_timeout = 2;
15my $op_timeout = 10;
16my $op_prompt;
5bf2d86b 17my $op_delay = 0;
7cb57a03
AA
18GetOptions (
19 'h' => \$op_help,
20 'port:i' => \$op_port,
21 'connect-timeout:i' => \$op_connect_timeout,
22 'timeout:i' => \$op_timeout,
5bf2d86b 23 'prompt-delay:f' => \$op_delay,
7cb57a03
AA
24 'prompt:s' => \$op_prompt,
25);
26if ($op_help) {
27 &display_help;
28 exit;
29}
30my $op_host = $ARGV[0];
31defined $op_host or die "ERROR: please specify remote host (-h for help)";
01b0c68f
AA
32defined $op_prompt or die "ERROR: please specify prompt regexp (-h for help)";
33my $prompt_re = qr/$op_prompt/;
7cb57a03
AA
34
35sub display_help {
36 print <<END;
37telnet batch client for RackTables.
38Takes commands list in standard input and gives the responses via standard output.
39Login credentials are not specially handled and should be placed as first lines of input
f1dfa768 40Usage:
01b0c68f 41$0 {hostname} [--port={port}] [--connect-timeout={seconds}] --prompt={regexp} [--timeout={seconds}]
7cb57a03
AA
42
43port: TCP port number to connect to
44connect-timeout: timeout for giving up connecting process, seconds
45prompt: command prompt regexp for interactive telnet (auth prompts too)
46timeout: wait time for activity of remote telnet peer in seconds
47
48END
49}
50
51my $port = $op_port || 23;
7cb57a03
AA
52
53my $session = Net::Telnet->new (
f1dfa768 54 Host => $op_host,
7cb57a03
AA
55 Port => $port,
56 Timeout => $op_connect_timeout,
57);
7cb57a03 58
5bf2d86b
AA
59use IO::Select;
60my $sel = new IO::Select($session);
61
7cb57a03 62my $buff = '';
458f572f 63my $nohang_read;
01b0c68f 64until ($session->eof) {
458f572f 65 # read output from the device
61f03e0f
AA
66 eval {
67 $buff .= $session->get (Timeout => $nohang_read ? 0 : $op_timeout, Errmode => $nohang_read ? 'return' : 'die');
68 };
69 if ($@) {
70 # check if there is something else in <STDIN>
71 if (defined <STDIN>) {
72 die $@;
73 }
74 else {
75 last; # no more input, seems like session was closed remotely by our last command
76 }
77 }
458f572f
AA
78 $nohang_read = 0;
79 print $1 if ($buff =~ s/(.*\n)//s);
458f572f 80
01b0c68f
AA
81 next unless ($buff =~ $prompt_re);
82 # send pending commands to the device
83 if ($op_delay and IO::Select->select ($sel, undef, undef, $op_delay)) {
84 # something is received, no prompt detection at this time
85 # set NOHANG options for next reading, cause it can be telnet control sequence
86 $nohang_read = 1;
87 }
88 elsif (defined ($_ = <STDIN>)) {
89 # replace all CR and LF symbols with single trailing LF
90 s/[\015\012]//g;
91 $session->put($_ . "\012");
92 }
93 else {
94 # no more commands in input
95 last;
96 }
7cb57a03
AA
97}
98print $buff;