Linux Blog

Asterisk AGI IP Address lookup

Filed under: General Linux — TheLinuxBlog.com at 12:01 am on Sunday, September 19, 2010

While not exactly a shell script, I figured it would be worthy to post something rather than nothing and technically it is a script none-the-less.
I was experimenting with Asterisk AGI scripts and needed a project. I decided that an IP address lookup would be a good one. Sometimes my dynamic IP changes but my dynamic DNS doesn’t update. With this script, I should be able to dial into my Asterisk machine and get it to tell me the IP address. At least, that’s the plan.

Here is the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/php
<?php
$agivars = array();
while (!feof(STDIN)) {
$agivar = trim(fgets(STDIN));
if ($agivar === '') {
break;
}
$agivar = explode(':', $agivar);
$agivars[$agivar[0]] = trim($agivar[1]);
}
extract($agivars);</code>
 
if ((time()-filemtime("/tmp/ipaddress.txt")) > 299) {
$ext = file_get_contents("http://www.whatismyip.com/automation/.asp");
file_put_contents("/tmp/ipaddress.txt", $ext);
touch("/tmp/ipaddress.txt");
}
else {
$ext = file_get_contents("/tmp/ipaddress.txt");
}
 
$arrayExt = explode(".", $ext);
$again = true;
 
while ($again == true) {
$again = false;
$i = 0;
foreach ($arrayExt AS $row) {
if ($i &gt; 0) { execute_agi('STREAM FILE dot ""');
}
execute_agi("SAY DIGITS $row \"\"");
$i++;
}
$i=0;
 
execute_agi('STREAM FILE to-hear-msg-again ""');
execute_agi('STREAM FILE press-1 ""');
 
$again = execute_agi('WAIT FOR DIGIT -1');
 
if ($again['result'] == 49) {
$again = true;
}
else {
$again = false;
}
 
}
 
exit;
 
function execute_agi($command) {
global $debug_mode, $log_file;
 
fwrite(STDOUT, "$command\n");
fflush(STDOUT);
$result = trim(fgets(STDIN));
$ret = array('code'=> -1, 'result'=> -1, 'timeout'=> false, 'data'=> '');
if (preg_match("/^([0-9]{1,3}) (.*)/", $result, $matches)) {
$ret['code'] = $matches[1];
$ret['result'] = 0;
if (preg_match('/^result=([0-9a-zA-Z]*)\s?(?:\(?(.*?)\)?)?$/', $matches[2], $match))  {
$ret['result'] = $match[1];
$ret['timeout'] = ($match[2] === 'timeout') ? true : false;
$ret['data'] = $match[2];
}
}
if ($debug_mode && !empty($logfile)) {
$fh = fopen($logfile, 'a');
if ($fh !== false) {
$res = $ret['result'] . (empty($ret['data']) ? '' : " / $ret[data]");
fwrite($fh, "-------\n>> $command\n<< $result\n<<     parsed $res\n");
fclose($fh);
}
}
return $ret;
}
 
?>

Coupled with the digits 47 (for IP) in my dialplan as follows:

exten =>> 47,1,AGI(ip-test.agi)

Here is what it Asterisk AGI IP lookup sounds like. For those who refuse to play mp3′s Asterisk AGI IP lookup in Ogg format.





Random Man Pages:
pom
times
bg_test
ur98

1 Comment »

Pingback by The Linux Blog » Asterisk AGI Area Code Lookup

January 9, 2011 @ 6:02 am

[...] written about my Asterisk meddling before in my Asterisk AGI IP Address lookup post. This time I’m writing about an Area Code Lookup Script I wrote that was inspired by [...]

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>