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!

Perl Script for MIB Conversion... 2

Status
Not open for further replies.

CTSQLGUY

Technical User
Feb 4, 2007
33
US
I have a script that takes MIB's and converts them to a CFX file, the file is something readable by a program I'm using for SNMP management

The script is a work in progress, the majority of it is done but I need some assistance to finish it...

The script does exactly what I want except right now it's not outputting the file, I'm forced to run it via command using:

script.pl >C:\Test\Test.CFX

I would love to be able to include it in the script - or better yet have it prompt me for the name!

Additionally, the other headache I've run into is with SNMP 2.0. MIB's no longer use TRAP-TYPE to identify traps, they now use NOTIFICATION-TYPE. Does someone know how I could say to look for either/or?

Any assistance would be greatly appreciated!

Thanks!

*********************************************************
$infile = 'c:\Test\TRAPS.mib';

#Outfile is not currently used - use file redirect when running script.
#$outfile = 'C:\Test\Test.CFX';

$inprocess = 0;

open (INFILE, "$infile") || die;
#open (OUTFILE, ">$outfile") || die;

writeheader();

while (<INFILE>) {

#
# This line grabs each TRAP-TYPE object within the specified MIB/infile.
#
if ($_ =~/[a-zA-Z0-9].* TRAP-TYPE/) {
if ($inprocess) {
punchout();
}
$object = $_;
chomp ($object);
$inprocess = 1;
next;
}

#
# This line grabs each trapID within the specified MIB/infile.
#
if ($_ =~/::= ([0-9]).*/) {
$trapid = $_;
chomp ($trapid);
next;
}

#
# This line grabs each ARGUMENT within the specified MIB/infile.
#
if ($_ =~/--#ARGUMENTS ([0-9,\{ ]).*/) {
$arg = $_;
chomp ($arg);
next;
}

#
# This line grabs each DESCRIPTION within the specified MIB/infile. Good for alert "message" content...
#
if ($_ =~/DESCRIPTION/) {
$descr = <INFILE>;
chomp ($descr);
next;
}
}
writeTrailer();

#
# Write one Trap Configuration Record to the output.
#
sub punchout {

@object = ($object =~ /(\w+)/g);
$object = @object[0];
print " <$object>\n";
print " trap_type = 6\n";
@trapid = ($trapid =~ /(\w+)/g);
print " specific_trap = @trapid[0]\n";
print " <alarm>\n";
print " active = yes\n";
$descr =~ s/"//g;
$descr =~ s/^\s*//;
print " message = $descr\n";
print " severity = 2\n";
print " subsystem = NORTEL\n";
print " suppkey = $object\n";
print " </alarm>\n";
print " convert_trap = no\n";
print " log_trap = no\n";
print " </$object>\n";
}


#
# Start by writing section header
#
sub writeheader {
print "<profiles> overwrite\n";
print " <.1.3.6.1.4.1.2636>\n";
print " name = Nortel Contivity TrapsV1\n";
print " active = yes\n";

}

# End by writing section trailer
#
sub writeTrailer {
print " </.1.3.6.1.4.1.2636>\n";
print "</profiles>\n";
}
*********************************************************
 
Your OUTFILE line looks correct you just need to change all your print statements to print OUTFILE "blah" and add a close OUTFILE; somewhere at the end there.

You could prompt for input using <STDIN> .

As for your reg ex's.. I think it would be

(TRAP|NOTIFICATION)-TYPE

but I am definitely the kind of person that has to research my reg expressions before using them.. I'm sure Kevin or Miller or someone knows it off the top of their head.
 
travs69 is right concerning both the printing and the regex. However, I suggest that you use the non-capturing version

(?:TRAP|NOTIFICATION)-TYPE

I also, I would strongly advise you to clean up your code. First off, you need to use "use strict;" at the top of all your scripts. This will currently throw massive amounts of errors as you have not declared a single variable. It will also encourage you to change the function call so that it accepts parameters instead of relying on globals.

Anyway, I could actually do this really quickly for you, but without knowing the format of your data file it's a little difficult.

Good luck,
- Miller
 
Miller can you tell me the difference between your regexp and mine?
 
Thanks :) I really need to get my head wrapped around regular expressions better!
 
Wow, thanks a ton for the help! If you can give me any additional help it would be a huge bonus!

This is a standard data file:

***********************************************
-- These MIBs were created on 08/02/2000
--
-- Copyright (c) 1999-2004, Juniper Networks, Inc.
-- All rights reserved.
--
-- MODULE-IDENTITY
-- OrgName
-- Juniper Networks, Inc.
-- ContactInfo
-- Customer Support
--
-- 1194 North Mathilda Avenue
-- Sunnyvale, California 94089-1206
-- USA
--
-- Tel: 1-800-638-8296
-- E-mail: customerservice@juniper.net
-- --
-- Added 4 new trap types - ipv6 ip conflicts(101), dip util raise(102) and clear(103),
-- ids-icmp-ping-id-zero(441).
-- Last modified date: 03/03/2005
-- Modified copyright and contact info
-- Last modified date: 05/03/2004
-- Add global-report manager specific trap
-- Last modified date: 09/28/2001
-- Add new traps (430~434)
-- Last modified date: 1/23/2004
-- Add traffic traps (1,2) and route traps (205~225)
-- Last modified date: 3/24/2004
--

NETSCREEN-TRAP-MIB DEFINITIONS ::= BEGIN
IMPORTS
enterprises FROM RFC1155-SMI
DisplayString FROM RFC1213-MIB
netscreen, netscreenTrapInfo FROM NETSCREEN-SMI;

netscreenTrapType OBJECT-TYPE
SYNTAX INTEGER {
traffic-sec(1), -- Traffic per-second threshold
traffic-min(2), -- Traffic per-minute threshold
user-auth-fail(3), -- User Authentication Fail
winnuke(4), -- Winnuke pak
syn-attack(5), -- Syn attack
tear-drop(6), -- tear-drop attack
ping-death(7), -- Ping of Death attack
ip-spoofing(8), -- IP spoofing attack
ip-src-route(9), -- IP source routing attack
land(10), -- land attack
icmp-flood(11), -- ICMP flooding attack
udp-flood(12), -- UDP flooding attack

illegal-cms-svr(13), -- Illegal server IP to connect to CMS port
url-block-srv(14), -- URL blocking server connection alarm
high-availibility(15),-- HA alarm

port-scan(16), -- Port Scan attack
addr-sweep(17), -- address sweep attack
policy-deny(18), -- Deny by policy attack

device-dead(19), -- device not working
low-memory(20), -- memory low

dns-srv-down(21), -- DNS server unreachable
generic-HW-fail(22), -- Fan, Power Supply failure
lb-srv-down(23), -- Load balance server unreachable
log-full(24), -- log buffer overflow
x509(25), -- X509 related
vpn-ike(26), -- VPN and IKE related
admin(27), -- admin realted
sme(28), -- Illegal src ip to connect to sme port
dhcp(29), -- DHCP related
cpu-usage-high(30), -- CPU usage is high
ip-conflict(31), -- Interface IP conflict
attact-malicious-url(32), -- Microsoft IIS server vulnerability
session-threshold(33), -- session threshold is exceeded
ssh-alarm(34), -- SSH related alarms
vpn-tunnel-up(40), -- VPN tunnel from down to up
vpn-tunnel-down(41), -- VPN tunnel from up to down
vpn-replay-attack(42), -- VPN replay detected
vpn-l2tp-tunnel-remove(43), -- VPN tunnel removed
vpn-l2tp-tunnel-remove-err(44), -- VPN tunnel removed and error detected
vpn-l2tp-call-remove(45), -- VPN call removed
vpn-l2tp-call-remove-err(46), -- VPN call removed and error detected
vpn-ias-too-many(47), -- Number of IAS exceeds configured maximum
vpn-ias-over-threshold(48), -- Number of IAS crossed configured upper threshold
vpn-ias-under-threshold(49), -- Number of IAS crossed configured lower threshold
vpn-ias-ike-error(50), -- IKE error occured for the IAS session
allocated-session-threshold(51), -- allocated session exceed threshold
nsrp-rto-up(60), -- NSRP rto self unit status change from up to down
nsrp-rto-down(61), -- NSRP rto self unit status change from down to up
nsrp-trackip-success(62), -- NSRP track ip successed
nsrp-trackip-failed(63), -- NSRP track ip failed
nsrp-trackip-failover(64), -- NSRP track ip fail over
nsrp-inconsistent-configuration(65), -- NSRP inconsistent configuration between master and backup
nsrp-vsd-init(70), -- NSRP vsd group status change to elect
nsrp-vsd-master(71), -- NSRP vsd group status change to master
nsrp-vsd-pbackup(72), -- NSRP vsd group status change to primary backup
nsrp-vsd-backup(73), -- NSRP vsd group status change to backup
nsrp-vsd-ineligible(74), -- NSRP vsd group status change to ineligible
nsrp-vsd-inoperable(75), -- NSRP VSD group status change to inoperable
nsrp-vsd-req-hearbeat-2nd(76), -- NSRP VSD request heartbeat from 2nd HA path
nsrp-vsd-reply-2nd(77), -- NSRP VSD reply to 2nd path request
nsrp-rto-duplicated(78), -- NSRP duplicated RTO group found


dip-util-raise(102), -- DIP utilization reaches raised threshold limit
dip-util-clear(103), -- DIP utilization reaches clear threshold limit
route-alarm(205), -- Errors in route module (exceed limit, malloc failure, add-perfix failure etc)

syn-frag-attack(412), -- screen syn fragment attack
tcp-without-flag(413), -- screen tcp packet without flag attack
unknow-ip-packet(414), -- screen unknown ip packet
bad-ip-option(415), -- screen bad ip option
dst-ip-session-limit(430), -- Dst IP-based session limiting
ids-block-zip(431), -- HTTP component blocking for .zip files
ids-block-jar(432), -- HTTP component blocking for Java applets
ids-block-exe(433), -- HTTP component blocking for .exe files
ids-block-activex(434), -- HTTP component blocking for ActiveX controls
icmp-fragment(435), -- screen icmp fragment packet
too-large-icmp(436),-- screen too large icmp packet
tcp-syn-fin(437), -- screen tcp flag syn-fin set
tcp-fin-no-ack(438), -- screen tcp fin without ack
ids-tcp-syn-ack-ack(439), -- avoid replying to syns after excessive 3 way TCP handshakes from same
-- src ip but not proceeding with user auth. (not replying to username/password)..
ids-ip-block-frag(440), -- ip fragment
ids-icmp-ping-id-zero(441), -- icmp ping id 0
link-statechange(1000) -- link state change
}
ACCESS not-accessible
STATUS mandatory
DESCRIPTION
"The integer value of the raised alarm type.
Note that the type should be interpreted within a specific trap"
::= { netscreenTrapInfo 1 }

netscreenTrapDesc OBJECT-TYPE
SYNTAX DisplayString (SIZE (0..255))
ACCESS not-accessible
STATUS mandatory
DESCRIPTION
"The textual description of the alarm"
::= { netscreenTrapInfo 3 }

netscreenTrapHw TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that some kind of hardware problem has occured."
::= 100

netscreenTrapFw TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that some kind of firewall functions has been triggered."
::= 200

netscreenTrapSw TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that some kind of software problem has occured."
::= 300

netscreenTrapTrf TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that some kind of traffic conditions has been triggered."
::= 400

netscreenTrapVpn TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that VPN tunnel status has occured."
::= 500

netscreenTrapNsrp TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that NSRP status has occured."
::= 600

netscreenTrapGPRO TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that some kind of Global PRO problems has occurred."
::= 700

netscreenTrapDrp TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that Drp status has occured."
::= 800

netscreenTrapIFFailover TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that interface fail over status has occured."
::= 900

netscreenTrapIDPAttack TRAP-TYPE
ENTERPRISE netscreen
VARIABLES { netscreenTrapType, netscreenTrapDesc }
DESCRIPTION
"This trap indicates that IDP attack status has occured."
::= 1000
END
***********************************************
 
Oh yeah, I forgot to mention, just copy the text I posted above and add it to a generic notepad file, save the file as example.mib and you're good to go!

Thanks again! :)
 
Given the data that you provided, I would advise you to change your parsing code to the following. You'll notice how I stripped out any of the formatting of the output, but that is easily added back in. Also, you previously were searching for ARGUMENTS instead of VARIABLES. I've fixed this, but don't know if this will actually serve a purpose in your output.

If you have any questions about why this is better, or how this works, feel free to ask. But I do strongly encourage you to parse in this way.

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]example.mib[/purple][red]'[/red][red];[/red] [gray][i]# 'c:\Test\TRAPS.mib';[/i][/gray]

[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]

[url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]$/[/blue] = [red]"[/red][purple][/purple][red]"[/red][red];[/red] [gray][i]# paragraph Separations[/i][/gray]

[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]
		
		[black][b]my[/b][/black] [blue]$variables[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
		[olive][b]if[/b][/olive] [red]([/red][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]
			[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Error: Variables not found in record - [blue]$_[/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][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] [red]"[/red][purple]Error: Description not found in record - [blue]$_[/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][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] [red]"[/red][purple]Error: Trapid not found in record - [blue]$_[/blue][/purple][red]"[/red][red];[/red]
		[red]}[/red]
		
		[gray][i]# Output results.[/i][/gray]
		[black][b]print[/b][/black] [red]<<OUT[/red][red];[/red]
[purple]    object = '[blue]$object[/blue]'[/purple]
[purple]    variables = '[blue]$variables[/blue]'[/purple]
[purple]    description = '[blue]$description[/blue]'[/purple]
[purple]    trapid = '[blue]$trapid[/blue]'[/purple]

[red]OUT[/red]
	[red]}[/red]
[red]}[/red]

[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]

Output:
Code:
>perl scratch.pl
    object = 'netscreenTrapHw'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that some kind of hardware problem has occured.'
    trapid = '100'

    object = 'netscreenTrapFw'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that some kind of firewall functions has been triggered.'
    trapid = '200'

    object = 'netscreenTrapSw'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that some kind of software problem has occured.'
    trapid = '300'

    object = 'netscreenTrapTrf'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that some kind of traffic conditions has been triggered.'
    trapid = '400'

    object = 'netscreenTrapVpn'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that VPN tunnel status has occured.'
    trapid = '500'

    object = 'netscreenTrapNsrp'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that NSRP status has occured.'
    trapid = '600'

    object = 'netscreenTrapDrp'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that Drp status has occured.'
    trapid = '800'

    object = 'netscreenTrapIFFailover'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that interface fail over status has occured.'
    trapid = '900'

    object = 'netscreenTrapIDPAttack'
    variables = 'netscreenTrapType, netscreenTrapDesc'
    description = 'This trap indicates that IDP attack status has occured.'
    trapid = '1000'

- Miller
 
Miller,

I noticed that this record is missing from the output:

Code:
netscreenTrapGPRO TRAP-TYPE
             ENTERPRISE netscreen
             VARIABLES  { netscreenTrapType, netscreenTrapDesc  }
             DESCRIPTION
                "This trap indicates that some kind of Global PRO problems has occurred."
             ::= 700

might be just a problem with the file that was copied into the thread or maybe from using $/ = "" as the input record seperator.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Good eye Kevin. Right you are.

For some reason there are 9 spaces on the blank line before that particular record. Now that could just be a copy and paste error. But if it's not, it's really easy to adapt the code to work under the new constraints.

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]example.mib[/purple][red]'[/red][red];[/red] [gray][i]# 'c:\Test\TRAPS.mib';[/i][/gray]

[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]

[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]
			[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [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] [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] [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] [red]<<OUT[/red][red];[/red]
[purple]	object = '[blue]$object[/blue]'[/purple]
[purple]	variables = '[blue]$variables[/blue]'[/purple]
[purple]	description = '[blue]$description[/blue]'[/purple]
[purple]	trapid = '[blue]$trapid[/blue]'[/purple]

[red]OUT[/red]
	[red]}[/red]
[red]}[/red]

[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]

This isn't as cute as using the paragraph separator, but it's probably good to show a different way of getting the entire record anyway.

Note: When tested it does get all records 100 - 1000.

- Miller
 
Thank you so much for the cleaned up script, however I'm still not sure how to make it so it automatically outputs the file to an SNMP.CFX filed...

Right now I run the script from command like this:

*****************************

script.pl > SNMP.CFX

*****************************

How do I make it do that within the script?

Even better... is there an easy way to set a little GUI for it, something that would have a basic Browse/Open File and then an output option, or would that be WAY TOO much effort?

If it's easy please point me in the right direction!

Thanks again for all the help everyone!
 
open(OUTFILE, "/path/to/snmp.cfx") or die "can't open /path/to/snmp.cx";

then every where you have print change it to print OUTFILE;

then at the end say close OUTFILE;

As for a little GUI.. I would assume TK would do it but I don't play with TK at all.
 
So it should look like this?

I'm receiving numerous errors...

**************************************************************
use strict;

my $infile = 'traps.mib'; # 'c:\Test\TRAPS.mib';

open(INFILE, "$infile") or die "Can't open $infile: $!";

open(OUTFILE, "c:\test\snmp.cfx") or die "can't open c:\test\snmp.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 {
OUTFILE "Error: Variables not found in record - $record";
}

my $description = '';
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/m) {
$description = $1;
} else {
OUTFILE "Error: Description not found in record - $record";
}

my $trapid = '';
if ($record =~ /^\s+::= (\d+)/m) {
$trapid = $1;
} else {
OUTFILE "Error: Trapid not found in record - $record";
}

# Output results.
OUTFILE <<OUT;
object = '$object'
variables = '$variables'
description = '$description'
trapid = '$trapid'

OUT
}
}

close INFILE;

1;

__END__
**************************************************************

 
Ummm... disregard, I re-read the original post - sorry and thanks for the help!
 
You need to do this:

open(OUTFILE, "c:/test/snmp.cfx") or die "can't open c:/test/snmp.cfx\n";
 
actualy you ned to do this:

Code:
open(OUTFILE, "[red]>[/red]c:/test/snmp.cfx") or die "can't open c:/test/snmp.cfx\n";

or:

Code:
open(OUTFILE, "[red]>>[/red]c:/test/snmp.cfx") or die "can't open c:/test/snmp.cfx\n";

the first example opens the file for over-writing: any existing data in the file is lost. The second opens the file for appending: any existing data is retained and new data added to the end of the file.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
OK....

My output file is MUCH more different than the original now.

Original Output:

------------------------------------------------------------
<profiles> overwrite
<.1.3.6.1.4.1.2636>
name = Nortel Contivity TrapsV1
active = yes
<netscreenTrapHw>
trap_type = 6
specific_trap = 100
<alarm>
active = yes
message = This trap indicates that some kind of hardware problem has occured.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapHw
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapHw>
<netscreenTrapFw>
trap_type = 6
specific_trap = 200
<alarm>
active = yes
message = This trap indicates that some kind of firewall functions has been triggered.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapFw
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapFw>
<netscreenTrapSw>
trap_type = 6
specific_trap = 300
<alarm>
active = yes
message = This trap indicates that some kind of software problem has occured.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapSw
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapSw>
<netscreenTrapTrf>
trap_type = 6
specific_trap = 400
<alarm>
active = yes
message = This trap indicates that some kind of traffic conditions has been triggered.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapTrf
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapTrf>
<netscreenTrapVpn>
trap_type = 6
specific_trap = 500
<alarm>
active = yes
message = This trap indicates that VPN tunnel status has occured.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapVpn
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapVpn>
<netscreenTrapNsrp>
trap_type = 6
specific_trap = 600
<alarm>
active = yes
message = This trap indicates that NSRP status has occured.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapNsrp
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapNsrp>
<netscreenTrapGPRO>
trap_type = 6
specific_trap = 700
<alarm>
active = yes
message = This trap indicates that some kind of Global PRO problems has occurred.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapGPRO
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapGPRO>
<netscreenTrapDrp>
trap_type = 6
specific_trap = 800
<alarm>
active = yes
message = This trap indicates that Drp status has occured.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapDrp
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapDrp>
<netscreenTrapIFFailover>
trap_type = 6
specific_trap = 900
<alarm>
active = yes
message = This trap indicates that interface fail over status has occured.
severity = 2
subsystem = NORTEL
suppkey = netscreenTrapIFFailover
</alarm>
convert_trap = no
log_trap = no
</netscreenTrapIFFailover>
</.1.3.6.1.4.1.2636>
</profiles>

------------------------------------------------------------

New Output:

------------------------------------------------------------
object = 'netscreenTrapHw'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that some kind of hardware problem has occured.'
trapid = '100'

object = 'netscreenTrapFw'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that some kind of firewall functions has been triggered.'
trapid = '200'

object = 'netscreenTrapSw'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that some kind of software problem has occured.'
trapid = '300'

object = 'netscreenTrapTrf'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that some kind of traffic conditions has been triggered.'
trapid = '400'

object = 'netscreenTrapVpn'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that VPN tunnel status has occured.'
trapid = '500'

object = 'netscreenTrapNsrp'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that NSRP status has occured.'
trapid = '600'

object = 'netscreenTrapGPRO'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that some kind of Global PRO problems has occurred.'
trapid = '700'

object = 'netscreenTrapDrp'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that Drp status has occured.'
trapid = '800'

object = 'netscreenTrapIFFailover'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that interface fail over status has occured.'
trapid = '900'

object = 'netscreenTrapIDPAttack'
variables = 'netscreenTrapType, netscreenTrapDesc'
description = 'This trap indicates that IDP attack status has occured.'
trapid = '1000'

------------------------------------------------------------

How can I get the new one to have what the original had...


Thanks all!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top