in reply to Grabbing Structured multi line output from a file?
#usr/bin/perl -w use strict; use Data::Dump qw(pp); my %hash; while (my $line = <DATA>) { my ($edit) = ($line =~ m/^\s*edit\s*\"(.*)\"/)[0]; next unless $edit; #find first "edit line" my $ip = get_ip(); #get ip if it exists in this record #else $ip is a null (not found) $hash{$edit}=$ip if $ip; } #returns ip = null if a valid ip is not found in this record #end of record is "next" sub get_ip { while (<DATA>) { my ($ip) = ($_ =~ /^\s*set ip\s*([\d\.]+)/)[0]; return $ip if ($ip or /next/); } } print pp \%hash; =prints { dmz => "192.18.254.1", internal1 => "10.1.16.3", internal2 => "192.168.2.1", wan1 => "1.1.1.1", wan2 => "10.4.254.198", } =cut __DATA__ config system interface edit "dmz" set vdom "root" set ip 192.18.254.1 255.255.254.0 set allowaccess ping fgfm set type physical set alias "Guest-Wlan" next edit "wan2" set vdom "test" set ip 10.4.254.198 255.255.255.252 set allowaccess ping https ssh snmp set type physical set alias "To MON ospf int" next edit "wan1" set vdom "root" set ip 1.1.1.1 255.255.255.252 set allowaccess ping https set type physical set alias "To Internet" next edit "modem" set vdom "root" set mode pppoe set allowaccess fgfm set type physical next edit "ssl.root" set vdom "root" set type tunnel next edit "ssl.MyRealm" set vdom "MyRealm" set type tunnel next edit "internal1" set vdom "root" set ip 10.1.16.3 255.255.255.0 set allowaccess ping https ssh snmp fgfm set type physical set alias "To MON Internal " next edit "internal2" set vdom "MyRealm" set dhcp-relay-service enable set dhcp-relay-ip "192.168.1.1" set ip 192.168.2.1 255.255.254.0 set allowaccess ping https ssh snmp fgfm set type physical set alias "To MINE" next edit "VPN-Dial" set vdom "root" set type tunnel set interface "wan1" next end
|
|---|