Skip to content

Instantly share code, notes, and snippets.

@tacitochaves
Last active August 29, 2015 14:09
Show Gist options
  • Save tacitochaves/4cdca4a5e796ef512dfe to your computer and use it in GitHub Desktop.
Save tacitochaves/4cdca4a5e796ef512dfe to your computer and use it in GitHub Desktop.
Organization hosts monitored in SESMA
#!/usr/bin/env perl
#
# Organization hosts monitored in SESMA
#
# Author: Tácito Chaves - 2014-11-19
# e-mail: [email protected]
# skype: tacito.chaves
use strict;
#use warnings;
use Data::Dumper;
# load file with the datas
my $path = "/home/chaves/automatização/intechne/hosts-monitorados/textual";
my $file = "textual_database.txt";
# opening filehandle
open my $fh, '<', "$path/$file" or die "[Error] File not found!\n";
my @list = <$fh>;
close $fh;
# initializing the hashref
my $data = {};
open my $out, '>', "hosts-monitorados.txt" or die "[Error] creating the file!\n";
for my $line ( @list ) {
chomp $line;
my ( $id, $host, $local ) = ($1, $2, $3)
if $line =~ m/^(\d{1,3})\s(\w+[\-\.]?\w+[\-\.]?\w+[\-\.]?\w+[\-\.]?\w+)\s+(\w+\s?\w+\s?\w+)/i;
# hashref populates the array with data
push @{ $data->{$local} }, {$host};
}
# creating the file with updated information
for my $name ( sort keys %{$data} ) {
print "$name\n";
print $out "$name\n";
for my $id ( @{$data->{$name}} ) {
for ( %{$id} ) {
print "\t$_\n";
print $out "\t$_\n";
}
}
}
close $out;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment