#!/bin/perl
use Net::Telnet();
use strict;

#my $IP="10.60.1.100";
my $IP="192.168.100.2";

# read config
#my @config=read_cisco_config($IP, "batman", "LTBTTTra25", "local", "prog");
my $version=read_devicetype($IP, "awisniewski", "peter", "local", "peter");
print "$version\n";

sub read_cisco_config {
    my ($host, $user, $pass, $login, $enable) = @_;
    my $t = new Net::Telnet (Timeout => 5, Prompt => '/#$/');
    
    my $found_proper_method=0;
    $t->errmode("return");
    $t->open($host);

    if ($t->waitfor('/>/i') == 1) {
        # 1 wenn ok
        # '' wenn n ok
        $t->print("enable");
        $t->waitfor('/password/i');
        $t->print("$pass");
        $t->waitfor('/#/i');
        $found_proper_method=1;
    }
    if ($found_proper_method==0) {
        if ($t->waitfor('/#/i') == 1) {
            $found_proper_method=1;
        }
    }
    if ($found_proper_method==0) {
        if ($t->waitfor('/username/i') == 1) {
            $t->print("$user");
            $t->waitfor('/password/i');
            $t->print("$pass");
            
            if ($t->waitfor('/>/i') == 1) {
                $t->print("enable");
                $t->waitfor('/password/i');
                $t->print("$enable");
            }
            if ($t->waitfor('/#/i') == '1') {
                $found_proper_method=1;
            }            
        }
    }
    if ($found_proper_method==0) {
        if ($t->waitfor('/password/i') == 1 and $found_proper_method==0) {
            $t->print("$login");
        
            if ($t->waitfor('/>/i') == 1) {
                $t->print("enable");
                $t->waitfor('/password/i');
                $t->print("$enable");
            }
            if ($t->waitfor('/#/i') == '1') {
                $found_proper_method=1;
            }
        }
    }
    if ($found_proper_method==1) {
        #$t->put(Timeout=>20);
        $t->cmd("terminal length 0");
        $t->waitfor('/#/');
        (@config) = $t->cmd("show running");
    }
    else {
        @config=("Kein Login möglich.");
    }
    return @config;
}

sub read_devicetype {
    my ($host, $user, $pass, $login, $enable) = @_;
    my $t = new Net::Telnet (Timeout => 2, Prompt => '/#[\s]*$/');
    my @version;
    my $devicetype;
    
    my $found_proper_method=0;
    $t->errmode("return");
    $t->open($host);

    if ($t->waitfor('/>/i') == 1) {
        # 1 wenn ok
        # '' wenn n ok
        $t->print("enable");
        $t->waitfor('/password/i');
        $t->print("$pass");
        $t->waitfor('/#/i');
        $found_proper_method=1;
    }
    if ($found_proper_method==0) {
        if ($t->waitfor('/#/i') == 1) {
            $found_proper_method=1;
        }
    }
    if ($found_proper_method==0) {
        if ($t->waitfor('/username/i') == 1) {
            $t->print("$user");
            $t->waitfor('/password/i');
            $t->print("$pass");
            
            if ($t->waitfor('/>/i') == 1) {
                $t->print("enable");
                $t->waitfor('/password/i');
                $t->print("$enable");
            }
            if ($t->waitfor('/#/i') == '1') {
                $found_proper_method=1;
            }            
        }
    }
    if ($found_proper_method==0) {
        if ($t->waitfor('/password/i') == 1 and $found_proper_method==0) {
            $t->print("$login");
        
            if ($t->waitfor('/>/i') == 1) {
                $t->print("enable");
                $t->waitfor('/password/i');
                $t->print("$enable");
            }
            if ($t->waitfor('/#/i') == '1') {
                $found_proper_method=1;
            }
        }
    }
    if ($found_proper_method==1) {
        #$t->put(Timeout=>20);
        $t->cmd("terminal length 0");
        $t->waitfor('/#/');
        $t->cmd("terminal pager 500");
        $t->waitfor('/#/'); 
        (@version) = $t->cmd("show version");
        
        ## Hier erfolgt die Auswertung des Gerätetyps
    }
    else {
        $devicetype="Kein Login möglich";
    }
    return $devicetype;
}