giovedì 26 giugno 2014

The simplest Click2Dial implementation EVER

Here's a really lightweight implementation of the Click to Dial feature for softphones, brought you by the flexibility of Asterisk and Linux shell scripting.

Prerequisites:

- Apache and Sudo are installed
- All Iax and Sip peers defined in iax.conf and sip.conf do require to qualify with the QUALIFY=X directive.
- The pbx_spool.so module is loaded (it is required to use Asterisk call files)

Here's the procedure:

nano /etc/sudoers

comment out this line:

Default requiretty

and append this line:

apache ALL=(ALL) NOPASSWD: ALL

at the end of file (THIS IS INSECURE!!!).


# nano /var/www/cgi-bin/index.cgi (NO CUSTOMIZATION NEEDED!)

#!/bin/bash
echo Content-type: text/html
echo ""
echo "<html><head><title>Click2Call</title></head><body>"


rm -rf /tmp/peers
sudo asterisk -rx 'iax2 show peers' | grep 'ms)' | awk  -F' ' 'BEGIN{OFS=FS;} {print $1,$2;}' | awk -F"/" '{print "iax2",$2;}' >>/tmp/peers
sudo asterisk -rx 'sip show peers' | grep 'ms)' | awk  -F' ' 'BEGIN{OFS=FS;} {print $1,$2;}' | awk -F"/" '{print "sip",$2;}' >>/tmp/peers


a=`sudo asterisk -rx 'iax2 show peers' | grep 'ms)' | awk -F "/" '{printf $1"\n";}'`
for dummy in $a
do
   echo "<a href="http://n.n.n.n/cgi-bin/call.sh?iax2/$dummy">$dummy</a href><br>"
done


a=`sudo asterisk -rx 'sip show peers' | grep 'ms)' | awk -F "/" '{printf $1"\n";}'`
for dummy in $a
do
   echo "<a href="http://n.n.n.n/cgi-bin/call.sh?sip/$dummy">$dummy</a href><br>"
done


# chmod +x /var/www/cgi-bin/index.cgi

# nano /var/www/cgi-bin/call.sh (NO CUSTOMIZATION NEEDED!)


#!/bin/bash
echo "Content-type: text/plain"
echo ""
echo "executed";


caller=`cat /tmp/peers | grep -w $REMOTE_ADDR | cut -d' ' -f2`
tech=`cat /tmp/peers | grep -w $REMOTE_ADDR | cut -d' ' -f1`


echo "Channel: $tech/$caller" >/tmp/do.call
echo "Application: Dial" >>/tmp/do.call
echo "Data: $1" >>/tmp/do.call
sudo cp /tmp/do.call /var/spool/asterisk/outgoing

# chmod +x /var/www/cgi-bin/call.sh

# sed -i 's/n.n.n.n/[YOUR_SERVER_IP]/' /var/www/cgi-bin/index.cgi


That's all. You can now navigate to this page:


http://[YOUR_SERVER_IP]/cgi-bin/index.cgi


And CLICK TO DIAL any sip or iax user currently registered to your machine. Your softphone will ring: answer the call and get automatically connected to the requested user!



mercoledì 11 giugno 2014

Dynamically update externip without Dyndns.org or similar

About a month ago, Dyndns.org shut down his free dynamic dns service, disrupting service to hundreds of thousands of home server installations, shame on them!

Many Asterisk setups relied on this to overcome nat problems with dynamically assigned WAN ips (using the externhost=xyz.dyndns.org directive)

Here's a tiny shell script to automatically update your externip directive from sip.conf reflecting your actual WAN ip address:

# nano /root/spinach.sh

#!/bin/bash
doreload=0
wanip=`curl -q tnx.nl/ip`
if [ -f /tmp/oldip.txt ]; then
   oldip=`cat /tmp/oldip.txt`
   [ "$wanip" != "$oldip" ] && doreload=1
else
   doreload=1
fi
echo $wanip > /tmp/oldip.txt
if [ $doreload -eq 1 ]; then
      sed -i 's/externip=.*/externip='"$wanip"'/' /etc/asterisk/sip.conf
      asterisk -rx 'sip reload'
fi

# crontab -e

* * * * * /root/./spinach.sh

From now on, your sip.conf externip=* directive will be checked every minute.

Note: reloading Sip should not drop any outgoing or incoming call!