Advanced, HowTo
Run common asterisk commands without switching to the root user
To run an asterisk command, you need to connect to the asterisk server (as root) and type your command. For example:
asterisk -r
Now, if you were already logged in as root, you could do this in one step:
asterisk -rx "dialplan reload"
Or if you were a regular user:
sudo /usr/sbin/asterisk -rx "dialplan reload"
But, the big problem here is that you need to be ‘root’ to run these commands. I don’t think it’s a bad thing to simply login as the root user (with great power comes great responsibility), but if want an asterisk command to be executed by a webserver (for example, as part of a monitoring script), then sudo’ing to root is not possible.
What we’ll do is create a “Set UID” executable that can only run a fixed command. “Set UID” means that the executable will run with the same permissions as owner of the executable.
To do this, we need to write a small C program which will run our common asterisk command. Create the following file:
#include#include #include int main (int argc, char *argv[]) { argv[0] = "/usr/sbin/asterisk"; argv[1] = "-rx"; argv[2] = "dialplan reload"; execv(*argv, argv); }
Now, compile the file.
gcc dialplan-reload.c -o dialplan-reload
You will now have an executible that will reload the dialplan. It needs to be set so that it will run as root.
sudo chown root dialplan-reload sudo chmod u+s dialplan-reload
Now, if you give your regular login write access to your dialplan, you can edit it without having to ’sudo’ or login as the root user.
Another useful executable views the status of all the SIP peers. Compile and set the permissions the same as the dialplan-reload command.
#include#include #include int main (int argc, char *argv[]) { argv[0] = "/usr/sbin/asterisk"; argv[1] = "-rx"; argv[2] = "sip show peers"; execv(*argv, argv); }
This executable will be used to help create a simple PBX monitoring tool that I’ll discuss in a future post.
how can i start asterisk and how i stop and how i restart the asterisk plz tell me
To start asterisk (you need to be root):
sudo /usr/sbin/asterisk
To stop:
sudo /usr/sbin/asterisk -rx “shutdown now”