How to return output of Asterisk CGI commands?

Hi,

I am trying to run a php scrip trough asterisk when somebody dials a number.

I need the output of the asterisk cgi command sip show peers for the variable peers in my .php script.

This is what I have tried:
exten => s,n,system(/pbxware/bin/php -q /php/foo.php peers=${asterisk -rx “sip show peers”})

However, it does not return the output of the command.

Is there another way to achieve this?

Kind regards,
Brandon

Asterisk variables are parsed before the command is looked up, so I imagine that it is failing to find the variable and substituting it as an empty string.

You may be able to avoid this by using \ on the $ and/or the {}, but it would be easier to use a shell script to hide the details.

Also, I don’t believe system provides any access to standard output. For that, there is a function, whose name I forget.

Why do you actually need to do this? Using asterisk -r is quite expensive, so I hope you don’t do it for every single call.

Thank you for your response.

No, I do not wish do this for every single call.

I built a php script, which changes the active user-profile of my phone when somebody calls a specific number and specifies an extension.

But if the new profile of phone 1 is still active in phone 2, I also need to change the profile of phone 2.

Example:
Phone 1 has Profile_ABC
Phone 2 has Profile_XYZ

I change the profile of Phone 1 to Profile_XYZ. It is still active on Phone 2. This is why I need the IP of the original Phone of a profile (which is Phone 2 here).

I am using sip show peers to get a list of all active profiles and their current IP so I can connect to the current IP of Phone 2 and remove profile_XYZ.

Using \ didn’t help.

You said there is a function for standard output.
Did you mean the SHELL function?
voip-info.org/wiki/view/Asterisk+func+shell
We are unable to use this function as we are still on asterisk 1.4.

Bump

The forum rules say you must wait 24 hours before bumping.

If ${SIPPEER()} also doesn’t exist on 1.4 (core show function SIPPEER), then you will have to use AGI. It does appear to exist on 1.4.

[quote=“david55”]The forum rules say you must wait 24 hours before bumping.

If ${SIPPEER()} also doesn’t exist on 1.4 (core show function SIPPEER), then you will have to use AGI. It does appear to exist on 1.4.[/quote]

SIPPEER seems to exist in 1.4

I have managed to get it to work using a fixed extension.

exten => _*77XXX,1,System(/pbxware/bin/php -q /php/change-profile.php extension=${EXTEN} ip=${CUT(CUT(SIP_HEADER(Via), ,2),:,1)} peer=${SIPPEER(714:ip)})

As you can see, I’m using the fixed extension 714 here.
But I need to make this dinymac. (Such as $EXTEN)

Use a variable.

This is what I’m having problems with. Could you show me an example of the syntax when using a variable inside SIPPEER?
(Preferably $EXTEN)

Same as anywhere else.

Note that it is bad practice to make device names and extension names the same.

Wow. :neutral_face:
Sorry I am new to asterisk.

Whatever I try, the SIPPEER part just returns null every time. As you can see my EXTEN variable returns *77714.

This is the problem, it should return 714 only.

That’s why I’m now trying to use the CUT function to make a new variable out of EXTEN and remove the unneeded parts.

exten => _*77XXX,1,SET(callerid=${CUT(EXTEN,4-6)})
exten => _*77XXX,2,System(/pbxware/bin/php -q /php/change-profile.php extension=${EXTEN} ip=${CUT(CUT(SIP_HEADER(Via), ,2),:,1)} peer=${SIPPEER(callerid:ip)})

But I can’t get it to work. :frowning:

| not : (some fonts show a break in the bar - it produces neater vertical lines, given there will be a gap between lines).

${callerid} when referencing the value.

There is an easier way of substring extensions (and functions), see the sample extensions.conf, the channelvariables.tex file, or the relevant chapter in Asterisk: The Future of Telephony

Thanks for your help.

My final resolution:

exten => _*77XXX,1,SET(callerid=${EXTEN:3:3})
exten => _*77XXX,2,System(/pbxware/bin/php -q /php/change-profile.php extension=${callerid} ip=${CUT(CUT(SIP_HEADER(Via), ,2),:,1)} peer=${SIPPEER(${callerid}:ip)})

[quote=“david55”]Same as anywhere else.

Note that it is bad practice to make device names and extension names the same.[/quote]

David could you explain why it is bad practice to make device names and extension names the same.?

Because it makes it fairly easy to guess a valid name, and even easier to guess subsequent ones.