#!/usr/bin/perl -w use strict; use warnings; use autodie; use Config::Tiny; use HTTP::Tiny; use JSON::PP; use Text::CSV; use File::Temp; use MIME::Lite; my $CONFIG_FILE = "/home/mischa/netskope/netskope.cnf"; my $config = Config::Tiny->read($CONFIG_FILE, 'utf8'); my $NTSKP_TENANT = $config->{netskope}{NTSKP_TENANT}; my $NTSKP_TOKEN = $config->{netskope}{NTSKP_TOKEN}; my $NTSKP_REPORTID = $config->{netskope}{NTSKP_REPORTID}; my $uri = "$NTSKP_TENANT/api/v1/reports?token=$NTSKP_TOKEN&op=reportInfo&id=$NTSKP_REPORTID"; my $response = HTTP::Tiny->new->get($uri); my $json = JSON::PP->new->utf8->decode($response->{'content'}); my $data = $json->{'data'}->{'latestScheduledRunInfo'}->{'widgets'}; my %files; for my $widget (@{$data}) { my $tmp_file = File::Temp->new(UNLINK => 0, TEMPLATE => 'tempXXXXX', DIR => '/tmp'); $files{$tmp_file} = $widget->{'name'}; $uri = "$NTSKP_TENANT/api/v1/reports?token=$NTSKP_TOKEN&op=widgetData&id=$widget->{'id'}"; $response = HTTP::Tiny->new->get($uri); open my $fh_out, ">", $tmp_file; print $fh_out $response->{'content'}; close $fh_out; } my $out_email = "azblocklist.csv"; my $out_zscaler = "zscaler.txt"; open my $fh_email, ">", $out_email; open my $fh_zscaler, ">", $out_zscaler; for my $item (keys %files) { my $count = 0; my $csv = Text::CSV->new({binary => 1, auto_diag => 1}); open my $fh, "<", $item; my $header = $csv->getline($fh); print "$files{$item}\n"; print $fh_email "$files{$item}\n"; while (my $row = $csv->getline($fh)) { last if ($count == 30); if ($row->[1] =~ m/,/) { my @domains = split "," , $row->[1]; for my $domain (@domains) { print "$domain,"; print $fh_email "$domain,"; print $fh_zscaler "$domain\n"; } } else { print "$row->[1],"; print $fh_email "$row->[1],"; print $fh_zscaler "$row->[1]\n"; } $count++; } print "\n"; print $fh_email "\n"; close $fh; unlink $item; } close $fh_email; close $fh_zscaler; my $msg = MIME::Lite->new( From => 'mischa@high5.nl', To => 'mischa@netskope.com', Cc => 'mischa@high5.nl', Subject => 'AztraZeneca Netskope Blocklist', Type => 'TEXT', Data => "Domains pushed to Zscaler for blocking\n\n" ); $msg->attach( Type => 'text/csv', Path => $out_email, Filename => $out_email ); $msg->send('smtp','mail.high5.nl', Debug=>0); unlink $out_email;