AppleScript C Perl Shell Xcode Other

Update dynamic IP on FreePBX box

Post Reply
coding / perl     Views: 49Prev .. Next
Update dynamic IP on FreePBX boxPosted: Wednesday, March 7, 2018 [20:52:08] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 166
If trunk requires an "externip" to be provided in SIP settings, every time your WAN IP changes it has to be reflected in settings.
Using a pfSense it is easy to read a WAN IP, say once an hour:
View Codemy $current_wan_ip = `/usr/bin/ssh root\@192.168.2.1 /sbin/ifconfig re0 | sed -n '/.inet /{s///;s/ .*//;p;}'`;
chomp $current_wan_ip;
if(-f '/http/current.wan.ip.txt') {$was_ip_wan = `cat /http/current.wan.ip.txt`;}
else {open(TXWW,">/http/current.wan.ip.txt");print TXWW $current_wan_ip;close(TXWW);}

## If IP is new - write a trigger file for FreePBX update
unless(($was_ip_wan eq $current_wan_ip) && $was_ip_wan) {
open(AST,">/http/asteriskchange.txt");print AST "$was_ip_wan\t$current_wan_ip";close(AST);
}


Then if trigger file present - run an Asterisk update script.
You can verify which Asterisk files your need to update by running a find on FreePBX:
View Codefind /etc/asterisk -type f -iname '*.conf' -exec grep -qi 'Your_Current_IP_Address' '{}' \; -print


List of those files to be included in update script below:
View Code#!/usr/bin/perl
## If run by cron - making sure the path is set
unless($ENV{'SECURITYSESSIONID'}) {
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local:/usr/local/bin';
}

## Do not run unless change is required
unless(-f "/http/asteriskchange.txt") {print "No source file present - exit\n";exit(0);}
$ips = `cat /http/asteriskchange.txt`;$ips =~ s/\r|\n//g;($was_ip,$now_ip) = split(/\t/,$ips);
$ips='';
## We need both (old and new) IP addresses to process, if one missing - exit
unless($was_ip && $now_ip) {print "At least one IP is missing - exit\n";exit(0);}

## Asterisk files to be updated by the script provided by "find" search
@allfiles = ('/etc/asterisk/sip_additional.conf',
'/etc/asterisk/sip_general_additional.conf',
'/etc/asterisk/pjsip.transports.conf');

## Keep the old files, just in-case
if(-f "/http/asteriskchange.old.txt") {unlink "/http/asteriskchange.old.txt";}
`mv /http/asteriskchange.txt /http/asteriskchange.old.txt`;

## Now update Asterisk config files
foreach $file (@allfiles) {$localfile=$file;$localfile =~ s#/etc/asterisk/##;
if(-f "/http/ipLogD/$localfile") {unlink "/http/ipLogD/$localfile";}
`scp root\@192.168.2.50:$file /http/temp/$localfile`;sleep(1);
$d = `cat /http/temp/$localfile`;
if($d =~ m/$was_ip/) {
$d =~ s#$was_ip#$now_ip#gs;
open(TXT,">/http/temp/$localfile");print TXT $d;close(TXT);$donefiles++;
`scp /http/ipLogD/$localfile root\@FreePBX_IP_Address:$file`;
} ## END IF FILE MATCHES OLD IP
} ## END FOREACH FILE

## If something changed - restart Asterisk
if($donefiles) { #############
`ssh root\@FreePBX_IP_Address '/usr/sbin/fwconsole restart'`;
} ## END IF FILES UPDATED ####


It is safe to run this script from cron once an hour or so.
All scripts above require password-less login to your FreePBX and pfSense boxes.
More on password-less login:
https://www.codemacs.com/coding/applescript/ftp-like-commands-and-remote-osascript-on-macs.5918921.htmThere's no place like ~
RE: Update dynamic IP on FreePBX boxPosted: Friday, March 9, 2018 [15:18:32] - 2
rootPosted by:rootMember Since:
June 16 2010
Posts: 166
Also, Asterisk SIP settings have to be updated with new IP Address:
Settings > Asterisk SIP Settings > External Address: NEW_IP_ADDRESS


Script above will update IP settings. Unless Asterisk SIP Settings are updated - an old IP will be written to the appropriate setup files either after reboot or Asterisk restart.There's no place like ~
coding / perlPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other