CodeWiki : routersh

WikiHome :: List Pages :: Login
cmantito.com

Script Information


routersh was a shell script designed for personal use as a login script that allows for management of iptables rules, route rules, and network interfaces, in a 'linux box as a router' environment. There is no support for it, but feel free to download it and customise it as you feel fit.



Script Source


routersh.pl
#!/usr/bin/perl
use Switch;

main();

sub main {

    $ENV{'PATH'} = "/bin:/sbin:/usr/sbin:/usr/bin";
   
    my $hostname = `hostname --fqdn`;
    chomp($hostname);

    clearScreen();
    moveCursor(28,2);
    fancyPrint($hostname."\n\n", 32, 1, 4);

    fancyPrint("Routing\n", 36, 4);
    fancyPrint("\t[1]", 33, 1); print " Start routing\n";
    fancyPrint("\t[2]", 33, 1); print " Stop routing\n";
    fancyPrint("\t[3]", 33, 1); print " Show routes\n";
    fancyPrint("\t[4]", 33, 1); print " Edit port forwards\n";
    print "\n";
    fancyPrint("Access Point\n", 36, 4);
    fancyPrint("\t[5]", 33, 1); print " SNMP AP Config\n";
    print "\n";
    fancyPrint("Network Interfaces\n", 36, 4);
    fancyPrint("\t[6]", 33, 1); print " Show interfaces\n";
    print "\n";
    fancyPrint("Clients\n", 36, 4);
    fancyPrint("\t[7]", 33, 1); print " Show DHCP leases\n";
    fancyPrint("\t[8]", 33, 1); print " Show all active clients\n";
    print "\n";
    fancyPrint("Other\n", 36, 4);
    #fancyPrint("\t[S]", 33, 1); print " Launch shell\n";
    fancyPrint("\t[X]", 33, 1); print " Exit to shell\n";

    print "\nPlease make a selction [1-7, S, X]: ";
    $choice = <STDIN>;
    chomp($choice);
    $choice = lc($choice);
   
    switch($choice){
        case "x" {
            exit;
        }
        case "s" {
            system('/bin/bash');
        }
        case "1" {
            $run = `/etc/rc.d/iptables start 2>&1`;
            if($run =~ /FAIL/i){
                fancyPrint("Start routing failed! Press RETURN.", 37, 41, 1);
            }else{
                fancyPrint("Routing started. Press RETURN.", 30, 42, 1);
            }
            $wait = <STDIN>;
        }
        case "2" {
            $run = `/etc/rc.d/iptables stop 2>&1`;
            if($run =~ /FAIL/i){
                fancyPrint("Stop routing failed! Press RETURN.", 37, 41, 1);
            }else{
                fancyPrint("Routing stopped.  Press RETURN.", 30, 42, 1);
            }
            $wait = <STDIN>;
        }
        case "3" {
            fancyPrint("Press 'q' to return. Press RETURN to continue.", 30, 42, 1);
            $wait = <STDIN>;
            system("/sbin/route -e -n | /bin/less");
        }
        case "4" {
            print "What you like to add or delete a port forward? (add/del): ";
            $pfDo = <STDIN>;
            chomp($pfDo);

            if(lc($pfDo) eq "add"){
                print "TCP or UDP? (tcp/udp): ";
                $proto = <STDIN>;
                chomp($proto);

                if(lc($proto) eq "tcp"){ $proto = "tcp"; }
                elsif(lc($proto) eq "udp"){ $proto = "udp"; }
                else{ main(); }

                print "Destination port (WAN): ";
                $dPort = <STDIN>;
                chomp($dPort);

                print "Destination IP (LAN): ";
                $dIP = <STDIN>;
                chomp($dIP);

                print "New destination port (LAN): ";
                $newdPort = <STDIN>;
                chomp($newdPort);

                open(FORWARDS, ">>/etc/ipForwards.conf");
                print FORWARDS $proto.",".$dPort.",".$dIP.",".$newdPort."\n";
                close(FORWARDS);

                system("/usr/sbin/iptables -A PREROUTING -t nat -p ".$proto." --dport ".$dPort." -i wan0 -j DNAT --to ".$dIP.":".$newdPort);
                system("/usr/sbin/iptables -A FORWARD -p ".$proto." -d ".$dIP." --dport ".$newdPort." -i wan0 -o lan0 -j ACCEPT");
                system("/usr/sbin/iptables -A FORWARD -p ".$proto." -s ".$dIP." --sport ".$newdPort." -i lan0 -o wan0 -j ACCEPT");
               
            }elsif(lc($pfDo) eq "del"){
                open(FORWARDS, "</etc/ipForwards.conf");
               
                $count = 0;
                print "Num: Proto,wanPort,lanIP,lanPort\n";
                while($line = <FORWARDS>){
                    chomp($line);
                    $forwards[$count] = $line;
                    print $count.": ".$line."\n";
                    $count++;
                }
                close(FORWARDS);
                print "Number of forward to remove: ";
                $rmPort = <STDIN>;
                chomp($rmPort);
                if($rmPort eq ""){ main(); }
                ($proto, $dPort, $dIP, $newdPort) = split(/,/, $forwards[$rmPort]);
                system("/usr/sbin/iptables -D PREROUTING -t nat -p ".$proto." --dport ".$dPort." -i wan0 -j DNAT --to ".$dIP.":".$newdPort);
                system("/usr/sbin/iptables -D FORWARD -p ".$proto." -d ".$dIP." --dport ".$newdPort." -i wan0 -o lan0 -j ACCEPT");
                system("/usr/sbin/iptables -D FORWARD -p ".$proto." -s ".$dIP." --sport ".$newdPort." -i lan0 -o wan0 -j ACCEPT");

                delete $forwards[$rmPort];

                open(FORWARDS, ">/etc/ipForwards.conf");
                foreach $newLine(@forwards) {
                    if($newLine ne ""){
                        print FORWARDS $newLine."\n";
                    }
                }
                close(FORWARDS);
            }else{
                main();
            }
        }
        case "5" {
            system("/usr/bin/ap-config");
        }
        case "6" {
            fancyPrint("Press 'q' to return. Press RETURN to continue.", 30, 42, 1);
            $wait = <STDIN>;
            system("/sbin/ifconfig | /bin/less");
        }
        case "7" {
            my @leases;
            my $x;
           
            clearScreen();
            open(DHCP, "/var/state/dhcp/dhcpd.leases");
            while($line = <DHCP>){
                if($line =~ /^lease (.+) \{/){
                    $ipAddr = $1;
                }
                if($line =~ /^\s+hardware ethernet (.+);/){
                    $macAddr = $1;
                }
                if($line =~ /^\s+client-hostname "(.+)";/){
                    $clientName = $1;
                }
                if($line =~ /^}/){
                    if(!$clientName){
                        $clientName = "<none>";
                    }
                    $leases[$x][0] = $clientName;
                    $leases[$x][1] = $ipAddr;
                    $leases[$x][2] = $macAddr;
                    $clientName = "";
                    $ipAddr = "";
                    $macAddr = "";
                    $x++;
                }
            }
            close(DHCP);
            fancyPrint("Hostname", 4);
            print "\t";
            fancyPrint("Assigned IP", 4);
            print "\t\t";
            fancyPrint("MAC Address", 4);
            print "\n";
            my %shownLease;
            for($q = 0; $q <= $#leases; $q++){
                if($shownLease{$leases[$q][2]} < 1){
                    print $leases[$q][0]."\t\t";
                    print $leases[$q][1]."\t\t";
                    print $leases[$q][2]."\n";
                    $shownLease{$leases[$q][2]} = 1;
                }
            }
            fancyPrint("Press RETURN to continue.", 30, 42, 1);
            $wait = <STDIN>;
        }
        case "8" {
            clearScreen();
            system("/sbin/arp -n -i lan0");
            fancyPrint("Press RETURN to continue.", 30, 42, 1);
            $wait = <STDIN>;
        }      
        else {
            main();
        }
    }

    main();

}
sub clearScreen {
    print "\e[2J";
    return;
}

sub moveCursor {
    my ($x, $y) = @_;

    print "\e[".$y.";".$x."H";
    return;
}

sub fancyPrint {
    my ($text) = shift(@_);
   
    $printStr = "\e[";
    for($i = 0; $i <= $#_; $i++){
        $printStr .= $_[$i];
        if($i < $#_){
            $printStr .= ";";
        }else{
            $printStr .= "m";
        }
    }

    $printStr .= $text."\e[0m";
    print $printStr;
    return;
}



Categories: CategoryAbandoned
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki