-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathtelnet
More file actions
executable file
·116 lines (105 loc) · 2.86 KB
/
telnet
File metadata and controls
executable file
·116 lines (105 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env perl
# This file is a part of RackTables, a datacenter and server room management
# framework. See accompanying file "COPYING" for the full copyright and
# licensing information.
use strict;
use Getopt::Long;
use Net::Telnet;
# fetch command-line parameters
my $op_help;
my $op_port;
my $op_connect_timeout = 2;
my $op_timeout = 10;
my $op_prompt;
my $op_delay = 0;
my $op_ipv4;
my $op_ipv6;
GetOptions (
'h' => \$op_help,
'port:i' => \$op_port,
'connect-timeout:i' => \$op_connect_timeout,
'timeout:i' => \$op_timeout,
'prompt-delay:f' => \$op_delay,
'prompt:s' => \$op_prompt,
'4' => \$op_ipv4,
'6' => \$op_ipv6,
);
if ($op_help) {
&display_help;
exit;
}
my $op_host = $ARGV[0];
defined $op_host or die "ERROR: please specify remote host (-h for help)";
defined $op_prompt or die "ERROR: please specify prompt regexp (-h for help)";
my $prompt_re = qr/$op_prompt/;
sub display_help {
print <<END;
telnet batch client for RackTables.
Takes commands list in standard input and gives the responses via standard output.
Login credentials are not specially handled and should be placed as first lines of input
Usage:
$0 {hostname} [--port={port}] [--connect-timeout={seconds}] --prompt={regexp} [--timeout={seconds}]
port: TCP port number to connect to
connect-timeout: timeout for giving up connecting process, seconds
prompt: command prompt regexp for interactive telnet (auth prompts too)
timeout: wait time for activity of remote telnet peer in seconds
4: force telnet to use IPv4 addresses only
6: force telnet to use IPv6 addresses only
END
}
my $port = $op_port || 23;
my $family;
if (defined $op_ipv4) {
$family = "IPv4";
}
elsif (defined $op_ipv6) {
$family = "IPv6";
} else {
$family = "any";
}
my $session = Net::Telnet->new (
Host => $op_host,
Port => $port,
Family => $family,
Timeout => $op_connect_timeout,
);
use IO::Select;
my $sel = new IO::Select($session);
$|=1;
my $buff = '';
my $nohang_read;
until ($session->eof) {
# read output from the device
eval {
my $chunk = $session->get (Timeout => $nohang_read ? 0 : $op_timeout, Errmode => $nohang_read ? 'return' : 'die');
$buff .= $chunk;
print $chunk;
};
if ($@) {
# check if there is something else in <STDIN>
if (defined <STDIN>) {
die $@;
}
else {
last; # no more input, seems like session was closed remotely by our last command
}
}
$nohang_read = 0;
$buff =~ s/(.*\n)//s;
next unless ($buff =~ $prompt_re);
# send pending commands to the device
if ($op_delay and IO::Select->select ($sel, undef, undef, $op_delay)) {
# something is received, no prompt detection at this time
# set NOHANG options for next reading, cause it can be telnet control sequence
$nohang_read = 1;
}
elsif (defined ($_ = <STDIN>)) {
# replace all CR and LF symbols with single trailing LF
s/[\015\012]//g;
$session->put($_ . "\012");
}
else {
# no more commands in input
last;
}
}