-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsplitNcmOutputIntoFilePerDevice.pl
More file actions
executable file
·100 lines (79 loc) · 2.32 KB
/
Copy pathsplitNcmOutputIntoFilePerDevice.pl
File metadata and controls
executable file
·100 lines (79 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl
#Split output from NCM into one file for each device
#Valid input is only text from Solarwinds Network Configuration Manager job output
use Modern::Perl '2014';
use autodie;
# use NetAddr::IP;
# use File::Slurp;
# use Getopt::Std;
# use vars qw/ %opt /;
# use Params::Validate qw(:all);
# use Data::Dumper;
# $Data::Dumper::Indent = 2;
# $Data::Dumper::Sortkeys = 1;
# $Data::Dumper::Purity = 1;
# #don't buffer stdout
# $| = 1;
exit main(@ARGV);
sub main {
#Check that we're redirecting stdin from a file
if (-t) {
say "You must redirect input from a valid NCM output file";
say "Usage: $0 < fileWithOutputFromNcm";
exit 1;
}
#Check that the first line looks like it's from NCM
my $firstLine = <>;
die "Not a valid Solarwinds NCM output file"
unless ( $firstLine =~ /SolarWinds Network Configuration Manager/ );
my $hostName;
my $ipAddress;
my $fileName;
my $fileHandle;
my $ipOctetRegex = qr/(?:25[0-5]|2[0-4]\d|[01]?\d\d?)/x;
my $ipv4AddressRegex = qr/$ipOctetRegex\.
$ipOctetRegex\.
$ipOctetRegex\.
$ipOctetRegex/mx;
#read from STDIN
while (<>) {
#Find the current hostname
if ($_ =~ /
^
\s+
(?<hostName>.*?)
\s+
\(
(?<ipAddress>$ipv4AddressRegex)
\)\:
\s*
$
/ix
)
{
$hostName = $+{hostName};
$ipAddress = $+{ipAddress};
$fileName = "$hostName - $ipAddress.txt";
# say $fileName;
open $fileHandle, '>', $fileName or croak $!;
}
#Clear variables between devices
elsif ( $_ =~ /(______________)/ix ) {
#Close the current file
if ($fileHandle) { close($fileHandle); }
# say "Clearing!";
$hostName = $ipAddress = $fileName = $fileHandle = undef;
}
else {
#Any lines that don't match fall through to here
#If we have an open fileHandle print the line to it
if ($fileHandle) {
# say $_;
$_ =~ s/\R//x;
say $fileHandle $_;
}
else { say $_; }
}
}
return 0;
}