my($self, $error) = @_;
clear_errors unless $self->{Errors};
push(@{$self->{Errors}}, $error);
trace("Always", "ERROR[" . scalar(@{$self->{Errors}}) . "]: $error\n");
}
sub getErrors
{
my($self) = @_;
return $self->{Errors};
}
sub getFirstError
{
my($self) = @_;
return $self->{Errors}[0] if $self->{Errors};
return;
}
1;
__END__
=head1 NAME
JUNOS::Device - Implements a remote JUNOScript device
=head1 SYNOPSIS
Here is example that makes a telnet connection to router11, then updates the router11's
configuration with the configuration from $xmlfile. It also deals with
error conditions and gracefully shuts down the telnet session.
use JUNOS::Device;
sub graceful_shutdown
{
my ($jnx, $req, $state, $success) = @_;
if ($state >= STATE_CONFIG_LOADED) {
print "Rolling back configuration ...\n";
$jnx->load_configuration(rollback => 0);
}
if ($state >= STATE_LOCKED) {
print "Unlocking configuration database ...\n";
$jnx->unlock_configuration();
}
if ($state >= STATE_CONNECTED) {
print "Disconnecting from the router ...\n";
$jnx->request_end_session();
$jnx->disconnect();
}
if ($success) {
die "REQUEST $req SUCCEEDED\n";
} else {
die "REQUEST $req FAILED\n";
}
}
$jnx = new JUNOS::Device(hostname => "router11",
login => "johndoe",
password => "secret",
access => "telnet");
unless ( ref $jnx ) {
die "ERROR: can't connect to $deviceinfo{hostname}.\n";
}
print "Locking configuration database ...\n";
my $res = $jnx->lock_configuration();
my $err = $res->getFirstError();
if ($err) {
print "ERROR: $deviceinfo{hostname}: can't lock configuration. Reason: $err->{message}.\n";
graceful_shutdown($jnx, $xmlfile, STATE_CONNECTED, REPORT_FAILURE);
}
#
# Load the configuration
#
print "Loading configuration from $xmlfile ...\n";
if (! -f $xmlfile) {
print "ERROR: Cannot load configuration in $xmlfile\n";
graceful_shutdown($jnx, $xmlfile, STATE_LOCKED, REPORT_FAILURE);
}
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile($xmlfile);
unless ( ref $doc ) {
print "ERROR: Cannot parse $xmlfile, check to make sure the XML data is well-formed\n";
graceful_shutdown($jnx, $xmlfile, STATE_LOCKED, REPORT_FAILURE);
}
$res = $jnx->load_configuration(configuration => $doc);
unless ( ref $res ) {
print "ERROR: can't load the configuration from $xmlfile\n";
=6= |