1
2
3
4
5
6
7
8
9
10
11
use Net::Telnet;
use strict;
my $telnet = new Net::Telnet (Timeout=>10, Errmode=>'die', Prompt=>'/\S*#$/i');
my $switch=$ARGV[0];
$telnet->open($switch);
$telnet->waitfor(/Username: $/i);
$telnet->print('user');
$telnet->waitfor(/Password: $/i);
$telnet->print('geheim');
my @output=$telnet->cmd('sh mac');
print @output;
1
2
3
4
5
6
7
8
9
Mit telnet->print das Kommando senden
solange:
Zeile mit telnet->getline einlesen
Prüfen ob '---More---' in Zeile vorkommt
Wenn ja dann mit telnet->print \n senden
Prüfen ob Prompt in Zeile vorkommt
wenn ja dann Ende:
weiter nach solange:
Ende:
https://metacpan.org/module/Net%3A%3ATelnet#What-To-Know-Before-UsingWhat To Know Before Using
All output is flushed while all input is buffered. Each object contains its own input buffer.
The output record separator for print() and cmd() is set to "\n" by default, so that you don't have to append all your commands with a newline. To avoid printing a trailing "\n" use put() or set the output_record_separator to "".
The methods login() and cmd() use the prompt setting in the object to determine when a login or remote command is complete. Those methods will fail with a time-out if you don't set the prompt correctly.
Use a combination of print() and waitfor() as an alternative to login() or cmd() when they don't do what you want.
https://metacpan.org/module/Net%3A%3ATelnet#METHODSwaitfor - wait for pattern in the input
(...)
To avoid unexpected backslash interpretation, always use single quotes instead of double quotes to construct a match operator argument for prompt() and waitfor() (e.g. '/bash\$ $/'). If you're constructing a DOS like file path, you'll need to use four backslashes to represent one (e.g. '/c:\\\\users\\\\bill>$/i').
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
use Net::Telnet;
use strict;
my $filename="dumplog.txt";
my $telnet = new Net::Telnet (Timeout=>10, Errmode=>'die',Prompt=>'/\S*#$/i');
my $switch=$ARGV[0];
my $timeout = 10;
$telnet->open($switch);
unlink('input.log');
$telnet->input_log('input.log');
$telnet->waitfor('/Username: $/i');
$telnet->print('user');
$telnet->waitfor('/Password: $/i');
$telnet->print('geheim');
unlink('script.log');
$telnet->cmd('sh mac');
while (my $line=$telnet->getline(Timeout => $timeout, Errmode => 'return')) {
last unless $line;
if ($line =~ /More/i) {
print "More gefunden\n";
$telnet->print('\n');
}
print $line;
last if $line =~ /#/;
}
$telnet->close;
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
User Access Verification
Username: user
Password:
SWITCH-4711-0#sh mac
Interface MAC Address VLAN Type
--------- ----------------- ---- -----------------
Eth 1/ 3 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/ 4 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/ 5 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/ 6 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/ 7 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/ 8 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/10 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/11 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/12 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/13 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/17 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/18 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/19 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/20 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/21 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/22 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/25 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/25 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/25 1A-2B-3C-4D-5E-6F 123 Learned
Eth 1/25 1A-2B-3C-4D-5E-6F 123 Learned
---More---
Guest KPWCode (perl): (dl )1 2 3 4 5if ($line =~ /More/i) { print "More gefunden\n"; $telnet->print('\n'); } print $line;
while (my $line=$telnet->get(Timeout => $timeout, Errmode => 'return')) {