RRZE – Projekte & Prozesse (P&P)

Das Blog der RRZE Stabsstelle "Projekte & Prozesse"

Content

IP mit Hostnamen ergänzen

English Version

Beschreibung

Es ist schwierig IP-Adressen richtig zuordnen zu können. Erst durch eine Auflösung mit den Hostnamen werden sie verständlich. Aus diesem Grund sollen diese mit den entsprechenden Hostnamen ergänzt werden. Die am RRZE verwendeten Firewallregeln basieren auf IP-Adressen. Um diese leichter zu verstehen und somit bearbeiten zu können müssen sie in Hostnamen aufgelöst werden.
Händisch wäre diese Ergänzung sehr aufwendig. Daher sollte hierfür ein Skript geschrieben werden. Ich habe mich entschieden Perl zu verwenden, da diese Skriptsprache die gewünschte Funktionalität und Flexibilität bietet.

Benutzung

[bash] $ ./ipHostname.pl -i INPUTFILE -o OUTPUTFILE[/bash]

Beispiel

[bash]

$ ./ipHostname.pl -i ipinput.txt -o ipoutput.txt

dali.rrze.uni-erlangen.de/131.188.84.???
web.de/213.165.64.???
mordor.rrze.uni-erlangen.de/131.188.12.???

Hosts saved to file: ipoutput.txt

[/bash]

Das letzte Oktett der IPs wurden aus Sicherheitsgründen zensiert.

Quellcode

[perl]
#!/usr/bin/perl
#
# Inputs IP address from log file and outputs
# host name to screen and file.
#
# Typical usage: ./ipHostname.pl -i inputfile -o outputfile [-s]
#

use Getopt::Long;
use strict;
use warnings;

my $outputfile = 0;
my $inputfile = 0;
my $silent = 0;
my $counter = 0;

#Sind die Optionen, welche Ausgewählt werden könen/müssen
GetOptions (
“o=s” => \$outputfile,
“output=s” => \$outputfile,
“i=s” => \$inputfile,
“input=s” => \$inputfile,
“s|silent” => \&silent,
“h|help|?” => \&syntax);

#Wenn Inputfile oder Outputfile nicht angegeben sind soll die Syntax ausgegeben werden, da beide Parameter Pflichtfelder sind
if ((!$inputfile ) || (!$outputfile )) {
&syntax;
}

#Die Variable $silent wird hier so gesetzt, dass es keine Ausgabe auf der Konsole gibt
sub silent {
$silent = 1;
}

#Hier werden In- und Outputfile geöffnet
open(IP, $inputfile) || warn “Can’t open input $inputfile: $!\n”;
open(HOST, “>$outputfile”) || warn “Can’t open output $outputfile: $!\n”;

print “\n” if !$silent;

#Datei wird zum gesplittet um sie richtig verarbeiten zu können
while() {
my @foo = split(/ /,$_);
my @ip = grep(/\d+\.\d+\.\d+\.\d+/,@foo);
my $ip = $ip[0];
#IPs werden gesplittet und mit gethostbyaddr gecheckt. Anschliessend folgt nur die Ausgabe.
if ($ip) {
my @numbers = split(/\./, $ip);
my $ip_number = pack(“C4”, @numbers);
my ($name) = (gethostbyaddr($ip_number, 2))[0];
if ($name) {
print “$name/$ip\n” if !$silent;
print HOST “$name/@foo”;
} else {
print “This IP has no name: $ip\n” if !$silent;
print HOST “(no host name)/@foo”;
}
}else {
print HOST “@foo”;
}
}
if ($outputfile && !$silent) {
print “\nHosts saved to file: $outputfile\n\n”;
}
close IP;
close HOST;

#Hilfe
sub syntax {

if ($counter == 0) {
print “\nipHostname.pl \n
Usage: ./ipHostname.pl -i inputfile -o outputfile [options]
-i Path to Inputfile
-o Path to Outputfile
-h –help Print this Help
-s Silence
-silent Silence
\n”;
++$counter;
}
}
exit;
[/perl]

IP Supplement with Hostname

Description

It is difficult to assign IP-adresses correctly. Only by breaking up through the hostname they become understandable. Because of this reason the hostname should be added to these. The firewall rules, used at the RRZE, are based on IP-adresses. To make this process easier to understand and therefore easier to work with the hostnames need to be broken up.
These supplements are very time consuming if you do this per hand. Therefore a skript is needed. I decided to use Perl, because this script language offers the needed functionality and flexability.

Usage

[bash] $ ./ipHostname.pl -i INPUTFILE -o OUTPUTFILE[/bash]

Example

[bash]

$ ./ipHostname.pl -i ipinput.txt -o ipoutput.txt

dali.rrze.uni-erlangen.de/131.188.84.???
web.de/213.165.64.???
mordor.rrze.uni-erlangen.de/131.188.12.???

Hosts saved to file: ipoutput.txt

[/bash]

The last octet of the IPs has been cencored because of safety issues.

Source Code

[perl]
#!/usr/bin/perl
#
# Inputs IP address from log file and outputs
# host name to screen and file.
#
# Typical usage: ./ipHostname.pl -i inputfile -o outputfile [-s]
#

use Getopt::Long;
use strict;
use warnings;

my $outputfile = 0;
my $inputfile = 0;
my $silent = 0;
my $counter = 0;

#Sind die Optionen, welche Ausgewählt werden könen/müssen
GetOptions (
“o=s” => \$outputfile,
“output=s” => \$outputfile,
“i=s” => \$inputfile,
“input=s” => \$inputfile,
“s|silent” => \&silent,
“h|help|?” => \&syntax);

#Wenn Inputfile oder Outputfile nicht angegeben sind soll die Syntax ausgegeben werden, da beide Parameter Pflichtfelder sind
if ((!$inputfile ) || (!$outputfile )) {
&syntax;
}

#Die Variable $silent wird hier so gesetzt, dass es keine Ausgabe auf der Konsole gibt
sub silent {
$silent = 1;
}

#Hier werden In- und Outputfile geöffnet
open(IP, $inputfile) || warn “Can’t open input $inputfile: $!\n”;
open(HOST, “>$outputfile”) || warn “Can’t open output $outputfile: $!\n”;

print “\n” if !$silent;

#Datei wird zum gesplittet um sie richtig verarbeiten zu können
while() {
my @foo = split(/ /,$_);
my @ip = grep(/\d+\.\d+\.\d+\.\d+/,@foo);
my $ip = $ip[0];
#IPs werden gesplittet und mit gethostbyaddr gecheckt. Anschliessend folgt nur die Ausgabe.
if ($ip) {
my @numbers = split(/\./, $ip);
my $ip_number = pack(“C4”, @numbers);
my ($name) = (gethostbyaddr($ip_number, 2))[0];
if ($name) {
print “$name/$ip\n” if !$silent;
print HOST “$name/@foo”;
} else {
print “This IP has no name: $ip\n” if !$silent;
print HOST “(no host name)/@foo”;
}
}else {
print HOST “@foo”;
}
}
if ($outputfile && !$silent) {
print “\nHosts saved to file: $outputfile\n\n”;
}
close IP;
close HOST;

#Hilfe
sub syntax {

if ($counter == 0) {
print “\nipHostname.pl \n
Usage: ./ipHostname.pl -i inputfile -o outputfile [options]
-i Path to Inputfile
-o Path to Outputfile
-h –help Print this Help
-s Silence
-silent Silence
\n”;
++$counter;
}
}
exit;
[/perl]

Leave a comment

Your email address will not be published Required fields are marked *

You can use the following HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>