A linux callerid ( caller id ) modem bash script

Well just thought I would share a quick bash script I cobbled together to record the numbers that call the landline phone.

Now the script uses ttyUSB0 since I have a USB connected modem. So you will need to replace the ttyUSB0 with the serial or USB device you are using.
So far I have tested this script on both a old laptop running Ubuntu 14.04LTS and a Raspberry Pi running Raspbian.

#!/bin/bash
cd ~/callerid
echo AT#CID=1 >/dev/ttyUSB0
NAME="N/A"
TIME="N/A"
DATE="N/A"
NUMBER="N/A"
while read line; do
  temp1="$( cut -d '=' -f 1 <<< "$line" )"
  temp2="$( cut -d '=' -f 2- <<< "$line" )"
  temp1="${temp1%"${temp1##*[![:space:]]}"}"
  temp1="${temp1#"${temp1%%[![:space:]]*}"}"
  temp2="${temp2#"${temp2%%[![:space:]]*}"}"
  temp2="${temp2%"${temp2##*[![:space:]]}"}"

  if [ "$temp1" = "DATE" ]; then
   DATE="$temp2"
  fi

  if [ "$temp1" = "TIME" ]; then
   TIME="$temp2"
  fi

  if [ "$temp1" = "NMBR" ]; then
   NUMBER="$temp2"
  fi

  if [ "$temp1" = "NAME" ]; then
   NAME="$temp2"
   NOW=$(date +"%Y-%m-%d-%H-%M-%S")
   BCKUP="$NOW-$NUMBER.txt"
   echo "$NOW - $NUMBER - $NAME" >>$BCKUP
   echo "$NOW - $NUMBER - $NAME"
   DATE="N/A"
   TIME="N/A"
   NUMBER="N/A"
   NAME="N/A"
  fi
done < /dev/ttyUSB0

2 thoughts on “A linux callerid ( caller id ) modem bash script”

  1. After a while of using this bash script I decided to add a few features for myself to notify me by text if someone had tried calling me. I also added a couple sections for picking up the phone line using the modem, playing a sound file, and then hanging the phone line up.

    Here is a example of my code update.

    #!/bin/bash
    cd ~/callerid
    echo Starting CallerID
    echo AT#CID=1 >/dev/ttyUSB0
    NAME="N/A"
    TIME="N/A"
    DATE="N/A"
    NUMBER="N/A"
    while read line; do
      temp1="$( cut -d '=' -f 1 < << "$line" )"
      temp2="$( cut -d '=' -f 2- <<< "$line" )"
      temp1="${temp1%"${temp1##*[![:space:]]}"}"
      temp1="${temp1#"${temp1%%[![:space:]]*}"}"
      temp2="${temp2#"${temp2%%[![:space:]]*}"}"
      temp2="${temp2%"${temp2##*[![:space:]]}"}"
    
      if [ "$temp1" = "DATE" ]; then
       DATE="$temp2"
      fi
    
      if [ "$temp1" = "TIME" ]; then
       TIME="$temp2"
      fi
    
      if [ "$temp1" = "NMBR" ]; then
       NUMBER="$temp2"
      fi
    
      if [ "$temp1" = "NAME" ]; then
       NAME="$temp2"
       NOW=$(date +"%Y-%m-%d-%H-%M-%S")
       BCKUP="$NOW-$NUMBER.txt"
       echo "$NOW - $NUMBER - $NAME" >>$BCKUP
       echo "$NOW - $NUMBER - $NAME"
       if [ "$NUMBER" = "5555555151" ]; then
         /usr/bin/ruby1.8 ~/bin/voicesms.rb -m "HOME: Brother called home line"
       fi
       if [ "$NUMBER" = "5555555152" ]; then
         /usr/bin/ruby1.8 ~/bin/voicesms.rb -m "HOME: Dad called home line"
       fi
       if [ "$NUMBER" = "5555555153" ]; then
         /usr/bin/ruby1.8 ~/bin/voicesms.rb -m "HOME: Friend called home line"
       fi
       if [ "$NUMBER" = "5555555251" ]; then
         echo ATH1 >/dev/ttyUSB0
         omxplayer /home/pi/Music/callerstop.flac
         sleep 2
         echo ATH >/dev/ttyUSB0
       fi
       if [ "$NUMBER" = "5555555252" ]; then
         echo ATH1 >/dev/ttyUSB0
         omxplayer /home/pi/Music/callerstop.flac
         sleep 2
         echo ATH >/dev/ttyUSB0
       fi
       DATE="N/A"
       TIME="N/A"
       NUMBER="N/A"
       NAME="N/A"
      fi
    done < /dev/ttyUSB0
    

    My next task is to probably change the section of code where it picks up the phone line to something that uses a text database of phone numbers or maybe uses a MySQL database that has my list of spam phone numbers. One of these days, months, or years I will get off my rear and do this.

    1. After some feedback on Facebook about this script it currently does not work on current distros of Raspbian. So at this time I thought I would let you know this script may not work for you at this time.

Comments are closed.