2

Ok, so I have a ruby script that grabs some data from FM Server and returns a tuple. I had to do this because there's no good perl FM module that I'm aware of.

[test.pl]

$ret = `ruby /root/rfm-query.rb $cid`;
@extens = split(/,/, $ret, 2);
print "DIAL SIP/$extens[0]";

So when I run this it will print "DIAL SIP/215" as expected but when using the same code in an Asterisk AGI script and using $extens[0] it always returns nothing.

#!/usr/bin/env perl
use Asterisk::AGI;
$|=1;

$AGI = new Asterisk::AGI;
%input = $AGI->ReadParse();

$cid = substr $input{'callerid'}, 1;
$cid =~ s/\+//g;

$ret = `ruby /root/rfm-query.rb $cid`; #rets nothing
@extens = split(/,/, $ret, 2);

$AGI->exec("DIAL SIP/$extens[0]");

Why does it work in a test script but not in an AGI?

cstrouse
  • 288
  • 1
  • 3
  • 17
  • Does [Net::FileMaker](http://search.cpan.org/dist/Net-FileMaker/) do what you're looking for? – cjm Jun 03 '11 at 19:32
  • I looked at Net::FileMaker and the documentation seems to suck. I'm not really sure if it does what I need or not. – cstrouse Jun 03 '11 at 19:47
  • @mu Yes they are both being ran as the same user. I can't tell you what's in $? when it fails because I can't figure out how to get the AGI script to output text to the Asterisk console. – cstrouse Jun 03 '11 at 19:48

1 Answers1

2

I'm not sure what an Asterix AGI script is, but if its anything like CGI, where your code is being run by a server, then its probably running as a different user as you. Hopefully it is and not root and it probably can't read /root/rfm-query.rb.

You can check this by trying to open and print the file for reading.

my $rfm_query_file = "/root/rfm-query.rb";
open my $fh, "<", $rfm_query_file or die "Cant open $rfm_query_file: $!";

(Also, shame on you if you're developing and testing code as root.)

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Asterisk runs with it's own user so I tried placing the ruby script into that user's directory and it still doesn't work. I don't understand why it refuses to work properly when ran via AGI but works fine when ran as a regular perl script on the CLI. – cstrouse Jun 03 '11 at 20:18
  • Could the script read the file? Also, the STDERR from the script is probably going into a log file somewhere and you're not seeing it. You can capture it with `ruby /root/rfm-query.rb $cid 2>&1`. Its also possible that `$cid` is not what you think it is. It might have spaces and shell meta-characters which is causing the command line to be incorrect. – Schwern Jun 03 '11 at 20:26
  • I don't know because I can't get it to output anything at all on the asterisk console for me to see. – cstrouse Jun 03 '11 at 20:29
  • If $cid wasn't what I was expecting then why does it work in the other script which contains the same code? – cstrouse Jun 03 '11 at 20:30
  • Ok thanks man. Adding 2>&1 showed me what the problem is. It wasn't able to find the ruby script. – cstrouse Jun 03 '11 at 20:32