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";