AMI doesn't execute command

Hello,

I have problem with my AMI command from Perl script.
I am able to login and logoff but no command is executed.

Asterisk 14.3.0
CentOS Linux 7

#!/usr/bin/perl

use IO::Socket::INET;

sub asterisk_command ()
{
#    my $command=$_[0];
    my $ami=IO::Socket::INET->new(PeerAddr=>'10.8.34.40',PeerPort=>5038,Proto=>'tcp') or die "failed to connect to AMI!";

        print $ami "Action: Login\r\nUsername: test\r\nSecret: 12345678\r\n\r\n";
        print $ami "Action: Originate\r\nChannel: SIP/test/200\r\nContext: phones\r\nExten: 200\r\nPriority: 1\r\n\r\n";
        print $ami "Action: Logoff\r\n\r\n";

}

#asterisk_command ("Action: Originate\r\nChannel: SIP/test/200\r\nContext: phones\r\nExten: 200\r\nPriority: 1\r\n");
asterisk_command ();

[test]
secret=12345678
deny=0.0.0.0/0.0.0.0
permit=10.8.34.40/255.255.255.0
read=all
write=all

== Manager ‘test’ logged on from 10.8.34.40
== Manager ‘test’ logged off from 10.8.34.40

When I use this via Telnet I have no problem:

telnet 10.8.34.40 5038

Action: Login
Username: test
Secret: 12345678

Action: Originate
Channel: SIP/test/200
Context: phones
Exten: 200
Priority: 1

 == Using SIP RTP CoS mark 5
    -- Called test/200
    -- SIP/test-00000007 is ringing
       > 0x7f6c30006af0 -- Probation passed - setting RTP source address to 10.4.10.98:51808
    -- SIP/test-00000007 answered
    -- Executing [200@phones:1] NoOp("SIP/test-00000007", "Test line 100") in new stack
    -- Executing [200@phones:2] Dial("SIP/test-00000007", "SIP/test2") in new stack
  == Using SIP RTP CoS mark 5
    -- Called SIP/test2
       > 0x7f6c30006af0 -- Probation passed - setting RTP source address to 10.4.10.98:51808
    -- SIP/test2-00000008 is ringing
    -- Got SIP response 486 "Busy Here" back from 10.4.10.98:5061
    -- SIP/test2-00000008 is busy
  == Everyone is busy/congested at this time (1:1/0/0)
    -- Executing [200@phones:3] Hangup("SIP/test-00000007", "") in new stack
  == Spawn extension (phones, 200, 3) exited non-zero on 'SIP/test-00000007'

Could you please give me some advice what I have done wrong?

I have also tried this:

#!/usr/bin/perl

use Asterisk::AMI;
$astman = Asterisk::AMI->new(PeerAddr => '10.8.34.40',
                                PeerPort => '5038',
                                Username => 'test',
                                Secret => '12345678'
                        );
 
die "Unable to connect to asterisk" unless ($astman);

print $astman;
 
my $action = $astman->({ Action => 'Command',
                      Command => 'sip show peers'
                    });

$astman->send_action(\%action);

Same problem.

Thank you for your help.

Solution:

I have to added sleep(1) before my logoff:

sleep(1);
print $ami "Action: Logoff\r\n\r\n";

Calling a number that is engaged.

Sorry I have no clue what you have meant.

The phone that you have called is busy.

AMI is executing the call, but the called device is rejecting the call with a status code that indicates that the person called busy.

Now I see. This is because I have not accept this call.

The problem is that my AMI command from Perl script is not executed.

My steps:

  1. Test telnet connection - OK
  2. Test “action” via telnet - OK
  3. Test Perl script Login/Logout - OK
  4. Test Perl script execute “action” - PROBLEM

Please provide a log demonstrating the failure case.

I hope that all is in my initial post.

  • Perl script
  • log from Asterisk CLI (only manager login\logout)
  • my test from telnet session

It doesn’t have a gray background. You need to realise that people skim postings for relevant information. In particular, if there is anything wrong in the more obvious log, people are not going to look beyond that.

In that case, there is not enough logging to see what is going wrong.

The first one will get a broken pipe , possibly as early as the response to the login.

I’m not sufficiently familiar with the class library to know what it waits for, but it may well get a broken pipe whilst processing the command.

You should read all the responses before falling off the end of the script.

It means, that you dont know why my commands are not executed in manager CLI.
You just think that problem is in broken pipe - what means my IO::Socket::INET connection is broken.
I think that this is not the problem because I have tested it :

I have tried two things:

  1. Telnet connection and whole AMI command
  • it works

AMI command:

Action: Login
Username: test
Secret: 12345678

Action: Command
Command: sip reload

Action: Logoff

AMI execution:

[root@asterisk]# telnet 10.8.34.40 5038
Trying 10.8.34.40...
Connected to 10.8.34.40.
Escape character is '^]'.
Asterisk Call Manager/3.1.0
Action: Login
Username: test
Secret: 12345678

Action: Command
Command: sip reload

Action: LogoffResponse: Success
Message: Authentication accepted

Event: FullyBooted
Privilege: system,all
Uptime: 433517
LastReload: 433517
Status: Fully Booted

Response: Success
Message: Command output follows
Output:

Event: SuccessfulAuth
Privilege: security,all
EventTV: 2017-04-03T09:26:43.117+0200
Severity: Informational
Service: AMI
EventVersion: 1
AccountID: test
SessionID: 0x7f6c40000e78
LocalAddress: IPV4/TCP/0.0.0.0/5038
RemoteAddress: IPV4/TCP/10.8.34.40/51330
UsingPassword: 0
SessionTV: 2017-04-03T09:26:43.117+0200



Response: Goodbye
Message: Thanks for all the fish.

Connection closed by foreign host.
[root@asterisk]#

AMI Asterisk CLI response:

asterisk*CLI>
  == Manager 'test' logged on from 10.8.34.40
 Reloading SIP
  == Manager 'test' logged off from 10.8.34.40
asterisk*CLI>
  1. I have edit my scrip and execute
  • command is not executed

Per script:
AMI command (same as in telnet connection)>> Action: Login\r\nUsername: test\r\nSecret: 12345678\r\n\r\nAction: Command\r\nCommand: sip reload\r\n\r\nAction: Logoff\r\n\r\n

#!/usr/bin/perl

    sub asterisk_command_sip_reload ()
    {
    #    my $command=$_[0];
        my $ami=IO::Socket::INET->new(PeerAddr=>'10.8.34.40',PeerPort=>5038,Proto=>'tcp') or die "failed to connect to AMI!";

            print $ami "Action: Login\r\nUsername: test\r\nSecret: 12345678\r\n\r\nAction: Command\r\nCommand: sip reload\r\n\r\n ";
            print $ami "Action: Logoff\r\n\r\n";

    }

    #asterisk_command ("Action: Command\r\nCommand: sip reload\r\n\r\n");
    asterisk_command_sip_reload ();

AMI CLI response:

asterisk*CLI>
  == Manager 'test' logged on from 10.8.34.40
  == Manager 'test' logged off from 10.8.34.40
asterisk*CLI>

Telnet output:

Event: SuccessfulAuth
Privilege: security,all
EventTV: 2017-04-03T09:49:54.985+0200
Severity: Informational
Service: AMI
EventVersion: 1
AccountID: test
SessionID: 0x7f6c2c00fe08
LocalAddress: IPV4/TCP/0.0.0.0/5038
RemoteAddress: IPV4/TCP/10.8.34.40/51336
UsingPassword: 0
SessionTV: 2017-04-03T09:49:54.985+0200

Perl scrip execution without error:

[root@asterisk perl]# ./asterisk_AMI_login.pl
[root@asterisk perl]#

@david551 : Thank you for your advice how to write posts. This is mine first one and I have tried my best. The problem is that I dont get log from my AMI script if command failed or what so ever. I have written all information what I can see. If you need more, just tell me what and I will try to provide it.

The connection is broken when the script exits. The script is exiting before all the responses have been received.

@david551 Thanks now it works fine .-)

I have to added sleep(1) before my logoff:

sleep(1);
print $ami "Action: Logoff\r\n\r\n";

Now I can move on.