#!/usr/bin/perl -w # This program will take entries from your sip.conf file and create # phoneXXX.cfg files in your /tmp directory, using your latest phone1.cfg # file. $sip_conf = '/etc/asterisk/sip.conf'; $polycom_template = '/home/PlcmSpIp/phone1.cfg'; # The template file @mods = (); @xmlfile = (); # These are the variables we're going to change @mod_vars = qw( reg.1.displayName reg.1.address reg.1.label reg.1.type reg.1.auth.userId reg.1.auth.password ); # This maps the fields from sip.conf to the above: %map_sip = ( "callerid" => "reg.1.displayName", "secret" => "reg.1.auth.password" ); # First, we create a hash of arrays. Each array has the info for the # individual extension my %phones; my @sipinfo = (); open( SIPINFO, "< $sip_conf" ); @sipinfo = ; foreach $sip_line (@sipinfo) { if ( $sip_line =~ m{\[(.*?)\]\s+$}i ) { $extension = $1; push ( @{ $phone{$extension} }, "reg.1.address=" . '"' . $extension . '"' ); push ( @{ $phone{$extension} }, "reg.1.label=" . '"' . $extension . '"' ); push ( @{ $phone{$extension} }, "reg.1.auth.userId=" . '"' . $extension . '"' ); push ( @{ $phone{$extension} }, "reg.1.type=" . '"' . "private" . '"' ); } else { if ( $sip_line =~ m{callerid=\"(.*?)\".*$}i ) { push ( @{ $phone{$extension} }, $map_sip{"callerid"} . '="' . $1 . '"' ); } if ( $sip_line =~ m{secret=(.*?)\s+$}i ) { push ( @{ $phone{$extension} }, $map_sip{"secret"} . '="' . $1 . '"' ); } } } close SIPINFO; # Now we get the variables to change from the template file # in one array and keep the entire file in another open( MODS, "< $polycom_template" ); @xmlfile = ; foreach $xml_line (@xmlfile) { foreach $mod_me (@mod_vars) { if ( $xml_line =~ m{.*($mod_me=\".*?\")}i ) { push @mods, $1; } } } close MODS; # Finally, we go through each extension, and make a new file with the # extension values from sip.conf, dumping them in the /tmp directory. # (You can do what you want with them from there) foreach $configfile ( sort keys %phone ) { $newfile = '/tmp/' . "phone" . $configfile . ".cfg"; open( NEWCONF, ">> $newfile" ); my @newconfig = (); @newconfig = @xmlfile; foreach $line (@newconfig) { foreach $tothis ( @{ $phone{$configfile} } ) { foreach $changethis (@mods) { if ( substr( $changethis, 0, index( $changethis, "=" ) ) eq substr( $tothis, 0, index( $tothis, "=" ) ) ) { $line =~ s/$changethis/$tothis/g; } } } print NEWCONF $line; } close NEWCONF; } exit 0;