-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkrl-parse.pl
executable file
·93 lines (68 loc) · 1.7 KB
/
krl-parse.pl
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
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use LWP::UserAgent;
use HTTP::Request;
use URI::Escape;
use JSON -support_by_pp;
#use Data::Dumper;
# global options
use vars qw/ %opt /;
my $opt_string = '?hvf:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{'h'} || $opt{'?'};
my $krl_file = $opt{'f'};
# get the KRL file to validate
my $ruleset;
if (-e $krl_file) {
local $/;
open(KRL, $krl_file);
$ruleset = <KRL>;
close(KRL)
or warn "Unable to close the file handle: $!";
} else {
die "File $$krl_file not found";
}
# print $ruleset;
my $krl_validate_url = "http://cs.kobj.net/manage/validate/";
my $krl_flush_url = "http://cs.kobj.net/ruleset/flush/";
my $parameters = ['flavor' => 'json',
# 'rule' => uri_escape($ruleset)
'rule' => $ruleset
];
my $ua = LWP::UserAgent->new();
my $response = $ua->post($krl_validate_url, $parameters);
#print $response->content;
my $json = JSON->new->allow_nonref;
my $content = eval {
$json->allow_singlequote->decode($response->content)
};
die "Parser failed miserably: ", $response->content if $@;
if ($content->{'status'} eq 'error') {
print $content->{'result'}, "\n";
} elsif (($content->{'status'} eq 'success')) {
if ($opt{'v'}) {
$json = $json->pretty(1);
$json = $json->indent_length(4);
print $json->encode($content->{'result'}), "\n";
} else {
print "OK\n";
}
} else {
warn "Invalid content returned";
}
1;
#
# Message about this program and how to use it
#
sub usage {
print STDERR << "EOF";
Validates a KRL ruleset
usage: $0 [-h?] -f filename
-h|? : this (help) message
-f file : input file
-v : be verbose (return parse tree)
example: $0 -f a16x565.krl
EOF
exit;
}