I have a script that I would like to make a slight change to. Currently, it is doing almost exactly what I want, however, at the beginning and end of it I want to add an open and close and am not sure how to do it.
If someone can point me in the right direction, as to what minor changes I would have to make to add the header/trailer it would be greatly appreciated!
This is the script:
---------------------------------------------------------
use strict;
my $infile = 'traps.mib'; # 'c:\Test\TRAPS.mib';
open(INFILE, "$infile") or die "Can't open $infile: $!";
open(OUTFILE, ">snmptd.cfx") or die "Can't Open snmptd.cfx";
while (<INFILE>) {
# This line grabs each TRAP-TYPE object within the specified MIB/infile.
if (/^(\w+)\s+(?:NOTIFICATION|TRAP)-TYPE/) {
my $object = $1;
# Get Entire Record.
my $record = $_;
until (/^\s*$/ || eof(INFILE)) {
$record .= $_ = <INFILE>;
}
# Parse for Data
my $variables = '';
if ($record =~ /^\s+VARIABLES\s+{\s+(.*?)\s+}$/m) {
$variables = $1;
} else {
print OUTFILE "Error: Variables not found in record - $record";
}
my $description = '';
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/m) {
$description = $1;
} else {
print OUTFILE "Error: Description not found in record - $record";
}
my $trapid = '';
if ($record =~ /^\s+::= (\d+)/m) {
$trapid = $1;
} else {
print OUTFILE "Error: Trapid not found in record - $record";
}
# Output results.
print OUTFILE <<OUT
<$object>
trap_type = 6
specific_trap = $trapid
<alarm>
active = yes
message = $description
severity = 2
subsystem = NORTEL
suppkey = $object
</alarm>
convert_trap = no
log_trap = no
</$object>
OUT
}
}
close INFILE;
1;
__END__
---------------------------------------------------------
If someone can point me in the right direction, as to what minor changes I would have to make to add the header/trailer it would be greatly appreciated!
This is the script:
---------------------------------------------------------
use strict;
my $infile = 'traps.mib'; # 'c:\Test\TRAPS.mib';
open(INFILE, "$infile") or die "Can't open $infile: $!";
open(OUTFILE, ">snmptd.cfx") or die "Can't Open snmptd.cfx";
while (<INFILE>) {
# This line grabs each TRAP-TYPE object within the specified MIB/infile.
if (/^(\w+)\s+(?:NOTIFICATION|TRAP)-TYPE/) {
my $object = $1;
# Get Entire Record.
my $record = $_;
until (/^\s*$/ || eof(INFILE)) {
$record .= $_ = <INFILE>;
}
# Parse for Data
my $variables = '';
if ($record =~ /^\s+VARIABLES\s+{\s+(.*?)\s+}$/m) {
$variables = $1;
} else {
print OUTFILE "Error: Variables not found in record - $record";
}
my $description = '';
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/m) {
$description = $1;
} else {
print OUTFILE "Error: Description not found in record - $record";
}
my $trapid = '';
if ($record =~ /^\s+::= (\d+)/m) {
$trapid = $1;
} else {
print OUTFILE "Error: Trapid not found in record - $record";
}
# Output results.
print OUTFILE <<OUT
<$object>
trap_type = 6
specific_trap = $trapid
<alarm>
active = yes
message = $description
severity = 2
subsystem = NORTEL
suppkey = $object
</alarm>
convert_trap = no
log_trap = no
</$object>
OUT
}
}
close INFILE;
1;
__END__
---------------------------------------------------------