Hallo zusammen,
anbei ein kleines Skript, was die Anrufliste der Fritzbox aktualisiert und in die rsslog-Datenbank postet. Was anzupassen ist: die Adresse der Fritzbox, das Passwort und der Pfad zur Datenbank.
Achtung: Das ganze sollte NICHT als WG-Plugin laufen, bei längeren Anruflisten läuft das auch schommal 10-20s, und das ist als WG-Plugin gänzlich ungeeignet. Ich musste allerdings ein Präfix auswählen, und das passte noch am besten.
Gruss,
der Jan
anbei ein kleines Skript, was die Anrufliste der Fritzbox aktualisiert und in die rsslog-Datenbank postet. Was anzupassen ist: die Adresse der Fritzbox, das Passwort und der Pfad zur Datenbank.
Achtung: Das ganze sollte NICHT als WG-Plugin laufen, bei längeren Anruflisten läuft das auch schommal 10-20s, und das ist als WG-Plugin gänzlich ungeeignet. Ich musste allerdings ein Präfix auswählen, und das passte noch am besten.
Gruss,
der Jan
Code:
#!/usr/bin/perl # # (c) 2011 by Jan N. Klug # Licenced under the GPLv3 # # sqlite-Anbindung inspiriert von rsslog.pl by Michael Markstaller # Fritzbox-Login aehnlich http://blog.soldierer.com/2009/12/06/neues-fritzbox-session-id-login-verfahren-in-perl/ by Walter Soldierer # # Fritzbox-Anrufliste für rsslog aufbereiten # # config my $boxpasswort = "XXXXXX"; my $boxaddress = "fritz.box"; my $logdb = '/var/www/rsslog.db'; # ab hier nichts aendern #allgemeine Deklarationen use strict; use LWP 5.64; use Digest::MD5 'md5_hex'; use XML::Simple; use DBI; use DateTime; # Fritzbox-LOGIN # challenge abholen my $user_agent = LWP::UserAgent->new; my $http_response = $user_agent->get("http://$boxaddress/cgi-bin/webcm?getpage=../html/login_sid.xml"); $http_response->content =~ /<Challenge>(\w+)<\/Challenge>/i and my $challengeStr = $1; # response zur challenge generieren my $ch_Pw = "$challengeStr-$boxpasswort"; $ch_Pw =~ s/(.)/$1 . chr(0)/eg; my $challenge_response = "$challengeStr-" . lc(md5_hex($ch_Pw)); # SID ermitteln $http_response = $user_agent->post("http://$boxaddress/cgi-bin/webcm", ["login:command/response" => $challenge_response, getpage => "../html/login_sid.xml" ], ); $http_response->content =~ /<SID>(\w+)<\/SID>/i and my $SID = $1; # Gespraechsdaten aktualisieren und XML Gespraechs-Daten holen $http_response = $user_agent->post("http://$boxaddress/cgi-bin/webcm", [getpage => '../html/de/menus/menu2.html','sid' => $SID, 'var:menu' => 'home', 'var:pagename' => 'foncalls' ]); $http_response = $user_agent->get("http://$boxaddress/cgi-bin/webcm?getpage=../html/de/home/foncallsdaten.xml&sid=$SID" ); # XML Daten parsen my $xml = new XML::Simple (KeyAttr=>[]); my @data = @{$xml->XMLin($http_response->content)->{Calls}}; #@data = sort {$b->{'id'} <=> $a->{'id'}} @data; # Datenbank öfnnen und letzten fritzcall ermitteln if (! -e $logdb) { die "$logdb existiert nicht! Bitte mit rsslog.php anlegen"; # FIXME: create sqlite-db } my $dbargs = {AutoCommit => 0, PrintError => 1}; my $dbh = DBI->connect("dbi:SQLite2:dbname=$logdb", "", "", $dbargs) or die "Couldn't open database: " . DBI->errstr; $dbh->commit(); my $sth = $dbh->prepare('SELECT Max(t) FROM Logs WHERE tags LIKE \'%fritzcall%\';') or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() # Execute the query or die "Couldn't execute statement: " . $sth->errstr; my @dblast = $sth->fetchrow_array(); $sth->finish(); (my $year, my $month, my $day, my $hour, my $minute, my $second) = split(/[- :]/, @dblast[0]); if ($year == undef) { $year=1900; $month=1; $day=1; $hour=0; $minute=0; $second=0; } my $dblastdate = DateTime->new( year => $year, month => $month, day => $day, hour => $hour, minute => $minute, second =>$second); # alle Gespreachsdaten durchsehen und neue eintragen $second = "00"; my @calltype = ('inbound', 'missed', 'outbound'); foreach my $call (@data) { ($day, $month, $year, $hour, $minute) = split(/[. :]/, $call->{'Date'}); $year = "20" . $year; my $calldate = DateTime->new( year => $year, month => $month, day => $day, hour => $hour, minute => $minute, second =>$second, time_zone => 'Europe/Berlin'); $calldate->set_time_zone('UTC'); # all sqlite-data is in UTC ! if ($calldate>$dblastdate) { # is a new entry #my $sqldate = $calldate->strftime("%Y-%m-%d %T"); my $sqlquery = 'INSERT INTO Logs(content, title, tags, t) VALUES( ' . " '" . $call->{'Number'} . "', '', " . " 'fritzcall,phone," . $calltype[$call->{'Type'}-1] . ",noack'," . " '" . $calldate->strftime("%Y-%m-%d %T") . "');"; $dbh->do($sqlquery) or die "Couldn't execute command: " . $dbh->errstr; $dbh->commit() or die "Couldn't execute command: " . $dbh->errstr; } } $dbh->disconnect();
Kommentar