How to run a script when an outbound REGISTER is rejected?


I’ve got a trunking with ITSP, that sometimes fails for an unkown reason after hours or days of normal operation.

For troubleshooting this, I want to run a script at the moment my first REGISTER is rejected by the ITSP.

What are the available means to do this and the most efficient one among those ?
Spying AMI events ?
Checking pjsip show registration output ?

Best regards

On Thursday 02 May 2024 at 18:13:38, oliv2831 via Asterisk Community wrote:

I want to run a script at the moment my first REGISTER is rejected by the

What do you want this script to do?

If it’s anything to do with traffic analysis (ie: looking at the packets netween
Asterisk and the ITSP, I think the latency of starting a script means you will
simply miss the interesting / useful part.

My approach would simply be to perform a full SIP packet capture (you can
restrict it to the IP address of your ITSP) and for example run this from
/etc/cron.daily so that you don’t build up an emormous capture file whilst
waiting for the problem to occur, but only ever get up to 24 hours’ worth.

Then, when you notice that the connection has failed, you can analyse with
wireshark / sngrep etc the packets both before and after the problem occurred.


I wasn’t sure about having a beard at first, but then it grew on me.

                                               Please reply to the list;
                                                     please *don't* CC me.

About 7 years ago, I created a script with a similar approach to the one you’re requesting. I keep it running in the background, and it sends me an email alert based on an array of particular events. It works flawlessly


// Establish connection to Asterisk Manager Interface
$socket = fsockopen($host, "5038", $errno, $errstr, 10);
if (!$socket) {
    echo "$errstr ($errno)\n";
} else {
    // Send login action to AMI
    fputs($socket, "Action: Login\r\n");
    fputs($socket, "UserName: $username\r\n");
    fputs($socket, "Secret: $secret\r\n\r\n");

    // Send WaitEvent action to AMI
    fputs($socket, "Action: WaitEvent\r\n");

    // Send Logoff action to AMI (to close the connection after receiving events)
    fputs($socket, "Action: Logoff\r\n\r\n");

    // Read AMI events
    while (!feof($socket)) {
        $result = fread($socket, 5000);
        $result .= date("Y-m-d H:i:s");
        $events = ["SoftHangupRequest", "Newchannel", "ChallengeResponseFailed", "InvalidPassword", "InvalidAccountID", "Hold", "Unhold"];

        // Check for specific events and take appropriate actions
        foreach ($events as $value) {
            if (preg_match("/\b$value\b/i", $result, $match)) {
                echo "$result\n";

                if ($value === "SoftHangupRequest") {
                    echo "$result\n";
                    mail("", "Soft Hangup Request Event", $result);

                if ($value === "Newchannel") {
                    echo "$result\n";
                    mail("", "New Channel Event", $result);

                if ($value === "ChallengeResponseFailed") {
                    echo "$result\n";
                    mail("", "Challenge Response Failed Event", $result);

                if ($value === "InvalidPassword") {
                    echo "$result\n";
                    mail("", "Invalid Password Event", $result);

                if ($value === "InvalidAccountID") {
                    echo "$result\n";
                    mail("", "Invalid Account ID Event", $result);

                if ($value === "Hold") {
                    echo "$result\n";
                    mail("", "Call is On Hold", $result);

                if ($value === "Unhold") {
                    echo "$result\n";
                    mail("", "Call has been Unheld", $result);



The trunk is defined with a hostname (such as ans uses a short registration period (180s).
Sometimes where the host replies to OPTIONS (so trunk is reachable) but rejects REGISTER. Maybe within ITSP architecture, the boxes tin-hat reply to REGISTER or OPTION are not the same as there is some load balancing or fencing device between.

Unfortunately I don’t manager either the router providing Internet access nor the firewall between the router and the LAN so I can’t check if my REGISTER messages ever leave the LAN to reach the Internet.

At the moment, the script would run simple black box tests such as:

  • ping LAN gateway and DNS server (is the router reachable, is name resolution working
  • check if name resolution works for the ITSP host (ie if translates to something) or has changed
  • check if the ITSP host replies to ping (though I’m not sure if in practice you can draw any conclusion from this you may have different boxe responding to pings, specifically)
  • maybe a traceroute to the ITSP host

Maybe in the future, this script would just reload PJSIP.

AMI’s Registry event with a filter on Status value seems to match what I’m after

Thank you very much for mentioning AMI and providing the above code.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.