Asterisk: round-robin dial out through trunks

March 27th, 2014

Imagine that we have 3 trunks for dial out and we must balance outgoing calls in random order. This could be done with the RAND function. Module func_rand.so must be compiled and loaded.

sip.conf:

[trunk1](trunk_preset)
host=10.10.10.11

[trunk2](trunk_preset)
host=10.10.10.12

[trunk3](trunk_preset)
host=10.10.10.13

extensions.conf:

exten => _79XXXXXXXXX,1,NoCDR()
    same => n,Dial(SIP/trunk${RAND(1,3)}/${EXTEN})
    same => n(hang),Hangup()

You may say – well, OK, but what if my trunks are named not like [trunk1], [trunk2], [trunkN] and I can not substitute the last symbol in Dial application?
In this case we can write a list of trunk names (which you configured already in sip.conf) in txt file, for example rrtrunks.txt:

teliasonera-out
deutschetelekom-out
level3-out

Ensure that a ‘sort’ command is available in your *NIX system. Usually it’s a part of coreutils. Older versions don’t suit us, because they don’t have the ‘-R’ flag which means sort randomly. Newer versions have.

Then load func_shell.so and use it in your dialplan. It can execute a shell command and use its output in dialplan.

exten => _79XXXXXXXXX,1,NoCDR()
    same => n,Dial(SIP/${SHELL(sort -R /etc/asterisk/rrtrunks.txt | head -1)}/${EXTEN},20)
    same => n,Hangup()

It means that we sort randomly our list of trunks and get the first one each time.

Get rid of LibreOffice slow start

March 26th, 2014

Assuming your hostname is darkstar, add these lines to your /etc/hosts:

# fix libreoffice slow start
127.0.0.1 darkstar.(none)

Be sure that your hostname is the same both in /etc/hostname and in /etc/hosts .

Thanks for www.nixp.ru.

Asterisk 12 Notes

March 24th, 2014

To use ‘Gosub‘ (and also ‘Return‘, ‘GosubIf‘ and ‘StackPop‘) application you must load app_stack.so .
To make calling between endpoints possible, you must load bridge_simple.so .

To originate calls from CLI you must load res_clioriginate.so (assuming app_originate.so is already loaded).

Using ‘screen’ for multiple SSH sessions from a remote computer

March 23rd, 2014

1. Log in to the remote server.
2. Type 'screen'. The virtual screen #0 will be opened. We’ll use it as a starting point for creating other screens.
3. Type 'screen -t server1'. This command will open one more virtual screen with title ‘server1’, we will use it for entering the server1, for example ‘ssh server1’.
4. Press Control-a and then ” (double inverted commas)
This command will list all your virtual screens. You may select any of them moving with up/down arrows and then pressing enter. Enter the screen #0.
5. From screen #0 we can create one more screen, for example 'screen -t server2'.
6. Jumping from one screen to some other can be done with Ctrl-a and then ” or Ctrl-a and the number of screen. In our case – 0, 1 and 2.
7. You may close any screen with Ctrl-D or 'exit', like usual.

Another scenario:

1. screen -S 1st_session_name to start screen with some session name.
2. Ctrl-a d to detach from session.
3. screen -ls to list all sessions and their pids.
4. screen -r %pid% to reconnect to a session with %pid%.

find

March 6th, 2014

Find files older than 15 days and list them:

[root@staff /root]# find /var/squid/logs/ -mtime +15 -execdir ls -lh {} \;
-rw-r-----  1 squid  squid    91k Feb 16 20:46 /var/squid/logs/cache.log.17
-rw-r-----  1 squid  squid   108M Feb 17 00:00 /var/squid/logs/access.log.17
-rw-r-----  1 squid  squid   122M Feb 16 00:00 /var/squid/logs/access.log.18
-rw-r-----  1 squid  squid   355M Feb 19 00:00 /var/squid/logs/access.log.15
-rw-r-----  1 squid  squid   137k Feb 18 23:55 /var/squid/logs/cache.log.15
-rw-r-----  1 squid  squid   275M Feb 18 00:00 /var/squid/logs/access.log.16
-rw-r-----  1 squid  squid   145k Feb 17 21:49 /var/squid/logs/cache.log.16

List files modified during last 5 minutes:

find /some/path/ -type f -mmin -5 -execdir ls -l {} \;

Find files bigger than 2 kilobytes in .mc/ directory:

find .mc/ -type f -size +2000c
.mc/ini

or:

find .mc/ -type f -size +2k
.mc/ini

Find files smaller than 80 bytes in .mc/ directory:

vn ~ # find .mc/ -type f -size -80c
.mc/Tree
.mc/cedit/cooledit.clip

Handy one-liner for searching a file with wildcard, case-insensitive, in 2 directories simultaneously (assuming the file contains ‘Yutong’):

srv ~ # find /storage/sounds/{cars,ru/cars} -iname yut\*
/storage/sounds/cars/Yutong.wav
/storage/sounds/ru/cars/Yutong.wav

Find files with “+380” in their name which were created on October 13, 2015 and copy them to /home/lexus/:

find /storage/cluster/ready/ -name "*+380*" -type f -newermt 2015-10-13 ! -newermt 2015-10-14 -execdir cp {} /home/lexus/ \;

WordPress: disabling quotes convertion

February 16th, 2014

You have to edit the file default-filters.php which is located here:
/your_WP_root_directory/wp-includes/default-filters.php

Comment out the line:
add_filter( 'the_content', 'wptexturize' );

Based on this topic: https://wordpress.org/support/topic/please-give-us-the-option-to-turn-of-smart-quotes .

awk

February 16th, 2014

Print the first line:
cat somefile.txt | awk ‘{print $1}’

… and see the number of active calls on Asterisk PBX:

asterisk -rx 'core show calls' | head -n 1 | awk '{print $1}'

Print all but first 6 columns of the file:
cat somefile.txt | awk ‘{$1=$2=$3=$4=$5=$6=””; print $0}’

… and see who’s trying to connect to 5060/TCP and 22/TCP (some fields reduced):

root@vds:~# tail -n 2 /var/log/syslog | awk '{$1=$2=$3=$4=$5=$6=""; print $0}'
      iptables denied Asterisk IN=eth0 SRC=69.60.119.204 DST=XX.XX.YY.ZZ PROTO=UDP SPT=5072 DPT=5060
      iptables denied SSH IN=eth0 SRC=222.186.62.39 DST=XX.XX.YY.ZZ PROTO=TCP SPT=6000 DPT=22

By default, awk doesn’t separate columns. To do that, use comma:

root@vds:~# grep Aster /var/log/syslog | awk '{print $1,$2,$3,$9,$13}'
Feb 17 07:03:25 Asterisk SRC=74.118.193.77
Feb 17 07:13:23 Asterisk SRC=85.25.194.185
Feb 17 07:56:31 Asterisk SRC=188.138.34.254
Feb 17 08:12:02 Asterisk SRC=200.12.49.147

PS: all these IPs are bad guys. They’re trying to connect to my SSH and SIP ports. So, I do not hide their addresses :)

Update:

In case you’ve got an output like

“123456”
“234567”
“345678”

and want to get rid of first and last symbols:

rev input_file.txt | cut -c2- | rev | cut -c2-

In case your columns have some other delimiters than space, p.e. comma, specify it:

cat file.txt | awk -F',' '{print $3}'

Debian/Ubuntu: set default applications (both X11 and console)

February 7th, 2014

To set the default web browser type:

sudo update-alternatives --config x-www-browser

This will lead to:

lexus@lexus:~$ sudo update-alternatives --config x-www-browser
There are 3 choices for the alternative x-www-browser (providing /usr/bin/x-www-browser).

  Selection    Path                    Priority   Status
------------------------------------------------------------
  0            /usr/bin/google-chrome   200       auto mode
* 1            /usr/bin/firefox         40        manual mode
  2            /usr/bin/google-chrome   200       manual mode
  3            /usr/bin/opera           200       manual mode

Press enter to keep the current choice[*], or type selection number: 

To set default editor:

update-alternatives --config editor

To edit (or leave with defaults) all variants, use:

sudo update-alternatives --all

Asterisk: block inbound call by CALLERID(num) (AEL syntax)

January 31st, 2014

One number :

    73522123456 =>
    {
        if(${CALLERID(num)} = 74995008119)
            {
            Hangup;
            }
        Dial(IAX2/iaxpeer/somedevice);
    }

Several numbers:

    73522123456 =>
    {
    switch (${CALLERID(num)}) {

         case 74957805170:
            NoOp(Block call from Susan);
            Hangup();
            break;

        pattern [78]4957805174:
            NoOp(Block call from Anna);
            Hangup();
            break;

        pattern [78]4957805061:
            NoOp(Block call from Lily );
            Hangup();
            break;

        pattern [78]495780508X:
            NoOp(Block call from Samantha);
            Hangup();
            break;

        default:
            Set(CDR(accountcode)=some-accountcode);
            Dial(SIP/somepeer/tomsphone01,,r);
        }
    }

pattern – here you may use constructions like [167] and/or 123X.
case – is exact sequence of symbols.

https://wiki.asterisk.org/wiki/display/AST/AEL+Conditionals

FreeSWITCH installation

January 17th, 2014

Download latest stable release (January, 2014) http://files.freeswitch.org/freeswitch-1.2.17.tar.bz2 to /usr/src/.
I have the basic Debian 7.3 Wheezy installation.
I needed these dependences: apt-get install zlib zlib-dev zlib1g-dev libjpeg62 libjpeg62-dev libjpeg8-dev libcurses-perl libncurses5-dev libssl-dev

Then:

./configure

make install
make cd-sounds-ru-install
make cd-moh-ru-install
make cd-moh-install

Symlinks:
ln -s /usr/local/freeswitch/bin/fs_cli /usr/local/bin/fs_cli
ln -s /usr/local/freeswitch/bin/freeswitch /usr/local/bin/freeswitch

Fire!:
fs_cli -l 5