Reverse Lookup in Germany

Sysopsis:

This is an agi-script that does a reverse lookup of german telephone numbers.

For example:
  • you receive a call from 0049-69-123456.
  • The script looks the number up from the german telephone book and gives you the name in the variable ${LONGNAME} back.
  • in case of phone book lookup failure, it optionally supports lookup in older isdn4linux isdnlog callerid.conf content and as a last-ditch effort at least lookup of city names via isdnlog's isdnrate.

There is also a cache for storing already resolved numbers and a logfile.

Description:

The script returns ${LONGNAME} - it's also easily possible to get the address information (if provided by the public phonebook).
There are two interesting files:
  • CACHE="/var/spool/asterisk/invsuche_cache" - Here all numbers and resolved numbers are stored for faster lookup in the future (or manual resolving if number is not in phonebook)
  • LOG="/var/log/asterisk/anrufliste_log" - A logfile containing all callers

Example Dialplan

new Asterisk (1.4+?):


exten => _X.,1,Set(CHANNEL(language)=de)
exten => _X.,2,AGI(reverse.agi, ${CALLERID(num)})
exten => _X.,3,Set(CALLERID(name)=${LONGNAME})



old Asterisk:


exten => 12345,1,SetLanguage(de)
exten => 12345,2,AGI,reverse.agi| ${CALLERIDNUM}
exten => 12345,3,SetCIDName(${LONGNAME})
exten => 12345,4,SetCIDNum(${CALLERIDNUM}) 




This is the "reverse.agi"-script - place it in the AGI directory



#!/bin/sh
#
#read agi_request
#read agi_language
#read agi_channel
#read agi_type
#read agi_uniqueid
#read agi_callerid
#read agi_dnid
#read agi_rdnis
#read agi_context
#read agi_extension
#read agi_priority
#read agi_enhanced
#read agi_accountcode
#read emptyline

#pfad zum cachefile
CACHE="/var/spool/asterisk/invsuche_cache"

#pfad um das tempfile anzulegen
TMPFILE="/tmp/tmpsuche.html"
TMPFILE2="/tmp/tmpclir"
LOG="/var/log/asterisk/anrufliste_log"

echo "$1-$2-$3" >/tmp/reverse.tmp

if [ "$1" = " " ] || [ -z "$1" ]; then
    echo "Keine Nummer"
    #echo | tail -n 10 /var/log/syslog | grep "RING (" >>$TMPFILE2
    #if [ "`tail -c 10 $TMPFILE2`" = "z audio)  " ]; then
       NAME="analoger Anrufer"
       DETAILS="Keine details"
    #fi
    #if [ "`tail -c 10 $TMPFILE2`" = "(Speech) " ]; then
    #   NAME="aktiv unterdrueckt"
    #   DETAILS="ISDN anrufer ohne Nummer"
    #fi
else
    NUMMER=`echo $1 | sed -e "s/\ //g" -e "s/+49/0/"`
    echo "Suche nach $NUMMER im cache"
    NAME=`awk  -F '\t' '{ if ($1 == "'$NUMMER'") print $2 }' $CACHE`
    DETAILS=`awk  -F '\t' '{ if ($1 == "'$NUMMER'") print $3 }' $CACHE`
    echo "Name: \"$NAME\""
    echo "Details: \"$DETAILS\""
    #echo "name $NAME details $DETAILS" >> /tmp/reverse.tmp
    if [ -z "$NAME" ]; then
        wget -q --tries=3 --timeout=5 -O $TMPFILE "http://www1.dasoertliche.de/?form_name=search_inv&ph=$NUMMER"
        NAME=`grep 'na:' $TMPFILE | sed 's/.*na: "// ;s/",.*//'`
        DETAILS=`grep 'st:' $TMPFILE | sed 's/.*st: "// ;s/",.*//;s/%20/ /g'`", "`grep 'pc:' $TMPFILE | sed 's/.*pc: "// ;s/",.*//'`" "`grep 'ci:' $TMPFILE | sed 's/.*ci: "// ;s/",.*//;s/%20/ /g'`

        if [ -z "$NAME" ]; then
            if egrep "leider nicht erfolgreich|Kein Teilnehmer gefunden" $TMPFILE > /dev/null; then
                NAME="Telefonnummer $NUMMER"
                DETAILS="Kein Eintrag $2"
                CACHE_IT=1
            fi
            if [ "$FINAL" != "1" ]; then
              # dasoertliche had issues, thus try lookup in isdnlog's callerid.conf
              CALL_INFOS=`/usr/local/asterisk/callerid_from_isdnlog.conf.perl $NUMMER`
              if [ -n "$CALL_INFOS" ]; then
                NAME="$CALL_INFOS"
                DETAILS=""
                CACHE_IT=1
                FINAL=1
              fi
            fi
            if [ "$FINAL" != "1" ]; then
              CITY=`isdnrate -N $NUMMER|sed -e 's/.* - \(.*\) - .*/\1/; s/(DE)//g; s/Germany//g'`
              if [ -n "$CITY" ]; then
                NAME="$CITY"
                DETAILS=""
                CACHE_IT=1
                FINAL=1
              fi
            fi
        fi
        if [ -z "$NAME" ]; then
            NAME="Telefonnummer $NUMMER"
            DETAILS="Fehler $2"
        fi
        if [ "$CACHE_IT" = "1" ]; then
            printf "$NUMMER\t$NAME\t$DETAILS\n" >> $CACHE
        fi
    fi
fi

###
### Here you can add "additional alert code"
###

# directly source an external scriptlet, for better separation with this publicly updated script
. /usr/local/asterisk/reverse.agi_notifier_sh

#echo -e -n "mesg TEL:$NAME \nquit\n"; sleep 1; echo -e -n "mesg $DETAILS \nquit\n"; sleep 1; echo quit | telnet vdr.gehrig.lan 2001 &



printf "`date +%Y-%m-%d\ %H:%M` $NAME\t$NUMMER\n" >>$LOG
echo 'SET VARIABLE LONGNAME '"\"$NAME\"" >/dev/stdout
read in

exit 0



Return codes

Always returns 0.

Sample "additional alert code"

Here are some samples what you can also do with this Script:

  • Sample 1: Show Caller Information on VDR (Video Disc Recorder)


echo -e -n "mesg TEL:$NAME \nquit\n" | telnet vdr.gehrig.lan 2001
echo -e -n "mesg $DETAILS \nquit\n"  | telnet vdr.gehrig.lan 2001 



  • Sample 2: Show Caller Information on dBox2 (running on Linux)


wget "http://172.17.1.150/control/message?popup=Anruf%20von:%20 $NAME%0A$DETAILS" 



  • Sample 3: Show Caller Information on Windows hosts (winpopup) and Linux hosts (linpopup)


echo Anruf von $NAME $DETAILS | smbclient -M hostname 



isdnlog callerid.conf format to phone info converter


For additional callerid.conf-based number lookup within reverse.agi in case of dasoertliche lookup failure.



#!/usr/bin/perl -w

use strict;

if (scalar(@ARGV) < 1)
{
  exit 1;
}

my $candidate = $ARGV[0];

open(IDs, "/etc/isdn/callerid.conf");
my $number_section = 0;
my $number = "";
my $alias = "";
while(<IDs>) {
  if (/^\s*\[([[:alnum:]]*)\]\s*$/)
  { # found a section header

    $number_section = ($1 eq "NUMBER");

    # examine content of any previous sections
    if ($number)
    {
      # escape +
        $number =~ s/\+/\\+/g;
      # prepare a regex
        $number =~ s/\*/.\*/g;
      # print "number ".$number." candidate ".$candidate."\n";
      if ($candidate =~ /$number/)
        {
                print "$alias\n";
                exit 0;
        }
    }
    $number = "";
    $alias = "";
  }
  else
  {
  {
        if ($number_section)
        {
                if (/^\s*NUMBER\s*=\s*([0-9+\*]*)\s*$/)
                {
                        $number = $1;
                        $number =~ s/\+49/0/;
                }
                elsif
                (/^\s*ALIAS\s*=\s*(.*)\s*$/)
                {
                        $alias = $1;
                }
        }
  }
}





ToDo


Helpful additions might be gathered from e.g. "Rückwärtssuche bei dasoertliche.de neues Script": http://www.ip-phone-forum.de/showthread.php?t=164211

Questions

If you have any Questions about the reverse.agi script you can write Mail to mailto:thorsten@gehrig.de or Andreas Mohr.


See also


Keywords: isdnlog i4l isdn4linux misdn
(make sure that former isdnlog users hit this Asterisk solution)

See also

Created by: thorsten.gehrig, Last modification: Fri 02 of Aug, 2013 (01:12 UTC) by admin


Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+

Page Changes | Comments

 

Featured -

Search: