site Search:


 
    All Forums Hot Topics Gallery






how-to block ads


 
Search Topic:
Uniqs:
1171
Share Topic
Posting?
Post a:
Post a:
Links: ·ALL ·Review Your VoIP Provider ·VoIP Providers ·VoIP FAQ ·Porting Rules ·What Codec?
page: 1 · 2
AuthorAll Replies

dangmoo

join:2012-02-09

4 edits

[Asterisk] hangup a channel from bash - odd behavior

As part of my plan, I need to hangup a channel connected to a specific extension from bash (on a Debian6 machine). As you know this could be done many times like this for example:

CHANNEL=`asterisk -rx "core show channels" | grep 124 | cut -f1 -d" "`; asterisk -rx "channel request hangup $CHANNEL"
 

however "core show channels" has a length limit for channel name, and when it's long, only partial string is returned - which of course is not enough for the hangup request.

Here comes the command ""core show channels concise" which always provides the channel name in full, followed by '!' and some extra data in a long string.

The problem: it seems impossible to use that channel name after a substring. That's what I tried:

CHANNEL=`asterisk -rx "core show channels concise" | grep 124 | cut -f1 -d'!'`; asterisk -rx "channel request hangup $CHANNEL"
 

or:

CHANNEL=`asterisk -rx "core show channels concise" | grep 124 | awk -F! '$0=$1'`; asterisk -rx "channel request hangup $CHANNEL"
 

Although in both cases the $CHANNEL variable seems to hold a perfectly valid channel name - Asterisk refuses to hang it up and outputs "unknown channel".

Hope someone can help with this mystery..
Why the last 2 examples are not working, and what makes them different from the (working) example at the beginning??

Dan_voip

join:2007-01-03
Saint-Hubert, QC
kudos:3
Reviews:
·TekSavvy DSL

You can try to run parts of the command to see if it's working well. Another thing instead of the 2nd command you can put echo $CHANNEL to see if you get the correct string in CHANNEL.
When I've added the 2nd grep I didn't get anything, not sure what 124 means for your Asterisk.


dangmoo

join:2012-02-09

2 edits

said by Dan_voip:

When I've added the 2nd grep I didn't get anything, not sure what 124 means for your Asterisk.

What did you expect to get? that was just an example, as mentioned.
124 could be any extension. ('grep SIP/1001' is not really essential for the example so I've removed it).

Of course I've tested my codes and everything OK - except the last command which should hangup the channel from the var

Dan_voip

join:2007-01-03
Saint-Hubert, QC
kudos:3

For me it's working the following command:

CHANNEL=`asterisk -rx "core show channels concise" | grep 124 | grep outbound | cut -f1 -d'!'`; asterisk -rx "channel request hangup $CHANNEL"
 

SCADAGeo

join:2012-11-08
N California

reply to dangmoo

said by dangmoo:

Although in both cases the $CHANNEL variable seems to hold a perfectly valid channel name - Asterisk refuses to hang it up and outputs "unknown channel".

I could see this occurring if ext124 called ext125... there would be too many arguments passed to "channel request hangup" (ext124 would be in two lines).

Does this work on your system?

#!/bin/bash
 
if [ $# != 1 ]
then
   echo "   Usage: $0 extension"
   exit
fi
echo;
 
CHANNEL=`asterisk -rx "core show channels concise" | cut -f1 -d'!' | grep $1`
 
for i in ${CHANNEL}
do
   echo -n "Request hangup on channel ${i} "
   read -p "(y/n)? " ans
   if [ ${ans} == "y" ]
   then
      echo "Requesting hangup on ${i}..."
      asterisk -rx "channel request hangup ${i}"
   fi
done
 
echo;
 

dangmoo

join:2012-02-09

1 edit

.


dangmoo

join:2012-02-09

reply to SCADAGeo

Thank you SCADAGeo but this is not working.
FYI I had to tweak the $CHANNEL var declaration in your code to make more sense, as follow:
CHANNEL=`asterisk -rx "core show channels concise" | grep $1 | cut -f1 -d'!'`
 
but anyway I'm still getting the same problem: although $CHANNEL holds the right value, Asterisk keeps refusing the hangup request

Here's how it looks like on bash shell:

root@codeplace:~# asterisk -rx "core show channels concise"
SIP/1001-00000022!default!124!7!Ring!Playback!holdmusic,noanswer!1001!!!3!17!(None)!1355355807.34
 
root@codeplace:~# ./hangup.sh 124
 
Request hangup on channel SIP/1001-00000022 (y/n)? y
Requesting hangup on SIP/1001-00000022...
SIP/1001-00000022 is not a known channel
 
Any other idea??

SCADAGeo

join:2012-11-08
N California

said by dangmoo:

SIP/1001-00000022 is not a known channel
 
Any other idea??

That's really strange. I'm running it under Centos 5.7, GNU bash version 3.2.25(1)-release (i686-redhat-linux-gnu).

Does this work from the command line:

asterisk -rx "channel request hangup SIP/1001-00000022"
 

If it does, I would try 'exec'

exec asterisk -rx "channel request hangup ${i}"
 

to see what happens.

BTW - there is a note in Asterisk 10.0.1 cli.c, that the 'concise' option is deprecated.

dangmoo

join:2012-02-09

when I hangup like this

asterisk -rx "channel request hangup SIP/1001-00000022"
 
it has always been working... but the whole issue is about the need to do it programatically, from a variable.

When I change your script to include the 'exec' , I'm still getting the same 'unknown channel' problem.

No luck with this yet...

I wonder what could be a different approach for my goal. All I need is hangup channel which is connected to specific extension. Is there another command whereby I can extract this data (full channel name + extension)??


Trev
IP Telephony Addict
Premium
join:2009-06-29
Victoria, BC
kudos:4

Check out AMI - Asterisk Manager Interface.

You won't get away with a one-liner, but you will get a significantly more reliable way of gathering call details and manipulating calls.
--
Wondering what I do? Find out at »www.digitalcon.ca
Get your Obihai ATA in Canada.


dangmoo

join:2012-02-09

said by Trev:

Check out AMI - Asterisk Manager Interface.

Thanks, but since I'm planning to eventually use such hangup request as a simple dialplan priority, using System() or Shell() , I wouldn't go that far for such a simple task.



Trev
IP Telephony Addict
Premium
join:2009-06-29
Victoria, BC
kudos:4

reply to Trev
In that case perhaps this would work for you:

- when a call to the extensions that may need hanging up come in, set a global variable with that extension number to the channel name
- create an extension that's sole purpose is to Hangup
- use the ChannelRedirect application to redirect that channel to the Hangup extension
--
Wondering what I do? Find out at »www.digitalcon.ca
Get your Obihai ATA in Canada.


SCADAGeo

join:2012-11-08
N California

said by Trev:

In that case perhaps this would work for you:

- when a call to the extensions that may need hanging up come in, set a global variable with that extension number to the channel name
- create an extension that's sole purpose is to Hangup
- use the ChannelRedirect application to redirect that channel to the Hangup extension

Nice way to work around the problem.

Debian 6 has a newer version of bash, so I wondered if it was an expansion quirk in the new version.

I downloaded v4.2.0, compiled, called it normally and with --posix.

Both methods worked... not sure where to go from here...

Just out of curiosity, are you running Debian 6.0.6?

What do the following show?

whereis asterisk

asterisk -rx "core show version"

grep ^`whoami` /etc/passwd

echo $SHELL

/bin/bash --version

SCADAGeo

join:2012-11-08
N California

reply to dangmoo
If you are on a test system, what happens when you substitute 'all' for ${i} in the script?

     asterisk -rx "channel request hangup all"
 

dangmoo

join:2012-02-09

2 edits

said by SCADAGeo:

If you are on a test system, what happens when you substitute 'all' for ${i} in the script?

It worked normally when using the command without variables. Problem occurred only when a variable was involved

dangmoo

join:2012-02-09

1 edit

reply to Trev

said by Trev:

In that case perhaps this would work for you:

- when a call to the extensions that may need hanging up come in, set a global variable with that extension number to the channel name
- create an extension that's sole purpose is to Hangup
- use the ChannelRedirect application to redirect that channel to the Hangup extension

Thank you Trev, based on your tip the problem has been solved. Bravo.

A priority was added to the target extension like this:
same => n,Set(GLOBAL(CHANTOKILL)=${CHANNEL})
 

and by adding this line to any other extension, call is hangup:
same => n,Set(result=${SHELL(asterisk -rx "channel request hangup ${CHANTOKILL}")})
same => n,NoOp(result is: ${result})
 

However, I'm still curious why the original code didn't work for me. This will remain an open question until someone solves this mystery

SCADAGeo

join:2012-11-08
N California

said by dangmoo:

said by Trev:

In that case perhaps this would work for you:

Thank you Trev, based on your tip the problem has been solved. Bravo.

Cool!

said by dangmoo:

It worked normally when using the command without variables. Problem occurred only when a variable was involved

I understood.

said by dangmoo:

However, I'm still curious why the original code didn't work for me. This will remain an open question until someone solves this mystery

I am curious, too, that's why I asked for the information.

I want to recreate your environment, but the full distro of Debian 6.0.6 contains 7 DVD's... it would be a waste of bandwidth and time to download a version that is different than the one you are using.

dangmoo

join:2012-02-09

1 edit

Sure SCADAGeo, sorry for delay.
I'm on a Debian Squeeze 32bit VPS, connected via SSH. Yes, it's the version 6.0.6 .
Hope the following info helps:

root@codeplace:~# cat /etc/debian_version
6.0.6
root@codeplace:~# whereis asterisk
asterisk: /usr/sbin/asterisk /etc/asterisk /usr/lib/asterisk /usr/include/asterisk /usr/include/asterisk.h /usr/share/man/man8/asterisk.8
root@codeplace:~# asterisk -rx "core show version"
Asterisk 1.8.3.3 built by root @ codeplace on a i686 running Linux on 2012-12-09 21:30:06 UTC
root@codeplace:~# grep ^`whoami` /etc/passwd
root:x:0:0:root:/root:/bin/bash
root@codeplace:~# echo $SHELL
/bin/bash
root@codeplace:~# /bin/bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 

dangmoo

join:2012-02-09

reply to dangmoo

OK.
I've just solved the problem,
In short, what happened was that 7 hidden characters were appended just before the beginning of the $CHANNEL value.
I checked the string-lenght of $CHANNEL and to my surprise it showed 24, while only 17 were visible:

root@codeplace:~# asterisk -rx "core show channels concise"
SIP/1001-00000175!default!124!7!Ring!Playback!holdmusic,noanswer!1001!!!3!8!(None)!1355439097.373
root@codeplace:~# echo $CHANNEL
SIP/1001-00000175
root@codeplace:~# echo $CHANNEL | awk '{print length}'
24
 

If you wondered what were these hidden characters here it is (time info?):
root@codeplace:~# echo ${CHANNEL:0}
SIP/1001-00000175
root@codeplace:~# echo ${CHANNEL:1}
[0;37mSIP/1001-00000175
 

Anyway, for hanging it up from bash shell, $CHANNEL variable must started from its position number 7:

root@codeplace:~# CHANNEL=`asterisk -rx "core show channels concise" | grep 124 | cut -f1 -d'!'`; asterisk -rx "channel request hangup ${CHANNEL:7}"
Requested Hangup on channel 'SIP/1001-00000175'
 

Hope this help someone :-)


Trev
IP Telephony Addict
Premium
join:2009-06-29
Victoria, BC
kudos:4

Those nondisplaying characters are ANSI control codes. They give you the different colours on the shell.

From a security perspective, I would stay far far far away from a shell script like this that that runs as root. This is exactly the kind of thing attackers take advantage of to compromise systems.
--
Wondering what I do? Find out at »www.digitalcon.ca
Get your Obihai ATA in Canada.


Thursday, 23-May 10:05:53 Terms of Use & Privacy | feedback | contact | Hosting by nac.net - DSL,Hosting & Co-lo
over 13.5 years online © 1999-2013 dslreports.com.
Most commented news this week
Hot Topics