Projet

Général

Profil

Actions

FsUserCreat.pl

Lit les arguments et génère des fichiers de config d'utilisateur pour freeswitch

fsUserCreate.pl <extension> <identifiant> <password>

generera les fichiers de config pour un seul utilisateur. Les argument suivants seront ignorés.

fsUsercreate.pl -f <fichiers1> <fichiers2>

Avec l'option -f les arguments seront des fichiers où chaque ligne sera lue comme une suite d'arguments dans l'ordre : . Plusieurs fichiers peuvent être lus à la suite.

Exemples

fsUserCreate.pl 2010 toto secretpass123

generera un utilisateur nommé toto portant l'extension (numero d'appel) 2010 et qui aura pour mot de passe secretpass123 .

fsUsercreate.pl -f listeUtilisateurs

lira le fichier listeUtilisateurs dans lequel chacune des lignes decrira un utilisateur sur le modèle précédent.

Code source

#! /usr/bin/perl -s

use XML::Writer;
use IO::File;

############
#       FUNCTIONS
#
#       - readUserFile
#               Read list of users from a file and push it in the array
#       - generateUser
#               Generate XML directory files for freeswitch from the array
#
############

sub readUserFile {      # Read userlist from a file
        open(FICHIER,"<$fileNumber") or die("open:  $!") ;

        while ($line =<FICHIER>)        {
                if ( $line =~ /^$/ )    {
        #               PRINT "ligne vide.\n" ;
        #               next ;
                } else  {
                @words = split ( /\W+/ , $line  ) ;
                push @userList, { uid => @words[0], name => @words[1], pass => @words[2] };
                }
        }

        close FICHIER ;
}

sub generateUser  {
        my ( $userUid, $userName, $userPass ) = @_ ;            # Get the data from sources

        my $outputdir = "/usr/local/freeswitch/conf/directory/default" ;

#       my $outputFile = $userUid ;                                                     # The output file will be named with user's uid

        my $output = new IO::File(">$outputDir/$userId.xml");           # Et voilà !

        my $writer = new XML::Writer( 
        OUTPUT      => $output,
        DATA_INDENT => 8,             # indentation, huit espaces
        DATA_MODE   => 1,             # changement ligne.
        ENCODING    => 'utf-8',
        );

        # Ok! Now let's generate some Good'Ol' XML !!

        $writer->comment("This file was auto generated and will may be again. Do not edit it manually, modifications could be lost");

        $writer->startTag( "include" ) ;
                $writer->startTag( "user", "id" => $userUid );
                        $writer->startTag( "params" ) ;
#                               $writer->emptyTag( "param", "name" => "password", "value" => "\$\${default_password}" ) ;
                                $writer->emptyTag( "param", "name" => "password", "value" => $userPass ) ;
                                $writer->emptyTag( "param", "name" => "vm-password", "value" => $userUid ) ;
                        $writer->endTag( "params" ) ;

                        $writer->startTag( "variables" ) ;
                                $writer->emptyTag( "variable" , "name" => "toll_allow", "value" => "domestic,international,local" ) ;
                                $writer->emptyTag( "variable" , "name" => "accountcode", "value" => $userUid ) ;
                                $writer->emptyTag( "variable" , "name" => "user_context", "value" => "default" ) ;
                                $writer->emptyTag( "variable" , "name" => "effective_caller_id_name", "value" => $userName ) ;
                                $writer->emptyTag( "variable" , "name" => "effective_caller_id_number", "value" => $userUid ) ;
                                $writer->emptyTag( "variable" , "name" => "outbound_caller_name", "value" => "\$\${outbound_caller_name}" ) ;
                                $writer->emptyTag( "variable" , "name" => "outbound_caller_number", "value" => "\$\${outbound_caller_id}" ) ;
                                $writer->emptyTag( "variable" , "name" => "callgroup", "value" => "techsupport" ) ;
                        $writer->endTag( "variables" ) ;


                $writer->endTag("user");
        $writer->endTag("include");


#       my $outputFile = $userName ;                                                    # The output file will be named with user's uid

        my $output = new IO::File(">$outputDir/$userName.xml");         # Et voilà !
#       my $output = new IO::File(">/usr/local/freeswitch/conf/directory/default/$outputFile.xml");             # Et voilà !

        my $writer = new XML::Writer( 
        OUTPUT      => $output,
        DATA_INDENT => 8,             # indentation, huit espaces
        DATA_MODE   => 1,             # changement ligne.
        ENCODING    => 'utf-8',
        );

        # Ok! Now let's generate some Good'Ol' XML !!

        $writer->comment("This file was auto generated and will may be again. Do not edit it manually, modifications could be lost");

        $writer->startTag( "include" ) ;
                $writer->startTag( "user", "id" => $userName , "cidr" => "192.0.2.0/24" );

                        $writer->startTag( "gateways" ) ;
                        $writer->endTag( "gateways" ) ;

                        $writer->startTag( "params" ) ;
#                               $writer->emptyTag( "param", "name" => "password", "value" => "\$\${default_password}" ) ;
                                $writer->emptyTag( "param", "name" => "password", "value" => $userPass ) ;
                                $writer->emptyTag( "param", "name" => "vm-password", "value" => $userUid ) ;
                        $writer->endTag( "params" ) ;

                        $writer->startTag( "variables" ) ;
                                $writer->emptyTag( "variable" , "name" => "user_context", "value" => "default" ) ;
                                $writer->emptyTag( "variable" , "name" => "effective_caller_id_name", "value" => $userName ) ;
                                $writer->emptyTag( "variable" , "name" => "effective_caller_id_number", "value" => $userUid ) ;
                                $writer->emptyTag( "variable" , "name" => "process_cdr", "value" => "true" ) ;
                                $writer->emptyTag( "variable" , "name" => "sip_secure_media", "value" => "true" ) ;
                        $writer->endTag( "variables" ) ;

                        $writer->startTag( "vcard" ) ;
                        $writer->endTag( "vcard" ) ;

                $writer->endTag("user");
        $writer->endTag("include");
}

#############
#  ACTION
##################
if ( $f ==1 )   {                       # if switch f is on 
        for $fileNumber ( @ARGV ) {     # read arguments as list of source files
                readUserFile ;          # and treat them with readUserFile Function
        }
}
else    {                               # else simply push arguments in the array
        push @userList, { uid => @ARGV[0], name => @ARGV[1], pass => @ARGV[2] };
}

for $i ( @userList ) {                  # for each user in the array
        generateUser( $i->{uid}, $i->{name}, $i->{pass} ) ;     # generate appropriate XML files with generateUser
        print "Files for user $i->{name} with extension $i->{uid} successfully generated.\n" ;
}

H2. TODO

  • Trouver une extension libre si celle ci n'est pas mentionnée.

  • Option pour parser LDAP

  • Liste les utilisateurs si aucun argument

  • Option R pour supprimmer un ou des utilisateurs

  • Demander confirmation avant ecrasement si un utilisateur existe deja. Option F pour passer outre et forcer.

Mis à jour par francois-xavier.lesaffre il y a plus de 12 ans · 15 révisions