diff options
author | mark <mark> | 2011-10-31 19:20:16 +0000 |
---|---|---|
committer | mark <mark> | 2011-10-31 19:20:16 +0000 |
commit | 2ad6569982365759d7baaf5a97bc836770a54291 (patch) | |
tree | 81c72ce1e7687e27ea1f727643f659fdb6335793 /bin | |
parent | 588ac92d054671aa13cb61f76b5da472661cb1ac (diff) |
export NAS table to sqlradius, #14697
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/clients.conf.import | 91 | ||||
-rwxr-xr-x | bin/sqlradius-nas.import | 67 |
2 files changed, 158 insertions, 0 deletions
diff --git a/bin/clients.conf.import b/bin/clients.conf.import new file mode 100755 index 000000000..16aac4b27 --- /dev/null +++ b/bin/clients.conf.import @@ -0,0 +1,91 @@ +#!/usr/bin/perl -w + +use strict; +use DBI; +use FS::UID qw(adminsuidsetup); #datasrc +use FS::Record qw(qsearch qsearchs dbh); +use FS::nas; +use FS::export_nas; +use FS::part_export; + +my $user = shift or die &usage; +my $filename = shift or die &usage; +my $all_nas = []; + +my $client; +my $in; +open ($in, '<', $filename) or die "can't open $filename for reading\n"; +my $i = 0; +while (my $line = <$in>) { + $i++; + $line =~ s/#.*//; + my @t = grep $_, split(/\s+/, $line); + next if !@t; + if ( $client ) { + if ( $t[0] eq 'ipaddr' ) { + $client->{nasname} = $t[2]; + } + elsif ( $t[0] eq 'secret' ) { + $client->{secret} = $t[2]; + } + elsif( $t[0] eq 'shortname' ) { + $client->{shortname} = $t[2]; + } + elsif( $t[0] eq 'nastype' ) { + $client->{type} = $t[2]; + } + elsif( $t[0] eq 'virtual_server' ) { + $client->{server} = $t[2]; + } + elsif( $t[0] eq '}' ) { + $client->{description} = $client->{shortname}; + push @$all_nas, $client; + undef $client; + } + else { + warn "unknown parameter '$t[0]' (line $i), skipped\n"; + next; + } + } + else { # not in a client section + die "parse error (line $i)\n" if $t[0] ne 'client' or $t[2] ne '{'; + $client = { nasname => $t[1], + shortname => $t[1] }; # hostname + } +} +close $in; + +warn scalar(@$all_nas)." records found.\n"; + +adminsuidsetup $user; + +$FS::UID::AutoCommit = 0; +my $dbh = dbh; + +# cache NAS names we already know about, and don't import them +my %existing_names = map { $_->nasname , $_->nasnum } qsearch('nas', {}); + +my $inserted = 0; +foreach my $row (@$all_nas) { + my %hash = %$row; + if (my $num = $existing_names{ $hash{nasname} }) { + warn "NAS $hash{nasname} already exists as #$num (skipped)\n"; + } + else { + my $nas = FS::nas->new(\%hash); + my $error = $nas->insert; + if ( $error ) { + $dbh->rollback; + die "error inserting $hash{nasname}: $error (changes reverted)\n"; + } + $inserted++; + } +} #foreach $row + +warn "Inserted $inserted NAS records.\n\n"; +$dbh->commit; + +sub usage { + die "Usage:\n\n clients.conf.import user filename\n\n"; +} + diff --git a/bin/sqlradius-nas.import b/bin/sqlradius-nas.import new file mode 100755 index 000000000..0583272db --- /dev/null +++ b/bin/sqlradius-nas.import @@ -0,0 +1,67 @@ +#!/usr/bin/perl -w + +use strict; +use DBI; +use FS::UID qw(adminsuidsetup); #datasrc +use FS::Record qw(qsearch qsearchs dbh); +use FS::nas; +use FS::export_nas; +use FS::part_export; + +my $user = shift or die &usage; +adminsuidsetup $user; + +$FS::export_nas::noexport_hack = 1; +$FS::UID::AutoCommit = 0; +my $dbh = dbh; + +my $exportnum = shift or die &usage; +my $part_export = qsearchs('part_export', { exportnum => $exportnum }) + or die "export $exportnum not found.\n"; + +$part_export->isa('FS::part_export::sqlradius') + or die "export $exportnum is not an sqlradius export.\n"; + +my $raddbh = DBI->connect( + $part_export->option('datasrc'), + $part_export->option('username'), + $part_export->option('password') +); + +# cache NAS names we already know about, and don't import them +my %existing_names = map { $_->nasname , $_->nasnum } qsearch('nas', {}); + +my @fields = (qw( nasname shortname type secret server community description )); +my $sql = 'SELECT '.join(', ',@fields).' FROM nas'; +my $all_nas = $raddbh->selectall_arrayref($sql) + or die "unable to retrieve NAS records: ".$dbh->errstr."\n"; + +warn scalar(@$all_nas)." records found.\n"; +my $inserted = 0; +foreach my $row (@$all_nas) { + my %hash; + @hash{@fields} = @$row; + if (my $num = $existing_names{ $hash{nasname} }) { + warn "NAS $hash{nasname} already exists as #$num (skipped)\n"; + } + else { + my $nas = FS::nas->new(\%hash); + my $error = $nas->insert + || $nas->process_m2m(link_table => 'export_nas', + target_table => 'part_export', + params => [ $exportnum ]); + if ( $error ) { + $dbh->rollback; + die "error inserting $hash{nasname}: $error (changes reverted)\n"; + } + $inserted++; + } +} #foreach $row + +warn "Inserted $inserted NAS records.\n\n"; +$dbh->commit; + +sub usage { + die "Usage:\n\n sqlradius-nas.import user exportnum\n\n"; +} + |