#!/usr/bin/perl use warnings; use strict; use File::Spec::Functions; my $dir = '/home/mm/.t_m'; my $name = 'naviga3'; my $max_MB = 95; mkdir( $dir ) or die "$!" if ! -d $dir; my $file = catfile( $dir, $name ); sub count_traffic { my $dev = '/proc/net/dev'; my $gesamt = 0; my $traffic = 0; my $summe = 0; my $mb_traffic=0; my @date = localtime(); my $day = $date[3]; my $month = $date[4] + 1; my $year = $date[5] + 1900; my $pidofpppd; if ( ! -f $file ) { open( my $fh, '>', $file ) or die "$!"; print $fh "$day:$month:$year:$traffic\n"; close( $fh ); } open( my $in, '<', $file ) or die "$!"; while ( <$in> ) { $gesamt = $1 if /^$day:$month:$year:(\d+)$/; } close( $in ); chomp( $pidofpppd = `pidof pppd` ); kill 15, $pidofpppd if $pidofpppd; system( "wvdial &" ); sub catch_signal { my $pidofwvdial = `pidof wvdial`; kill( 15, $pidofwvdial ); }; local $SIG{INT} = \&catch_signal; local $SIG{QUIT} = \&catch_signal; local $SIG{KILL} = \&catch_signal; local $SIG{TERM} = \&catch_signal; while ( 1 ) { chomp( $pidofpppd = `pidof pppd` ); last if $pidofpppd; select( undef, undef, undef, 0.25 ); } while ( 1 ) { open( my $proc_net, '<', $dev ) or die "$!"; while ( <$proc_net> ) { if ( $_ =~ /^\s*ppp0:\s+(\d+)(?:\s+\d+){7}\s+(\d+)(?:\s+\d+){7}\s*$/ ) { my $summe = $1 + $2; $traffic = $gesamt + $summe; $mb_traffic = $traffic / 1048576; printf "%.2f\n", $mb_traffic; my @d = localtime(); if ( $d[3] != $day ) { $summe=0; $gesamt=0; $traffic=0; } if ( $mb_traffic > $max_MB ) { $pidofpppd = `pidof pppd`; kill 15, $pidofpppd; #write_to_file( $traffic ); exit; } } } close( $proc_net ); select( undef, undef, undef, 0.25 ); chomp( my $pidofpppd = `pidof pppd` ); last if ! $pidofpppd; } END { write_to_file( $traffic ); }; } count_traffic();