# Problem with agi script

**URL:** <https://community.asterisk.org/t/problem-with-agi-script/30256>\
**Category:** Asterisk Support\
**Created:** [January 15, 2010, 3:57pm UTC](https://community.asterisk.org/t/problem-with-agi-script/30256 "2010-01-15T15:57:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![blwegrzyn](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@blwegrzyn](https://community.asterisk.org/u/blwegrzyn)\
**Post date:** [January 15, 2010, 3:57pm UTC](https://community.asterisk.org/t/problem-with-agi-script/30256/1 "2010-01-15T15:57:08Z")

</div>

Hello,  
I have a script that checks for the clidid existence.  
It worked fine with older version of asterisk (1.4.x)  
Now after i switched to the latest(1.6.2.0) i get errors but the script works fine.  
ERROR[13171]: utils.c:1126 ast\_carefulwrite: write() returned error: Broken pipe

any ideas?

#!/usr/bin/php -q

 \<?php ob\_implicit\_flush(true); set\_time\_limit(6); $in = fopen("php://stdin","r"); $stdlog = fopen("/var/log/asterisk/my\_agi.log", "w"); // toggle debugging output (more verbose) $debug = false; // Do function definitions before we start the main loop function read() { global $in, $debug, $stdlog; $input = str\_replace("\n", "", fgets($in, 4096)); if ($debug) fputs($stdlog, "read: $input\n"); return $input; } function errlog($line) { global $err; echo "VERBOSE \"$line\"\n"; } function write($line) { global $debug, $stdlog; if ($debug) fputs($stdlog, "write: $line\n"); echo $line."\n"; } // parse agi headers into array while ($env=read()) { $s = split(": ",$env); $agi[str\_replace("agi\_","",$s[0])] = trim($s[1]); if (($env == "") || ($env == "\n")) { break; } } // main program // main program $cli = $agi[callerid]; $exten= $agi[extension]; //$pattern = "\*\\<[0-9]+\\>\*"; $pattern = "\*[0-9]+\*"; preg\_match($pattern,$cli,$matches); $out = trim($matches[0], "\<\>"); errlog("Call from -$out-"); errlog("Call from -$cli-"); errlog("Call from -$matches[0]-"); If (!$out) { write ("SET CONTEXT from-sbc"); write ("EXEC GoTO from-sbc|s|1"); } Else { write ("SET CONTEXT planusers"); write ("EXEC GoTO $out,1"); } // clean up file handlers etc. fclose($in); fclose($stdlog); exit; ?\> 

when asterisk executes it it give errors:

```
-- Executing [s@pppp:2] AGI("SIP/sip.broadvoice.com.home-00000004", "checkcid.agi") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/checkcid.agi

```

\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_request: checkcid.agi  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_channel: SIP/sip.broadvoice.com.home-00000004  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_language: en  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_type: SIP  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_uniqueid: 1263569251.6  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_version: 1.6.2.0  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_callerid: 1112223334  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_calleridname: Chicgozn01 IL  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_callingpres: 0  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_callingani2: 0  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_callington: 0  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_callingtns: 0  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_dnid: 2212223334  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_rdnis: unknown  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_context: pppp  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_extension: s  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_priority: 2  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_enhanced: 0.0  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_accountcode:  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> agi\_threadid: -1222173808  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\>  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Rx \<\< VERBOSE "Call from -1112223334-"  
checkcid.agi: Call from -1112223334-  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> 200 result=1  
[Jan 15 09:27:39] ERROR[13171]: utils.c:1126 ast\_carefulwrite: write() returned error: Broken pipe  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Rx \<\< VERBOSE "Call from -1112223334-"  
checkcid.agi: Call from -1112223334-  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> 200 result=1  
[Jan 15 09:27:39] ERROR[13171]: utils.c:1126 ast\_carefulwrite: write() returned error: Broken pipe  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Rx \<\< VERBOSE "Call from -1112223334-"  
checkcid.agi: Call from -1112223334-  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> 200 result=1  
[Jan 15 09:27:39] ERROR[13171]: utils.c:1126 ast\_carefulwrite: write() returned error: Broken pipe  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Rx \<\< SET CONTEXT planusers  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> 200 result=0  
[Jan 15 09:27:39] ERROR[13171]: utils.c:1126 ast\_carefulwrite: write() returned error: Broken pipe  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Rx \<\< EXEC GoTO 1112223334,1  
– AGI Script Executing Application: (GoTO) Options: (1112223334,1)  
– Goto (planusers,1112223334,1)  
\<SIP/sip.broadvoice.com.home-00000004\>AGI Tx \>\> 200 result=0  
[Jan 15 09:27:39] ERROR[13171]: utils.c:1126 ast\_carefulwrite: write() returned error: Broken pipe  
– \<SIP/sip.broadvoice.com.home-00000004\>AGI Script checkcid.agi completed, returning 0
