#!/usr/bin/perl

#
# Fritz!BOX telnet automation
#

use Net::Telnet;
use Getopt::Std;

sub log { }

my $pass = undef;
my $host = "192.168.178.1";
my $cmd = "ps";
my $timeout = 10;

my %o=();
getopts("t:p:c:h:",\%o);
# Moegliche Parameter verarbeiten
if (not defined $o{h}) {
        print "\n[*] help / usage\n\n";
        print "\t-h <host>         IP of your Fritz!BOX mybe 192.168.178.1?\n";
        print "\t-p <password>     [optional]\n";
        print "\t-c <cmd>          [default \"ps\"]\n";
        print "\t-t <int>          [default \"10\"]\n\n";
        exit 0;
}
if (defined $o{p}) {
        $pass = $o{p};
}
if (defined $o{h}) {
        $host = $o{h};
} 
if (defined $o{c}) {
        $cmd = $o{c};
}
if (defined $o{t}) {
        $timeout = $o{t};
}

#
# MAIN ()
#
my $t = Net::Telnet->new( Timeout => $timeout,
                          Prompt  => '/#/',
                          Host    => $host, 
			  Errmode => sub { main::log(@_) } );
if ($pass) {
	$t->waitfor(String => 'Fritz!Box web password:', Timeout => $timeout);
	$t->print($pass);
} else {
  	print "[*] No Password? are you sure?\n";
}
$t->waitfor(String => '#', Timeout => $timeout) or die "[*] ERROR no console Prompt: @{[ $t->errmsg() ]}\n"; 

if ($cmd eq "") { $cmd = "ps"; }
@out = $t->cmd($cmd);
$t->close;
foreach (@out) { print "$_"; }

exit 0;


