#!/usr/bin/perl
use Net::Telnet::Cisco;
use Digest::MD5::File;
use Env qw(TEMP USERNAME PID);
use strict;

AGAIN:
print "\n\nx beendet\n\n";
print "IP:";
my $HOST=<STDIN>;
chomp $HOST;

exit if ($HOST eq "x");

my $session = Net::Telnet::Cisco->new(Host => "$HOST", Errmode=>'return');
$session->login(Name => "batman", Password => "LTBTTTra25");
my @rcon = $session->cmd('show running');
$session->close;

my @rconfig=(); # führende returns entfernen
foreach my $line (@rcon) {
    chomp $line;
    if ($line eq "") {next;}
    elsif ($line =~ /^\n/) {
        my ($l) = $line =~ /\n(.*)/;
        push @rconfig,$l;
    }
    else {
        push @rconfig,$line;
    }
}

my $inint=0;
my $intname="";
my $inlist="";
my $outlist="";
my @INTERFACES=();

foreach my $line (@rconfig) {
    chomp $line;
    if ($inint==0) {
        if ($line =~ /^[\s]*interface/) {
            ($intname)=$line=~/interface (.*)/;
            $inint=1;
            #print "$line\n";
        }
    }
    else {
        if ($line =~ /\bip access-group .* in$/) {
            ($inlist)=$line=~/access-group (.*) in/;
        }
        if ($line =~ /\bip access-group .* out$/) {
            ($outlist)=$line=~/access-group (.*) out/;
        }
        if ($line =~ /^[\s]*!/) {
            if ($inlist ne "" or $outlist ne "") {
                push @INTERFACES, "$intname#$inlist#$outlist";
            }
            $intname=""; $inlist=""; $outlist="";
            $inint=0;
        }
    }
}

#printf "%2s %20s %20s %20s\n", "ID", "Interface", "ACL in", "ACL out";
my $i=0;
my @MENU=();
print "\n";
foreach (@INTERFACES) {
    my ($int,$in,$out)=split /#/, $_;
    if ($in ne "") {
        printf "%2d %30s %30s (in)\n", $i, $int, $in;
        push @MENU, "$int#$in#in";
        $i++;
        
    }
    if ($out ne "") {
        printf "%2d %30s %30s(out)\n", $i, $int, $out;
        push @MENU, "$int#$out#out";
        $i++;
        
    }
}

print "\nWhich ACL do you want to edit?";
my $choice=<STDIN>;
chomp $choice;
exit if ($choice eq "x");
print "\n";

my $int;
my $acl;
my $dir;
($int,$acl,$dir)=split/#/,$MENU[$choice];

my $rc;
$rc=join "\n",@rconfig;

my $ACCESS="";
my $CONTENT="";                                                #[\s]*
($ACCESS,$CONTENT)= $rc =~ /(ip access-list extended $acl\n(([\s]+[permit|deny|remark].*\n)*))/m;
my @CONT=split/\n/,$CONTENT;

open TEMPF, ">temp$$.txt";
foreach (@CONT) {
    printf TEMPF "$_\n";
}
close TEMPF;

#print "$TEMP\\pdk-$USERNAME-$$\\"; <STDIN>;

my $olddigest = Digest::MD5::File::file_md5_hex("temp$$.txt");
system "$TEMP\\pdk-$USERNAME-$$\\editor.exe temp$$.txt";
#system "editor.exe temp$$.txt";
my $newdigest = Digest::MD5::File::file_md5_hex("temp$$.txt");

if ($newdigest ne $olddigest) {
    print "Save changes? YES saves!";
    my $save=<STDIN>;
    chomp $save;
    if ($save eq "YES") {
        print "SAVING\n";
        # telnet
        $session = Net::Telnet::Cisco->new(Host => "$HOST", Errmode=>'return');
        $session->login(Name => "batman", Password => "LTBTTTra25");
        
        # conf t
        $session->cmd('conf t');
        
        # int $int
        $session->cmd("interface $int");
        
        # no ip access-group $acl $dir
        $session->cmd("no ip access-group $acl $dir");
        
        # exit
        $session->cmd("exit");
        
        # no ip access-l ext $acl
        $session->cmd("no ip access-list extended $acl");
        
        # ip access-l ext $acl
        $session->cmd("ip access-list extended $acl");
                      
        # <temp.txt
        open EDIT, "<temp$$.txt";
        my @NEWACL=<EDIT>;
        close EDIT;
        
        foreach (@NEWACL) {
            $session->cmd("$_");
        }
        
        # int $int
        $session->cmd("interface $int");
        
        # ip access-group $acl $dir
        $session->cmd("ip access-group $acl $dir");
        
        # end
        $session->cmd("end");
        
        # wr mem
        $session->cmd("wr mem");
        
	$session->close;

        print "SAVED";
    }
    else {
        print "CANCELED";
    }
}
else {
    print "NO CHANGES";
}
unlink "temp$$.txt";
print "\n\nNochmal? (y)";

my $again=<STDIN>;
chomp $again;
exit if ($again ne "y");
goto AGAIN if ($again eq "y");