Dades en objects.csv
Xarxes alumnes,Aula Idiomes ,ipaddr,192.168.0.0,24 ,
Xarxes alumnes,Xarxa educat1x1 ,ipaddr,192.168.168.0,21 ,
Xarxa docent profes No proxy,Profes docent 130 ,ipaddr,192.168.130.0,24 ,
AulaIdiomes,aula Idiomes ,iprange,192.168.0.101,192.168.0.125,
AulaIdiomes,server-idiomes ,ipaddr,192.168.0.249,32 ,
Informatica Alumnes,Informatica ,ipaddr,192.168.2.0,24 ,
No proxy alumnes,no_proxy,iprange,192.168.0.150,192.168.0.200,
Reserves docent,laptop-alex ,ipaddr,192.168.130.10,32 ,00:00:00:01:01:02
Reserves docent,alumneHector2012 ,ipaddr,192.168.130.100,32 ,00:00:00:01:01:01
Script addNetObjects.pl
#chmod +x addNetObjects.pl
#./addNetObjects.pl
#!/usr/bin/perl
use strict;
use warnings;
use EBox;
use EBox::Global;
use EBox::Objects;
use Scalar::Util;
EBox::init();
open (my $OBJECTS, 'objects.csv');
# File objects.csv format "objecte name", "member name", "member type", "member ip", "member mac o ip"
# types : ipaddr | iprange
my $module = EBox::Global->getInstance(0)->modInstance(qw(objects));
if (not $module) {
EBox::error(qw(objects) . "has impot data but it is not installed, not importing");
return;
}
my @members;
my $object = '';
while (my $line = <$OBJECTS>) {
chomp ($line);
my $member;
my ($objectname, $membername, $membertype, $memberip, $memberipormask, $membermac) = split(',', $line);
$membername =~ s/^\s+|\s+$//g;
$membertype =~ s/^\s+|\s+$//g;
$memberip =~ s/^\s+|\s+$//g;
$memberipormask =~ s/^\s+|\s+$//g;
if ($membertype eq 'ipaddr') {
if ($membermac) {
$membermac =~ s/^\s+|\s+$//g;
$member = { 'name' => $membername,
'address_selected' => $membertype,
'ipaddr_ip' => $memberip,
'ipaddr_mask' => $memberipormask,
'macaddr' => $membermac };
} else {
$member = { 'name' => $membername,
'address_selected' => $membertype,
'ipaddr_ip' => $memberip,
'ipaddr_mask' => $memberipormask };
}
} else {
$member = { 'name' => $membername,
'address_selected' => $membertype,
'iprange_begin' => $memberip,
'iprange_end' => $memberipormask };
}
if ($object ne $objectname) {
# canvi objecte
if (@members) {
# Some member exist. Not first loop
print "Adding $object...\n";
$module->addObject('name' => $object, 'members' => \@members);
@members = ();
}
$object = $objectname;
}
push(@members, $member);
}
print "Adding $object...\n";
$module->addObject('name' => $object, 'members' => \@members);
close ($OBJECTS);
1;
Leave a Reply