Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a header and trailer... 2

Status
Not open for further replies.

CTSQLGUY

Technical User
Feb 4, 2007
33
US
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__
---------------------------------------------------------
 
Based off your original post, the following things are all you're trying to add:


Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$infile[/blue] = [red]'[/red][purple]traps.mib[/purple][red]'[/red][red];[/red] [gray][i]# 'c:\Test\TRAPS.mib';[/i][/gray]
[black][b]my[/b][/black] [blue]$outfile[/blue] = [red]'[/red][purple]snmptd.cfx[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]INFILE, [red]"[/red][purple][blue]$infile[/blue][/purple][red]"[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$infile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red]OUTFILE, [red]"[/red][purple]> [blue]$outfile[/blue][/purple][red]"[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$outfile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[gray][i]# Header[/i][/gray]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUTFILE <<OUT
<profiles> overwrite
   <[fuchsia].1.3.6.1.4.1.2636[/fuchsia]>
   name = Nortel Contivity TrapsV1
   active = yes
OUT

[olive][b]while[/b][/olive] [red]([/red]<INFILE>[red])[/red] [red]{[/red]
	[gray][i]# This line grabs each TRAP-TYPE object within the specified MIB/infile.[/i][/gray]
	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^([purple][b]\w[/b][/purple]+)[purple][b]\s[/b][/purple]+(?:NOTIFICATION|TRAP)-TYPE[/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]my[/b][/black] [blue]$object[/blue] = [blue]$1[/blue][red];[/red]

		[gray][i]# Get Entire Record.[/i][/gray]
		[black][b]my[/b][/black] [blue]$record[/blue] = [blue]$_[/blue][red];[/red]
		[olive][b]until[/b][/olive] [red]([/red][red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red] || [url=http://perldoc.perl.org/functions/eof.html][black][b]eof[/b][/black][/url][red]([/red]INFILE[red])[/red][red])[/red] [red]{[/red]
			[blue]$record[/blue] .= [blue]$_[/blue] = <INFILE>[red];[/red]
		[red]}[/red]

		[gray][i]# Parse for Data[/i][/gray]
		[black][b]my[/b][/black] [blue]$variables[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
		[olive][b]if[/b][/olive] [red]([/red][blue]$record[/blue] =~ [red]/[/red][purple]^[purple][b]\s[/b][/purple]+VARIABLES[purple][b]\s[/b][/purple]+{[purple][b]\s[/b][/purple]+(.*?)[purple][b]\s[/b][/purple]+}$[/purple][red]/[/red][red]m[/red][red])[/red] [red]{[/red]
			[blue]$variables[/blue] = [blue]$1[/blue][red];[/red]
		[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
			[black][b]print[/b][/black] OUTFILE [red]"[/red][purple]Error: Variables not found in record - [blue]$record[/blue][/purple][red]"[/red][red];[/red]
		[red]}[/red]

		[black][b]my[/b][/black] [blue]$description[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
		[olive][b]if[/b][/olive] [red]([/red][blue]$record[/blue] =~ [red]/[/red][purple]^[purple][b]\s[/b][/purple]+DESCRIPTION[purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]+"(.*?)"$[/purple][red]/[/red][red]m[/red][red])[/red] [red]{[/red]
			[blue]$description[/blue] = [blue]$1[/blue][red];[/red]
		[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
			[black][b]print[/b][/black] OUTFILE [red]"[/red][purple]Error: Description not found in record - [blue]$record[/blue][/purple][red]"[/red][red];[/red]
		[red]}[/red]

		[black][b]my[/b][/black] [blue]$trapid[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
		[olive][b]if[/b][/olive] [red]([/red][blue]$record[/blue] =~ [red]/[/red][purple]^[purple][b]\s[/b][/purple]+::= ([purple][b]\d[/b][/purple]+)[/purple][red]/[/red][red]m[/red][red])[/red] [red]{[/red]
			[blue]$trapid[/blue] = [blue]$1[/blue][red];[/red]
		[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
			[black][b]print[/b][/black] OUTFILE [red]"[/red][purple]Error: Trapid not found in record - [blue]$record[/blue][/purple][red]"[/red][red];[/red]
		[red]}[/red]

		[gray][i]# Output results.[/i][/gray]
		[black][b]print[/b][/black] OUTFILE <<OUT
	  <[blue]$object[/blue]>
		 trap_type = [fuchsia]6[/fuchsia]
		 specific_trap = [blue]$trapid[/blue]
		 <[url=http://perldoc.perl.org/functions/alarm.html][black][b]alarm[/b][/black][/url]>
			active = yes
			message = [blue]$description[/blue]
			severity = [fuchsia]2[/fuchsia]
			subsystem = NORTEL
			suppkey = [blue]$object[/blue]
		 <[red]/[/red][purple]alarm>[/purple]
[purple]		 convert_trap = no[/purple]
[purple]		 log_trap = no[/purple]
[purple]	  <[/purple][red]/[/red][blue]$object[/blue]>
OUT
	[red]}[/red]
[red]}[/red]

[gray][i]# Footer[/i][/gray]
[black][b]print[/b][/black] OUTFILE <<OUT
   <[red]/[/red][purple].1.3.6.1.4.1.2636>[/purple]
[purple]<[/purple][red]/[/red]profiles>
OUT

[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] INFILE[red];[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
Thanks Miller, but that script isn't outputting anything for me... I'm getting the following error via command:

I've gone through the script and I'm not sure what the issue is... any idea?

Thanks!

------------------------------------------------------------
C:\Test>perfect.pl
syntax error at C:\Test\Perfect.pl line 17, near ") {"
Can't use global $1 in "my" at C:\Test\Perfect.pl line 20, near "= $1"
syntax error at C:\Test\Perfect.pl line 66, near "}"
(Might be a runaway multi-line << string starting on line 51)
Execution of C:\Test\Perfect.pl aborted due to compilation errors.
------------------------------------------------------------
 
on all the ines lik this:

print OUTFILE <<OUT

put a semi-colon on the end:

print OUTFILE <<OUT[red];[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top