Device-Network-ConfigParser

 view release on metacpan or  search on metacpan

lib/Device/Network/ConfigParser/Linux/NetUtils.pm  view on Meta::CPAN

package Device::Network::ConfigParser::Linux::NetUtils;
# ABSTRACT: Parse the output from net-utils utilities (ifconfig, route, arp, etc)
our $VERSION = '0.006'; # VERSION

use 5.006;
use strict;
use warnings;
use Modern::Perl;
use Parse::RecDescent;
use Data::Dumper;
use JSON;

use Exporter qw{import};

our @EXPORT_OK = qw{get_parser get_output_drivers parse_config post_process};

=head1 NAME

Device::Network::ConfigParser::CheckPoint::Expert - parse CheckPoint expert mode output.

=head1 VERSION

version 0.006

=head1 SYNOPSIS

This module is intended to be used in conjunction with L<Device::Network::ConfigParser>, however there's nothing stopping it being used on its own.

The module provides subroutines to parse & post-process the output from net-tools utilities such as ifconfig, route, arp, etc.

=head1 SUBROUTINES

=head2 get_parser

For more information on the subroutine, see L<Device::Network::ConfigParser/"get_parser">.

Thos module currently recognises output from the following utilities:

=over 4

=item * 'ifconfig' output

=back

=cut

sub get_parser {
    return new Parse::RecDescent(q{
        <autoaction: { [@item] }>
        startrule: config_line(s) { $item[1] }
        config_line:
            ifconfig(s) { { ifconfig => $item{'ifconfig(s)'} } } |
            not_parsed { $item[1] }

            ifconfig: 
                interface encap hw_addr(?) inet(?) inet6(s?) flag(s) mtu if_metric rx_stats tx_stats rx_bytes tx_bytes {
                    {
                        interface => $item{interface},
                        encapsulation => $item{encap},
                        hw_addr => $item{'hw_addr(?)'},
                        inet => $item{'inet(?)'},
                        inet6 => $item{'inet6(s?)'},
                        flags => $item{'flag(s)'},
                        mtu => $item{mtu},
                        metric => $item{if_metric},
                        rx_stats => $item{rx_stats},
                        tx_stats => $item{tx_stats},
                        rx_bytes => $item{rx_bytes},
                        tx_bytes => $item{tx_bytes},
                    }
                }

                interface: m{[-\w]+} { $item{__PATTERN1__} }
                encap: 'Link encap:' m{Ethernet|Local Loopback} { $item{__PATTERN1__} }
                hw_addr: 'HWaddr' m{[0-9a-f:]+} { $item{__PATTERN1__} }
                inet: 
                    inet_addr inet_bcast(?) inet_mask {
                        {
                            address => $item{inet_addr},
                            mask => $item{inet_mask},
                            broadcast => $item{'inet_bcast(?)'}
                        }
                    }

                inet_addr: 'inet addr:' ipv4 { $item{ipv4} }
                inet_bcast: 'Bcast:' ipv4 { $item{ipv4} }
                inet_mask: 'Mask:' netmask { $item{netmask} }
                inet6: inet6_addr inet6_mask inet6_scope {
                    {
                        address => $item{inet6_addr},
                        mask => $item{inet6_mask},
                        scope => $item{inet6_scope}
                    }
                }
                    inet6_addr: 'inet6 addr:' ipv6 { $item{ipv6} }
                    inet6_mask: '/' m{\d{1,3}} { $item{__PATTERN1__} }
                    inet6_scope: 'Scope:' m{\w+} { $item{__PATTERN1__} }

                flag: m{UP|BROADCAST|RUNNING|MULTICAST|LOOPBACK} { $item{__PATTERN1__} }
                mtu: 'MTU:' m{\d+} { $item{__PATTERN1__} }
                if_metric: 'Metric:' m{\d+} { $item{__PATTERN1__} }
                rx_stats: 'RX packets:' m{\d+} 'errors:' m{\d+} 'dropped:' m{\d+} 'overruns:' m{\d+} 'frame:' m{\d+} {
                    {
                        packets => $item{__PATTERN1__},
                        errors => $item{__PATTERN2__},
                        dropped => $item{__PATTERN3__},
                        overruns => $item{__PATTERN4__},
                        frame => $item{__PATTERN5__},
                    }
                }
                tx_stats: 'TX packets:' m{\d+} 'errors:' m{\d+} 'dropped:' m{\d+} 'overruns:' m{\d+} 'carrier:' m{\d+} 'collisions:' m{\d+} 'txqueuelen:' m{\d+}{
                    {
                        packets => $item{__PATTERN1__},
                        errors => $item{__PATTERN2__},
                        dropped => $item{__PATTERN3__},
                        overruns => $item{__PATTERN4__},
                        carrier => $item{__PATTERN5__},
                        collisions => $item{__PATTERN5__},
                        txqueuelen => $item{__PATTERN5__},
                    }
                }
                rx_bytes: 'RX bytes:' m{\d+} m{\(\d{1,}\.\d \w{1,2}\)} { $item{__PATTERN1__} } 
                tx_bytes: 'TX bytes:' m{\d+} m{\(\d{1,}\.\d \w{1,2}\)} { $item{__PATTERN1__} } 


        not_parsed: m{\N+} { { type => $item{__RULE__}, line => $item{__PATTERN1__} } }


        ipv4: m{\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} { $item{__PATTERN1__} }
        netmask: m{\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} { $item{__PATTERN1__} }
        ipv6: m{[0-9a-f:]+} { $item{__PATTERN1__} }
        cidr: '/' m{\d{1,2}} { $item{__PATTERN1__} }
        });
}


=head2 parse_config

For more information on the subroutine, see L<Device::Network::ConfigParser/"parse_config">.

=cut

sub parse_config {
    my ($parser, $config_contents) = @_;

    my $parse_tree = $parser->startrule($config_contents);

    return $parse_tree;
}



=head2 post_process

For more information on the subroutine, see L<Device::Network::ConfigParser/"post_process">.

This module does not post-process the data structure.



( run in 0.445 second using v1.01-cache-2.11-cpan-39bf76dae61 )