Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

PTkPPP

by le (Friar)
on Jun 05, 2000 at 22:33 UTC ( [id://16477]=sourcecode: print w/replies, xml ) Need Help??
Category: Networking
Author/Contact Info Lukas Ertl lukas.ertl@gmx.at
Description: This is a Tk frontend for modem dialers. It depends on pppd(8) and chat(8). It logs connection-times to a file and calculates total times and costs.
#!/usr/bin/perl -w
#
## ptkppp
#
## a Perl/Tk PPP dialer frontend
#
## (c) 1999 Lukas Ertl <lukas.ertl@gmx.at>
#

use Tk;

#########################################################
#  Configuration
## adjust to your needs

# The path to the pppd(8) daemon
$pppd = "/usr/sbin/pppd";

# Your modem device
$device = "your modem device";

# The path to the chat(8) program
$chat = "/usr/bin/chat";

# The phone number to dial
$phone = "your phone number";

# $user and $remotename are usually defined in /etc/ppp/pap-secrets, i
+n the
# form:
# username   serveralias   <password>
$user = "your username to login";
$serveralias = "your serveralias as defined in /etc/ppp/pap-secrets";

# This is the full dial command, you may need to adjust it, see pppd(8
+)
# and chat(8)
$dialcommand = <<EOT;
$pppd $device connect '$chat -v "" at\\&f1 OK atdt$phone CONNECT' 1152
+00 modem crtscts defaultroute user $user remotename $serveralias
EOT
$hangupcommand = "killall pppd";

# The file where connections are logged
$logfile = "your logfile";

# The file that lists holidays, like
# ...
# Dec 25
# Dec 26
# ...
# and so on
$holidayfile = "your holidays file";

# The hour where 'cheap' time begins, in Austria this is 18h.
$cheapbegin = 18;

# The hour until 'cheap' time lasts. In Austria this is 8h, but we hav
+e to
# define 7, because we calculate until 7:59:59 h.
$cheapend = 7;

# The delay in seconds between impulses, during 'cheap' time.
$impulse_delay_long = 360;

# The delay in seconds between impulses, during normal time.
$impulse_delay_short = 120;

# Number of initial impulses that are counted when a connection is est
+ablished
$initial_impulse = 1;

# Costs of one impulse.
$impulse_cost = 1.116;

# Your currency shorthand, fill in whatever you like.
$currency = "ATS";

## Configuration end
#  You don't need to change anything beyond.
#########################################################

$credits = "-- Perl/Tk PPP --\nPPP dialer frontend";

$message = "Idle.";

my $mw = MainWindow->new;

%counter = (
        'window' => $mw,
        'hrs' => 0,
        'min' => 0,
        'sec' => 0,
        'time' => '00:00:00',
        'stop' => 1
        );

$program_name  = $mw->Label(-textvariable => \$credits)->pack;
$status = $mw->Label(-textvariable => \$message)->pack;
$count = $mw->Label(-textvariable => \$counter{'time'})->pack;
$dial_button = $mw->Button(-text => "Dial",
               -command => \&dial)->pack;
$hangup_button = $mw->Button(-text => "Hangup",
                 -command => \&hangup)->pack;
$log_view_button = $mw->Button(-text => "Logfile",
                   -command => \&logview)->pack;
$exit_button = $mw->Button(-text => "Quit",
               -command => sub {exit} )->pack;


$dial_button->configure(-width => 8);
$hangup_button->configure(-width => 8);
$log_view_button->configure(-width => 8);
$exit_button->configure(-width => 8);

MainLoop;


sub dial {
    $counter{'hrs'} = $counter{'min'} = $counter{'sec'} = 0;
    $status = system("$dialcommand");
    die $! unless ($status == 0);

    $message = "Running...";

    if ($counter{'stop'}) {
        $counter{'stop'} = 0;
        &switch;
    }
}

sub hangup {
    open (LOG, ">> $logfile") || die $!;
    $status = system("$hangupcommand");
    die $! unless ($status == 0);

    $counter{'stop'} = 1;

    $today = localtime;

    print LOG "$today : hangup after $counter{'time'}\n";

    $message = "Inactive...";
    close (LOG);
}

sub switch {
    return if $counter{'stop'};
    if ($counter{'sec'} > 59) {
        $counter{'sec'} = 0;
        $counter{'min'}++;
    }
    if ($counter{'min'} > 59) {
        $counter{'min'} = 0;
        $counter{'hrs'}++;
    }
    $counter{'time'} = sprintf("%02d:%02d:%02d", $counter{'hrs'},
                   $counter{'min'}, $counter{'sec'});
    $counter{'window'}->after(1000, \&switch);
    $counter{'sec'}++;
}

sub logview {
    my ($total_hrs, $total_min, $total_sec, $impuls_sum) = 0;
    my @lines = ();
    my $logview = $mw->Toplevel();
    my $top = $logview->Frame()->pack;
    my $bottom = $logview->Frame()->pack;
    my $clear_button = $bottom->Button(-text => "Clear log",
                    -command => \&clear_log)->pack;
    open (LOG, "$logfile") || die $!;

    while (<LOG>) {
        next if /^\n/;
        chomp;
        if (/^(\w+)\s+(\w+)\s+(\d+)\s+(\d+):.+after\s+(\d+):(\d+):(\d+
+)$/) {
                $wday = $1;
                $month = $2;
                $day = $3;
                $downtime = $4;
                $total_hrs += $5;
                $total_min += $6;
                $total_sec += $7;
                checktime($downtime);
                checkday($wday, $month, $day);
                calc_impulse($5, $6, $7, $freizeit);
                $impuls_sum += $impulse;
                push @lines, $_;
        }
    }

    close (LOG) || die $!;

    my $listbox = $top->Scrolled('Listbox', -scrollbars => 'oe');
    $listbox->insert('end', @lines);
    $listbox->configure(-width => 45);
    $listbox->pack;

    if ($total_sec > 59) {
        $sec = $total_sec % 60;
        $min_from_sec = int($total_sec / 60);
        $total_min += $min_from_sec;
    }

    if ($total_min > 59) {
        $min = $total_min % 60;
        $hrs_from_min = int($total_min / 60);
        $total_hrs += $hrs_from_min;
    } else {
        $min = $total_min;
    }

    $money = $impuls_sum * $impulse_cost;

    $output = sprintf("Total online time:\t%d:%02d:%02d\n\nTotal costs
+: %d %s", $total_hrs, $min, $sec, $money, $currency);

    my $label = $bottom->Label(-textvariable => \$output)->pack;
    my $quit_button = $bottom->Button(-text => "Close",
                       -command => sub { $logview->destroy })->pack;
}

sub clear_log {
    system("cp $logfile $logfile.old");
    unlink("$logfile") || die $!;
}

sub checkday {
    my ($wday, $month, $day) = @_;
    if ($wday eq "Sat" || $wday eq "Sun") {
        return $freizeit = 1;
    } else {
        my $date = "$month $day";
        open (HOLIDAYS, "$holidayfile") or die $!;
        while ($x = <HOLIDAYS>) {
                chomp $x;
                $x =~ /$month/ ? return $freizeit = 1 : return $freize
+it = 0;
        }
    close (HOLIDAYS);
    }
}

sub checktime {
    my $time = shift;
    ($time >= $cheapbegin || $time <= $cheapend) ? return $freizeit = 
+1 : return $freizeit = 0;
}

sub calc_impulse {
    my $hours = shift;
    my $minutes = shift;
    my $seconds = shift;
    my $factor = shift;
    $minutes += $hours * 60;
    $seconds += $minutes * 60;
    if ($factor == 1) {
        $impulse = int($seconds / $impulse_delay_long) + $initial_impu
+lse;
        return $impulse;
    } else {
        $impulse = int($seconds / $impulse_delay_short) + $initial_imp
+ulse;
        return $impulse;
    }
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://16477]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found