Sunday, July 5, 2009

Perl Tutorial on Getting Internal IP and beginning regex

I am working on an tiny project for remote server administration and this was a bit I can show you guys. This is how one would figure out the internal IP's for a computer running Mac or Linux, with perl, with a code explanation for each below:


For Mac:
#!/opt/local/bin/perl
my @ifconfig = `ifconfig`;
my $inetter = qr/(inet \d+\.\d+\.\d+.\d+)/;
foreach (@ifconfig) {
my $line = $_;
if ($line =~ $inetter){
print $&."\n";
}
}

The first line as alwas is the shebang, it tells the script where the perl interpretter is located, if you don't know, you can run your script by typing perl nameof.pl, rather than ./nameof.pl. The second line says that we want to create an array so that, we can load the output of ifconfig into it. Any time you put backticks it runs that command and returns the output. We use an array because it is multiple lines and we want to loop through it. In the next line we are writting or regular expression statement previos to running it. We are putting it in a scalar and using the quote regex function on our statement. We are looking for the phrase inet then a space then a digit then a period etc.. You may notice a lot of back slashes, it's because you have to escape the periods(otherwise our program won't parse them correctly), and the "\d+" is a replacement for saying a digit. Now we do a foreach loop, which says for each line in our array @ifconfig run what is in the curly brackets. The next line says declare a variable called line, it is assigned $_. $_ is a special variable in perl and means the defualt string, in this case the line from ifconfig we are looking at.Then we say if we found the match(inetter) Then print the matched phrase($&), then join the information that is in quotes "/n" and print that as well. /n by the way is a newline character, meaning a newline will begin. then we close out our brackets.

For Linux:

#!/usr/bin/perl
my @ifconfig = `ifconfig`;
my $inetter = qr/(inet addr:\d+\.\d+\.\d+.\d+)/;
foreach (@ifconfig) {
my $line = $_;
if ($line =~ $inetter){
print $&."\n";
}
}

The below paragraph is the same as the one explaining mac with one exception, the regex match expression($inetter) is diffrent because of slight diffrences in the comman ifconfig. Many people do not realize that Mac OS X is based off of a type of unix, BSD in particular(hence the name of the blog).

The first line as alwas is the shebang, it tells the script where the perl interpretter is located, if you don't know, you can run your script by typing perl nameof.pl, rather than ./nameof.pl. The second line says that we want to create an array so that, we can load the output of ifconfig into it. Any time you put backticks it runs that command and returns the output. We use an array because it is multiple lines and we want to loop through it. In the next line we are writting or regular expression statement previos to running it. We are putting it in a scalar and using the quote regex function on our statement. We are looking for the phrase inet then a space and then addr: and then another space then a digit then a period etc.. You may notice a lot of back slashes, it's because you have to escape the periods(otherwise our program won't parse them correctly), and the "\d+" is a replacement for saying a digit. Now we do a foreach loop, which says for each line in our array @ifconfig run what is in the curly brackets. The next line says declare a variable called line, it is assigned $_. $_ is a special variable in perl and means the defualt string, in this case the line from ifconfig we are looking at.Then we say if we found the match(inetter) Then print the matched phrase($&), then join the information that is in quotes "/n" and print that as well. /n by the way is a newline character, meaning a newline will begin. then we close out our brackets.

You may also like
How to get your external IP, using LWP and regex

Or Arp Sniffing Tutorial with perl

No comments: