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
#!perl -w use strict; use Net::Telnet; # Hostinfos my $host = '10.0.0.1'; # Anmeldeinfos my $username = 'user'; my $password = 'password'; #my $obj = new Net::Telnet ($host); my $obj = new Net::Telnet (Host => $host, Timeout => 10, Input_log => "input.log", Dump_log => "dump.log", Timeout => "5", Prompt => '/[\$%#>] $/'); my $ok = $obj->login($username, $password); print "Login OK\n" if ($ok =~ /1/); my $string = 'show ver'; $ok = $obj->cmd(String => $string); $obj->print("\n"); print "weiter gehts\n"; while (my $line = $obj->getline()) { last unless $line; print "$line\n"; if ($line =~ /More/i) { print "More gefunden\n"; $obj->print("\n"); } print $line; last if $line =~ /#/; } $ok = $obj->close; exit;
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
#!perl -w use strict; use Net::Telnet; # Hostinfos my $host = '10.0.0.1'; # Anmeldeinfos my $username = 'user'; my $password = 'pass'; # Parameter für Verbindung my $obj = new Net::Telnet ( Host => $host, Timeout => 10, Input_log => "input2.log", Dump_log => "dump2.log" ); # öffne Verbindung $obj->open($host) or die "Kann Verbindung nicht öffnen: $obj->errmsg\n"; # log dich ein $obj->login($username, $password) or die "Kann mich nicht einloggen: $obj->errmsg\n"; # führe Kommando aus $obj->print('show ver') or die "Kann Kommando nicht ausführen: $obj->errmsg\n"; sleep(1); # gehe Output durch while (my $line = $obj->get(Timeout => '3', Errmode => 'return')) { last unless $line; if ($line =~ /More/i) { #print " ##### More gefunden #####\n"; #$obj->print("\n"); # Newline ghet nur eine Zeile weiter $obj->print(" "); # Space geht ganzen Block weiter } print $line; last if $line =~ /#/; } $obj->close; exit;
perldoc Net::Telnet...
open - connect to port on remote host
...
On success 1 is returned. On time-out or other connection failures, the error mode action is performed. See errmode().
...
1 2 3 4 5 6 7 8 9
# $telnet entspricht Deinem $obj # Daten-Logging aktivieren $telnet->dump_log("/tmp/dump.log"); ### hole Deine Daten ab # Daten-Logging deaktivieren $telnet->dump_log();