Obsolete
/
dnsmanagerv1
Archived
3
0
Fork 0

exemples (ssh, scp) + tout se fait via ssh (prog externalisable)

master
Philippe Pittoli 2014-01-25 18:49:58 +01:00
parent 95d7368642
commit e51f9d89dc
9 changed files with 153 additions and 43 deletions

View File

@ -17,7 +17,7 @@ use Moose;
has dbh => ( is => 'rw', builder => '_void');
has dnsi => ( is => 'rw', builder => '_void');
has um => ( is => 'rw', builder => '_void');
has [ qw/zdir dbname dbhost dbport dbuser dbpass sgbd dnsapp/ ] => qw/is ro required 1/;
has [ qw/zdir dbname dbhost dbport dbuser dbpass sgbd dnsapp sshhost sshuser/ ] => qw/is ro required 1/;
sub _void { my $x = ''; \$x; }
### users
@ -91,7 +91,10 @@ sub add_domain {
return 0;
}
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
my $ze = app::zone::edit->new(zname => $domain
, zdir => $self->zdir
, host => $self->sshhost
, user => $self->sshuser );
$ze->addzone();
}
@ -103,7 +106,10 @@ sub delete_domain {
return 0 unless $success;
return 0 unless $user->delete_domain($domain);
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
my $ze = app::zone::edit->new(zname => $domain
, zdir => $self->zdir
, host => $self->sshhost
, user => $self->sshuser );
$ze->del();
1;
@ -111,19 +117,28 @@ sub delete_domain {
sub update_domain_raw {
my ($self, $login, $zone, $domain) = @_;
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
my $ze = app::zone::edit->new(zname => $domain
, zdir => $self->zdir
, host => $self->sshhost
, user => $self->sshuser );
$ze->update_raw($zone);
}
sub update_domain {
my ($self, $login, $zone, $domain) = @_;
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
my $ze = app::zone::edit->new(zname => $domain
, zdir => $self->zdir
, host => $self->sshhost
, user => $self->sshuser );
$ze->update($zone);
}
sub get_domain {
my ($self, $login, $domain) = @_;
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
my $ze = app::zone::edit->new(zname => $domain
, zdir => $self->zdir
, host => $self->sshhost
, user => $self->sshuser );
$ze->get();
}
@ -146,7 +161,10 @@ sub get_all_users {
sub new_tmp {
my ($self, $login, $domain) = @_;
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
my $ze = app::zone::edit->new(zname => $domain
, zdir => $self->zdir
, host => $self->sshhost
, user => $self->sshuser );
$ze->new_tmp();
}

View File

@ -2,6 +2,8 @@ use Modern::Perl;
use Data::Dump "dump";
use DNS::ZoneParse;
use File::Copy;
use Net::SCP;
use Net::SSH2;
use v5.14;
use lib '../../';
@ -9,12 +11,15 @@ use app::zone::rndc_interface;
package app::zone::edit;
use Moose;
has [ qw/zname zdir/ ] => qw/is ro required 1/;
has [ qw/zname zdir host user/ ] => qw/is ro required 1/;
sub get {
my ($self) = @_;
my $dest = '/tmp/' . $self->zname;
my $file = $self->zdir.'/'.$self->zname;
return DNS::ZoneParse->new($file, $self->zname);
$self->_scp_get($file, $dest);
DNS::ZoneParse->new($dest, $self->zname);
}
=pod
@ -28,19 +33,24 @@ sub addzone {
my ($self) = @_;
my $tpl = $self->zdir."/tpl.zone";
my $file = $self->zdir.'/'.$self->zname;
my $tmpfile = '/tmp/'.$self->zname;
$self->_cp($tpl, $file);
$self->_scp_get($tpl, $tmpfile); # get the template
$self->_sed($tmpfile); # sed CHANGEMEORIGIN by the real origin
my $zonefile = DNS::ZoneParse->new($file, $self->zname);
my $zonefile = DNS::ZoneParse->new($tmpfile, $self->zname);
$zonefile->new_serial(); # update the serial number
# write the new zone file to disk
# write the new zone tmpfile to disk
my $newzone;
open($newzone, '>', $file) or die "error";
open($newzone, '>', $tmpfile) or die "error";
print $newzone $zonefile->output();
close $newzone;
my $file = $self->zdir.'/'.$self->zname;
$self->_scp_put($tmpfile, $file); # put the final zone on the server
unlink($tmpfile); # del the temporary file
my $rndc = app::zone::rndc_interface->new();
$rndc->addzone($self->zdir, $self->zname);
@ -58,16 +68,21 @@ sub update {
# update the serial number
$zonefile->new_serial();
my $file = $self->zdir.'/'.$self->zname;
my $tmpfile = '/tmp/' . $self->zname;
# write the new zone file to disk
# write the new zone tmpfile to disk
my $newzone;
open($newzone, '>', $file) or die "error";
open($newzone, '>', $tmpfile) or die "error";
print $newzone $zonefile->output();
close $newzone;
my $file = $self->zdir.'/'.$self->zname;
$self->_scp_put($tmpfile, $file); # put the final zone on the server
unlink($tmpfile); # del the temporary file
my $rndc = app::zone::rndc_interface->new();
$rndc->reload($self->zname);
1;
}
=pod
@ -96,7 +111,6 @@ sub update_raw {
unlink($file);
$self->update($zonefile);
1;
}
# sera utile plus tard, pour l'interface
@ -106,7 +120,9 @@ sub new_tmp {
my $tpl = $self->zdir."/tpl.zone";
my $file = '/tmp/'.$self->zname;
$self->_cp($tpl, $file);
$self->_scp($tpl, $file);
$self->_sed($file);
my $zonefile = DNS::ZoneParse->new($file, $self->zname);
$zonefile->new_serial(); # update the serial number
@ -119,9 +135,27 @@ sub _cp {
my ($self, $src, $dest) = @_;
File::Copy::copy($src, $dest) or die "Copy failed: $! ($src -> $dest)";
}
sub _scp_put {
my ($self, $src, $dest) = @_;
my $scp = Net::SCP->new( { host => $self->host, user => $self->user } );
$scp->put($src, $dest) or die $scp->{errstr};
}
sub _scp_get {
my ($self, $src, $dest) = @_;
my $scp = Net::SCP->new( { host => $self->host, user => $self->user } );
$scp->get($src, $dest) or die $scp->{errstr};
}
sub _sed {
my ($self, $file) = @_;
my $orig = $self->zname;
my $cmd = qq[sed -i "s/CHANGEMEORIGIN/$orig/" $dest 2>/dev/null 1>/dev/null];
my $cmd = qq[sed -i "s/CHANGEMEORIGIN/$orig/" $file 2>/dev/null 1>/dev/null];
system($cmd);
}
@ -130,6 +164,17 @@ sub del {
my $rndc = app::zone::rndc_interface->new();
$rndc->delzone($self->zdir, $self->zname);
$rndc->reconfig();
my $ssh = Net::SSH2->new();
$ssh->connect($self->host);
$ssh->auth( username => $self->user);
my $chan = $ssh->channel();
my $file = $self->zdir.'/'.$self->zname;
$chan->exec( "rm $file" );
$ssh->disconnect();
1;
}
1;

View File

@ -33,7 +33,6 @@ sub reconfig {
sub delzone {
my ($self, $zdir, $zname) = @_;
system("rndc delzone $zname 2>/dev/null 1>/dev/null");
unlink("$zdir/$zname");
}
1;

View File

@ -9,3 +9,5 @@ passwd = HardPass4bla
# possible options for dnsserver : bind rndc
dnsapp = rndc
zones_path = "/srv/named/"
sshhost = pizza
sshuser = karchnu

View File

@ -3,9 +3,7 @@ use warnings;
use v5.14;
use autodie;
use Modern::Perl;
use DNS::ZoneParse;
use Config::Simple;
use Data::Dump qw( dump );
package initco;
@ -15,14 +13,16 @@ sub initco {
$cfgfile = defined $cfgfile ? $cfgfile : './config.ini';
my $cfg = new Config::Simple($cfgfile);
my $app = app->new( zdir => $cfg->param('zones_path'),
dbname => $cfg->param('dbname'),
dbhost => $cfg->param('host'),
dbport => $cfg->param('port'),
dbuser => $cfg->param('user'),
dbpass => $cfg->param('passwd'),
sgbd => $cfg->param('sgbd'),
dnsapp => $cfg->param('dnsapp') );
my $app = app->new( zdir => $cfg->param('zones_path')
, dbname => $cfg->param('dbname')
, dbhost => $cfg->param('host')
, dbport => $cfg->param('port')
, dbuser => $cfg->param('user')
, dbpass => $cfg->param('passwd')
, sgbd => $cfg->param('sgbd')
, sshhost => $cfg->param('sshhost')
, sshuser => $cfg->param('sshuser')
, dnsapp => $cfg->param('dnsapp') );
$app->init();

16
t/scp.pl Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.14;
use autodie;
use Modern::Perl;
use Net::SCP;
my $hostname = "pizza";
my $username = "karchnu";
my $scp = Net::SCP->new( { host => $hostname, user => $username } );
$scp->get("/etc/resolv.conf", "kikoo") or die $scp->{errstr};
$scp->put("kikoo", "lolwat") or die $scp->{errstr};
# $scp->put("filename") or die $scp->{errstr};

26
t/ssh.pl Normal file
View File

@ -0,0 +1,26 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.14;
use autodie;
use Modern::Perl;
use Data::Dump qw( dump );
use DNS::ZoneParse;
my $hostname = "pizza";
my $username = "karchnu";
use Net::SSH2;
my $ssh = Net::SSH2->new();
$ssh->connect($hostname);
$ssh->auth( username => $username);
my $chan = $ssh->channel();
$chan->exec('ls /');
my $buf = '';
say $buf while $chan->read($buf, 1500);
$ssh->disconnect();

View File

@ -16,3 +16,6 @@ passwd = HardPass4bla
# possible options for dnsserver : bind rndc
dnsapp = rndc
zones_path = "/etc/bind/named/"
sshhost = localhost
sshuser = karchnu

View File

@ -23,14 +23,16 @@ our $VERSION = '0.1';
sub initco {
my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini');
my $app = app->new( zdir => $cfg->param('zones_path'),
dbname => $cfg->param('dbname'),
dbhost => $cfg->param('host'),
dbport => $cfg->param('port'),
dbuser => $cfg->param('user'),
dbpass => $cfg->param('passwd'),
sgbd => $cfg->param('sgbd'),
dnsapp => $cfg->param('dnsapp') );
my $app = app->new( zdir => $cfg->param('zones_path')
, dbname => $cfg->param('dbname')
, dbhost => $cfg->param('host')
, dbport => $cfg->param('port')
, dbuser => $cfg->param('user')
, dbpass => $cfg->param('passwd')
, sgbd => $cfg->param('sgbd')
, sshhost => $cfg->param('sshhost')
, sshuser => $cfg->param('sshuser')
, dnsapp => $cfg->param('dnsapp') );
$app->init();
@ -224,11 +226,10 @@ prefix '/domain' => sub {
}
$zone->new_serial();
my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini');
my $ed = app::zone::edit->new(zdir=>$cfg->param('zones_path')
, zname => param('domain'));
$app->update_domain( session('login')
, $zone
, param('domain'));
$ed->update($zone);
redirect '/domain/details/'.param('domain');
}