r4598 bugfix: vrp53PickInterfaceSubcommand now understands eth-trunk members
[racktables] / gateways / telnet
CommitLineData
7cb57a03
AA
1#!/usr/bin/perl
2
3use strict;
4use Getopt::Long;
5use Net::Telnet;
6
7# fetch command-line parameters
8my $op_help;
9my $op_port;
10my $op_connect_timeout = 2;
11my $op_timeout = 10;
12my $op_prompt;
13GetOptions (
14 'h' => \$op_help,
15 'port:i' => \$op_port,
16 'connect-timeout:i' => \$op_connect_timeout,
17 'timeout:i' => \$op_timeout,
18 'prompt:s' => \$op_prompt,
19);
20if ($op_help) {
21 &display_help;
22 exit;
23}
24my $op_host = $ARGV[0];
25defined $op_host or die "ERROR: please specify remote host (-h for help)";
26
27sub display_help {
28 print <<END;
29telnet batch client for RackTables.
30Takes commands list in standard input and gives the responses via standard output.
31Login credentials are not specially handled and should be placed as first lines of input
32Usage:
33$0 {hostname} [--port=X] [--connect-timeout=X] [--prompt=X] [--timeout=X]
34
35port: TCP port number to connect to
36connect-timeout: timeout for giving up connecting process, seconds
37prompt: command prompt regexp for interactive telnet (auth prompts too)
38timeout: wait time for activity of remote telnet peer in seconds
39
40END
41}
42
43my $port = $op_port || 23;
44my $prompt_re;
45if (defined $op_prompt && $op_prompt ne '') {
46 $prompt_re = qr/$op_prompt/;
47}
48
49my $session = Net::Telnet->new (
50 Host => $op_host,
51 Port => $port,
52 Timeout => $op_connect_timeout,
53);
54$session->timeout($op_timeout);
55
56my $buff = '';
57my $cmd_finished = 0;
58while (! $session->eof) {
59 $buff .= $session->get;
60 print $1 if ($buff =~ s/(.*\n)//s);
61
62 if (! $cmd_finished && (! defined $prompt_re || $buff =~ $prompt_re)) {
63 my $cmd = <STDIN>;
64 if (defined $cmd) {
9f1da7de
AA
65 # replace all CR and LF symbols with single trailing LF
66 $cmd =~ s/[\015\012]//g;
67 $cmd .= "\012";
7cb57a03
AA
68 $session->put($cmd);
69 }
70 else {
71 $cmd_finished = 1;
72 $session->shutdown(1); # half-closing TCP connection (nothing to write)
73 }
74 }
75}
76print $buff;