Search code examples
bashtelnet

If telnet output contains something execute action


I am trying to write a small script in bash. My target is that a Phone Call on my FritzBox will mute or pause my TV.

I get the information of a call via telnet (telnet fritz.box 1012) on my RaspberryPi and if there is coming a phone call in I get this output:

24.08.15 14:03:05;RING;0;017mobilephonenumber;49304myhomenumber;SIP0;

The only Part that is every time the same is the RING before the calling number. What I need is a script that checks the output of the telnet and if in the telnet output is a ring than execute an action in my case i just need to do a http request on an internal site or start another script.

This is what I have tried is this:

#!/bin/bash

#string='echo "My string"'
string=$(telnet –e p fritz.box 1012)
for reqsubstr in 'alt' 'RING';do
  if [ -z "${string##*$reqsubstr*}" ] ;then
      echo "String '$string' contain substring: '$reqsubstr'."
    else
      echo "String '$string' don't contain substring: '$reqsubstr'."
    fi
  done

But I don`t get the output of my telnet session into the string. Anyone who can help me?


Solution

  • Here my short conclusion: To see incomming calls on your FritzBox you can simply execute: telnet fritz.box 1012 in your terminal. The output should look like that:

    24.08.15 14:03:05;RING;0;017mobilephonenumber;49304myhomenumber;SIP0;
    

    I my case I want the execute an command the mutes my TV. Here are the instructions how I controll my TV via Web/RaspberryPi (Philips Harmony Hub) if some one is interested.

    To execute a command on an incomming call create a script called action.sh

    #!/bin/bash
    while true;
    do
      read logline
      for substr in 'alt' '\;RING\;'
      do
        if [[ "$logline" = *${substr}* ]]; then
          echo "Got a match"
          #here you can put the command you want to execute on an incoming call
        fi
      done
    done
    

    After you create it make it executable using chmod +x action.sh After that you can start the script using nc 192.168.1.1 1012 | action.sh And that's how you execute a script on an incomming call! Special thanks to tgo!!