<div dir="ltr">thanks a lot Tony<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Oct 10, 2013 at 4:31 PM, Tony Mountifield <span dir="ltr"><<a href="mailto:tony@softins.co.uk" target="_blank">tony@softins.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">In article <<a href="mailto:CAE6_NE%2BDxtsGAdTg0Mp-9JUMnGXGuwo4eXAdM_HrwC8oPUOsew@mail.gmail.com">CAE6_NE+DxtsGAdTg0Mp-9JUMnGXGuwo4eXAdM_HrwC8oPUOsew@mail.gmail.com</a>>,<br>

<div><div class="h5">akhilesh chand <<a href="mailto:omakhileshchand@gmail.com">omakhileshchand@gmail.com</a>> wrote:<br>
><br>
> I want to make call through socket i have set code given below:<br>
><br>
> #!/usr/bin/perl -w<br>
><br>
> use IO::Socket::INET;<br>
><br>
><br>
> sub asterisk_command ()<br>
>         {<br>
>         #  my $command=$_[0];<br>
>         my<br>
> $ami=IO::Socket::INET->new(PeerAddr=>'127.0.0.1',PeerPort=>5038,Proto=>'tcp')<br>
> or die "failed to connect to AMI!";<br>
>         print $ami "Action: Login\r\nUsername: lite\r\nSecret:<br>
> 4003\r\n\r\nAction: Logoff\r\n\r\n";<br>
>         }<br>
> &asterisk_command("Channel: DAHDI/27/7702009896\r\nExten: s\r\nContext:<br>
> outbound\r\nCallerID: 20048645\r\nPriority: 1\r\nMaxRetries: 2\r\n");<br>
><br>
> Whenever i execute that code i'm get following error<br>
><br>
> [Oct 10 15:13:23] ERROR[856]: utils.c:1175 ast_careful_fwrite: fwrite()<br>
> returned error: Broken pipe<br>
> [Oct 10 15:13:23] ERROR[856]: utils.c:1175 ast_careful_fwrite: fwrite()<br>
> returned error: Broken pipe<br>
> [Oct 10 15:13:23] ERROR[856]: utils.c:1175 ast_careful_fwrite: fwrite()<br>
> returned error: Broken pipe<br>
> [Oct 10 15:13:23] ERROR[856]: utils.c:1175 ast_careful_fwrite: fwrite()<br>
> returned error: Broken pipe<br>
><br>
><br>
> asterisk verison :-  1.6.2.7<br>
> CentOS release 5.3<br>
> kernel version :- 2.6.18-128.el5<br>
<br>
</div></div>AMI is a *two-way* protocol. You mustn't just fire in a bunch of commands<br>
and close the socket!<br>
<br>
The reason Asterisk reports the fwrite() error is because you have closed<br>
the socket before it had a chance to send you the responses.<br>
<br>
What you need to do is this:<br>
<br>
1. Connect to the AMI port.<br>
2. Read the one-line greeting message that Asterisk sends you. It will tell<br>
   you the version of the protocol (which might be of interest if you wanted<br>
   to be compatible with different versions of Asterisk).<br>
3. Send the Login action with username, secret and terminating blank line.<br>
4. Read the response lines from Asterisk until it gives you a blank line.<br>
5. Send whatever command you want it to do, and go back to step 4.<br>
6. When you have done the commands you want, send the Logoff action.<br>
7. *** READ THE RESPONSE TO THE LOGOFF<br>
8. Close the socket.<br>
<br>
If it helps. Here is similar piece of code I wrote to query pri spans.<br>
Note carefully the setting of $/ in two places, and the inclusion of<br>
"Events: off" to avoid responses getting confused by asynchronous events.<br>
<br>
==================================================================<br>
#!/usr/bin/perl<br>
<br>
use IO::Socket;<br>
<br>
my $numspans = 4;<br>
my $host = 'localhost';<br>
my $login = "Action: login\r\nUsername: xxxx\r\nSecret: yyyy\r\nEvents: off\r\n\r\n";<br>
<br>
$/ = "\r\n";    # <> reads a single line for signon banner<br>
<br>
my $s = IO::Socket::INET->new("$host:5038") or die "can't connect to $host: $!\n";<br>
my $banner = <$s>;      # read the banner<br>
<br>
#my $line = ('-' x 78)."\n";<br>
#print $banner,$line;<br>
<br>
$/ = "\r\n\r\n";        # <> reads a complete response ending in a blank line<br>
<br>
print $s $login;<br>
my $resp = <$s>;<br>
<br>
#print $resp,$line;<br>
<br>
my @spans;<br>
<br>
foreach $span (1..$numspans) {<br>
        print $s "Action: Command\r\nCommand: pri show span $span\r\n\r\n";<br>
        $resp = <$s>;<br>
        #print $resp,$line;<br>
<br>
        if ($resp =~ /Status: (.*)\n/) {<br>
                $status = $1;<br>
        } else {<br>
                $status = 'Unknown';<br>
        }<br>
        $spans[$span-1] = "Span $span status = $status\n";<br>
}<br>
<br>
print $s "Action: Logoff\r\n\r\n";<br>
$resp = <$s>;<br>
#print $resp,$line;<br>
<br>
close $s;<br>
<br>
# go on to display the results from @spans<br>
==================================================================<br>
<br>
Cheers<br>
Tony<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Tony Mountifield<br>
Work: <a href="mailto:tony@softins.co.uk">tony@softins.co.uk</a> - <a href="http://www.softins.co.uk" target="_blank">http://www.softins.co.uk</a><br>
Play: <a href="mailto:tony@mountifield.org">tony@mountifield.org</a> - <a href="http://tony.mountifield.org" target="_blank">http://tony.mountifield.org</a><br>
<br>
--<br>
_____________________________________________________________________<br>
-- Bandwidth and Colocation Provided by <a href="http://www.api-digital.com" target="_blank">http://www.api-digital.com</a> --<br>
New to Asterisk? Join us for a live introductory webinar every Thurs:<br>
               <a href="http://www.asterisk.org/hello" target="_blank">http://www.asterisk.org/hello</a><br>
<br>
asterisk-users mailing list<br>
To UNSUBSCRIBE or update options visit:<br>
   <a href="http://lists.digium.com/mailman/listinfo/asterisk-users" target="_blank">http://lists.digium.com/mailman/listinfo/asterisk-users</a><br>
</font></span></blockquote></div><br></div>