Running shell script from within the DialPlan

Hi Daniel,

I have tried TrySystem, makes no change… everything acts the same…

The reason for the double up is purely for testing permissions and location etc. Was not sure if I could run a shell script or a command directly.
was expecting either to fail or work, where I would at least know what I am missing.

Thanks

Paul

Hi,

Only the TrySystem can give you the ${SYSTEMSTATUS}.
I think that the problem is coming from your BASH script. Can you paste it here?

Thank you,

Daniel Friedman
Trixton LTD.

I updated the DialPlan, did a core reload and dialed the extensions:

[2015-11-25 12:10:01] NOTICE[25539]: pbx_spool.c:427 attempt_thread: Call completed to Local/[email protected]
== Spawn extension (tc-maint, s, 7) exited non-zero on ‘Local/[email protected];2’
== Using SIP RTP TOS bits 184
== Using SIP RTP CoS mark 5
– Executing [[email protected]:1] Wait(“SIP/210-00000094”, “1”) in new stack
– Executing [[email protected]:2] TrySystem(“SIP/210-00000094”, “/home/asterisk/on.sh”) in new stack
– Executing [[email protected]:3] TrySystem(“SIP/210-00000094”, “/usr/local/bin/tdtool --on 1”) in new stack
– Executing [[email protected]:4] Verbose(“SIP/210-00000094”, “SUCCESS”) in new stack
SUCCESS
– Executing [[email protected]:5] System(“SIP/210-00000094”, “whoami > /home/asterisk/textfile”) in new stack
– Executing [[email protected]:6] Verbose(“SIP/210-00000094”, “SUCCESS”) in new stack
SUCCESS
– Executing [[email protected]:7] Hangup(“SIP/210-00000094”, “”) in new stack
== Spawn extension (from-internal, 111, 7) exited non-zero on ‘SIP/210-00000094’
– Executing [[email protected]:1] Hangup(“SIP/210-00000094”, “”) in new stack
== Spawn extension (from-internal, h, 1) exited non-zero on ‘SIP/210-00000094’
== Using SIP RTP TOS bits 184
== Using SIP RTP CoS mark 5
– Executing [[email protected]:1] Wait(“SIP/210-00000095”, “1”) in new stack
– Executing [[email protected]:2] TrySystem(“SIP/210-00000095”, “/home/asterisk/off.sh”) in new stack
– Executing [[email protected]:3] TrySystem(“SIP/210-00000095”, “/usr/local/bin/tdtool --off 1”) in new stack
– Executing [[email protected]:4] Hangup(“SIP/210-00000095”, “”) in new stack
== Spawn extension (from-internal, 112, 4) exited non-zero on ‘SIP/210-00000095’
– Executing [[email protected]:1] Hangup(“SIP/210-00000095”, “”) in new stack
== Spawn extension (from-internal, h, 1) exited non-zero on ‘SIP/210-00000095’

Thanks

Paul

Shell script:

[[email protected] asterisk]# cat on.sh
#!/bin/bash
#/bin/stty -F /dev/ttyUSB0 4800 -parity cs8 -cstopb
#/bin/echo 1 > /dev/ttyUSB0
/usr/local/bin/tdtool --on 1

Line 2 and 3 are from a previous attempt to write directly to a serial device.
again this worked by CLI but not from the dialplan

Thanks

Paul

Hi,

You need to set the PATH environment in your script, like that:

#Set the PATH environment
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin/

Add it beneath the first line of your script (#!/bin/bash)

Thank you,

Daniel Friedman
Trixton LTD.

Updated script and no change… :confused:
I did think of the app or script not in the path, which is why I used the complete path in the command.

This has to be something silly that has been overlooked…

Thanks Paul

Hi,

What is your command (/usr/local/bin/tdtool --on 1) suppose to do? What is the output?
Do not forget that you are running it under the asterisk user.

Thank you,

Daniel Friedman
Trixton LTD.

Hi,
The tdtool command sends a command to a USB device that initiates operation of a remote switch. The application and hardware are matched.

If I uncomment lines 2 and 3, I can place another USB device in the system , I can communicate directly via serial to an Arduino.

I thought about user permissions…
When I issue ‘su asterisk’ and launch the shell scripts they work as user asterisk and everything works fine from the CLI.

Thanks

Paul

Hi,

can you paste here the result of ls -la /home/asterisk/on.sh and ls -la /usr/local/bin/tdtool ?

Thank you,

Daniel Friedman
Trixton LTD.

as requested…

bash-4.1$ ls -la /home/asterisk/on.sh -rwx------ 1 asterisk asterisk 184 Nov 25 12:35 /home/asterisk/on.sh bash-4.1$ ls -la /usr/local/bin/tdtool -rwxr-xr-x 1 root root 14332 Nov 23 22:32 /usr/local/bin/tdtool bash-4.1$

Hi,

Change the permissions of your script to 755 like that:

chmod 755 /home/asterisk/on.sh

Thank you,

Daniel Friedman
Trixton LTD.

Done:

bash-4.1$ ls -la *.sh -rwxr-xr-x 1 asterisk asterisk 174 Nov 25 12:37 off.sh -rwxr-xr-x 1 asterisk asterisk 184 Nov 25 12:35 on.sh bash-4.1$

no change…

it is almost like Asterisk is prevented from actually running anything…
But Asterisk will write to a file so there is some I/O.

I see posts where people have had issues, but no resolution.
Indeed there was one with the same title as this post from 2013:

The System and TrySystem command, while documented, do really work as described?

Thanks

Paul

Hi,

Yes it works great. I use it all the time. We will need serious debugging on this. paste your asterisk log (not the console) when you run it (tail -f /var/log/asterisk/full).

Thank you,

Daniel Friedman
Trixton LTD.

1 Like

Hi,

I called the exten then ran the command, when it did not show both calls, I did it again while the log was being followed:

[[email protected] asterisk]# [[email protected] asterisk]# tail -f /var/log/asterisk/full [2015-11-25 13:52:10] VERBOSE[28779][C-000014ae] pbx.c: == Spawn extension (from-internal, h, 1) exited non-zero on 'SIP/210-000000a2' [2015-11-25 13:52:13] VERBOSE[1898][C-000014af] netsock2.c: == Using SIP RTP TOS bits 184 [2015-11-25 13:52:13] VERBOSE[1898][C-000014af] netsock2.c: == Using SIP RTP CoS mark 5 [2015-11-25 13:52:13] VERBOSE[28786][C-000014af] pbx.c: -- Executing [[email protected]:1] Wait("SIP/210-000000a3", "1") in new stack [2015-11-25 13:52:14] VERBOSE[28786][C-000014af] pbx.c: -- Executing [[email protected]:2] TrySystem("SIP/210-000000a3", "/home/asterisk/off.sh") in new stack [2015-11-25 13:52:14] VERBOSE[28786][C-000014af] pbx.c: -- Executing [[email protected]:3] TrySystem("SIP/210-000000a3", "/usr/local/bin/tdtool --off 1") in new stack [2015-11-25 13:52:14] VERBOSE[28786][C-000014af] pbx.c: -- Executing [[email protected]:4] Hangup("SIP/210-000000a3", "") in new stack [2015-11-25 13:52:14] VERBOSE[28786][C-000014af] pbx.c: == Spawn extension (from-internal, 112, 4) exited non-zero on 'SIP/210-000000a3' [2015-11-25 13:52:14] VERBOSE[28786][C-000014af] pbx.c: -- Executing [[email protected]:1] Hangup("SIP/210-000000a3", "") in new stack [2015-11-25 13:52:14] VERBOSE[28786][C-000014af] pbx.c: == Spawn extension (from-internal, h, 1) exited non-zero on 'SIP/210-000000a3' [2015-11-25 13:53:02] VERBOSE[1898][C-000014b0] netsock2.c: == Using SIP RTP TOS bits 184 [2015-11-25 13:53:02] VERBOSE[1898][C-000014b0] netsock2.c: == Using SIP RTP CoS mark 5 [2015-11-25 13:53:02] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:1] Wait("SIP/210-000000a4", "1") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:2] TrySystem("SIP/210-000000a4", "/home/asterisk/on.sh") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:3] TrySystem("SIP/210-000000a4", "/usr/local/bin/tdtool --on 1") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:4] Verbose("SIP/210-000000a4", "SUCCESS") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] app_verbose.c: SUCCESS [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:5] System("SIP/210-000000a4", "whoami > /home/asterisk/textfile") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:6] Verbose("SIP/210-000000a4", "SUCCESS") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] app_verbose.c: SUCCESS [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:7] Hangup("SIP/210-000000a4", "") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: == Spawn extension (from-internal, 111, 7) exited non-zero on 'SIP/210-000000a4' [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: -- Executing [[email protected]:1] Hangup("SIP/210-000000a4", "") in new stack [2015-11-25 13:53:03] VERBOSE[28816][C-000014b0] pbx.c: == Spawn extension (from-internal, h, 1) exited non-zero on 'SIP/210-000000a4' [2015-11-25 13:53:07] VERBOSE[1898][C-000014b1] netsock2.c: == Using SIP RTP TOS bits 184 [2015-11-25 13:53:07] VERBOSE[1898][C-000014b1] netsock2.c: == Using SIP RTP CoS mark 5 [2015-11-25 13:53:07] VERBOSE[28823][C-000014b1] pbx.c: -- Executing [[email protected]:1] Wait("SIP/210-000000a5", "1") in new stack [2015-11-25 13:53:08] VERBOSE[28823][C-000014b1] pbx.c: -- Executing [[email protected]:2] TrySystem("SIP/210-000000a5", "/home/asterisk/off.sh") in new stack [2015-11-25 13:53:08] VERBOSE[28823][C-000014b1] pbx.c: -- Executing [[email protected]:3] TrySystem("SIP/210-000000a5", "/usr/local/bin/tdtool --off 1") in new stack [2015-11-25 13:53:08] VERBOSE[28823][C-000014b1] pbx.c: -- Executing [[email protected]:4] Hangup("SIP/210-000000a5", "") in new stack [2015-11-25 13:53:08] VERBOSE[28823][C-000014b1] pbx.c: == Spawn extension (from-internal, 112, 4) exited non-zero on 'SIP/210-000000a5' [2015-11-25 13:53:08] VERBOSE[28823][C-000014b1] pbx.c: -- Executing [[email protected]:1] Hangup("SIP/210-000000a5", "") in new stack [2015-11-25 13:53:08] VERBOSE[28823][C-000014b1] pbx.c: == Spawn extension (from-internal, h, 1) exited non-zero on 'SIP/210-000000a5'

This looks the same as the console to me.

Thanks

Paul

I added a line to the shell script:

touch this_is_on

this does not create the file.
The shell script is not being run.

Thanks

Paul

Hi,

The script is running, but you have permissions problems on the device itself to my opinion. Write a dial plan and integrate it with the sudoers file like that:

[from-internal-custom]
exten => 111,1,Answer()
same => n,TrySystem(/usr/local/bin/tdtool --on 1)
same => n,NoOp(Status of TrySystem is: ${SYSTEMSTATUS})
same => n,Hangup

After adding this to your extensions_custom.conf file please reload the Asterisk dial plan (asterisk -rx’dialplan reload’).

Do not forget to add these lines to the end of the sudoers file (visudo):

#Allow asterisk user to run tdtool command
Cmnd_Alias TDTOOL = /usr/local/bin/tdtool
asterisk        ALL = NOPASSWD: TDTOOL

Thank you,

Daniel Friedman
Trixton LTD.

1 Like

Hi Daniel,

I made the changes you suggested and tried both with and without sudo…

[2015-11-25 14:39:13] VERBOSE[1898][C-000014cd] netsock2.c: == Using SIP RTP TOS bits 184 [2015-11-25 14:39:13] VERBOSE[1898][C-000014cd] netsock2.c: == Using SIP RTP CoS mark 5 [2015-11-25 14:39:13] VERBOSE[30319][C-000014cd] pbx.c: -- Executing [[email protected]:1] Answer("SIP/210-000000b0", "") in new stack [2015-11-25 14:39:13] VERBOSE[30319][C-000014cd] pbx.c: -- Executing [[email protected]:2] TrySystem("SIP/210-000000b0", "sudo /usr/local/bin/tdtool --on 1") in new stack [2015-11-25 14:39:14] VERBOSE[30319][C-000014cd] pbx.c: -- Executing [[email protected]:3] NoOp("SIP/210-000000b0", "Status of TrySystem is: SUCCESS") in new stack [2015-11-25 14:39:14] VERBOSE[30319][C-000014cd] pbx.c: -- Executing [[email protected]:4] Hangup("SIP/210-000000b0", "") in new stack [2015-11-25 14:39:14] VERBOSE[30319][C-000014cd] pbx.c: == Spawn extension (from-internal, 111, 4) exited non-zero on 'SIP/210-000000b0' [2015-11-25 14:39:14] VERBOSE[30319][C-000014cd] pbx.c: -- Executing [[email protected]:1] Hangup("SIP/210-000000b0", "") in new stack [2015-11-25 14:39:14] VERBOSE[30319][C-000014cd] pbx.c: == Spawn extension (from-internal, h, 1) exited non-zero on 'SIP/210-000000b0' [2015-11-25 14:39:17] VERBOSE[1898][C-000014ce] netsock2.c: == Using SIP RTP TOS bits 184 [2015-11-25 14:39:17] VERBOSE[1898][C-000014ce] netsock2.c: == Using SIP RTP CoS mark 5 [2015-11-25 14:39:17] VERBOSE[30322][C-000014ce] pbx.c: -- Executing [[email protected]:1] Answer("SIP/210-000000b1", "") in new stack [2015-11-25 14:39:17] VERBOSE[30322][C-000014ce] pbx.c: -- Executing [[email protected]:2] TrySystem("SIP/210-000000b1", "sudo /home/asterisk/off.sh") in new stack [2015-11-25 14:39:17] VERBOSE[30322][C-000014ce] pbx.c: -- Executing [[email protected]:3] Hangup("SIP/210-000000b1", "") in new stack [2015-11-25 14:39:17] VERBOSE[30322][C-000014ce] pbx.c: == Spawn extension (from-internal, 112, 3) exited non-zero on 'SIP/210-000000b1' [2015-11-25 14:39:17] VERBOSE[30322][C-000014ce] pbx.c: -- Executing [[email protected]:1] Hangup("SIP/210-000000b1", "") in new stack [2015-11-25 14:39:17] VERBOSE[30322][C-000014ce] pbx.c: == Spawn extension (from-internal, h, 1) exited non-zero on 'SIP/210-000000b1'

it still is not functioning.

Is there something else we can try, not using my scripts?

Thanks

Paul

I didn’t change the off.sh line… my mistake, it was locked on and would not turn off

It now works. Thank you.

So, it was a permissions issue but not something obvious.
I will now apply that to my original plan with the serial and arduino…

Hope this helps someone else :smile:
yay

1 Like

Hi,

I am glad that it is working for you.

Can you elaborate what was locked exactly?

Thank you,

Daniel Friedman
Trixton LTD.

Hi,

The output was in the on state.
When I dialed 112 it would not turn off as the dialplan was still pointing to the shell script.
So dialing 111 would not turn the output on as it was already on.

When I changed state manually via CLI, and tried again, I was able to turn the output on when I dialed 111.
That is when I realised I only changed the the 111 extension and not the 112.

Hope that makes sense

It was the last change you suggested that got it to function :smile:

Thanks

Paul