<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technical Diary &#187; network</title>
	<atom:link href="http://andriigrytsenko.net/tag/network/feed/" rel="self" type="application/rss+xml" />
	<link>http://andriigrytsenko.net</link>
	<description>With Andrii Grytsenko</description>
	<lastBuildDate>Sun, 01 Apr 2012 11:10:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>IPCalc on Perl</title>
		<link>http://andriigrytsenko.net/2010/03/ipcalc-on-perl/</link>
		<comments>http://andriigrytsenko.net/2010/03/ipcalc-on-perl/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 18:52:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=720</guid>
		<description><![CDATA[Today I decided to make my own IPCalc program. I had two reason for that. First to learn perl&#8217;s binary operations and second one to make some IP calculation points more clear for me. There is some listing of my script I hope this example help you to understand some fundamental points in IP math. [...]]]></description>
			<content:encoded><![CDATA[<p>Today I decided to make my own IPCalc program. I had two reason for that. First to learn perl&#8217;s binary operations and second one to make some IP calculation points more clear for me.<br />
<span id="more-720"></span></p>
<p>There is some listing of my script I hope this example help you to understand some fundamental points in IP math.</p>
<pre class="brush: perl; title: ; notranslate">
#!/usr/local/bin/perl
# Written by Andrii Grytsenko 2010
# PP: http://andriigrytsenko.net    

use POSIX;

sub hex2bin {
   my $hex = shift;
   my $pack = unpack(&quot;B32&quot;,pack(&quot;N&quot;,$hex));
   my $pack = substr($pack,-8);
   return $pack;
}                                          

sub bin2hex {
   my $bin = shift;
   return unpack(&quot;N&quot;, pack(&quot;B32&quot;, substr(&quot;0&quot; x 32 . $bin, -32)))
}                                                               

sub bin2ip {
    my $bin = shift;
    my $type = shift;
    my $ip;
    $ip .= bin2hex(substr($bin,0,8)) .&quot;\.&quot;;
    $ip .= bin2hex(substr($bin,8,8)) .&quot;\.&quot;;
    $ip .= bin2hex(substr($bin,16,8)) .&quot;\.&quot;;
    if ( $type eq 'first') {
        $ip .= bin2hex(substr($bin,24,8)) + 1 ;
    } elsif ($type eq 'broad') {
        $ip .= bin2hex(substr($bin,24,8)) - 1 ;
    } else {
        $ip .= bin2hex(substr($bin,24,8));
    }
    return $ip;
}

sub normalization {
    my $line = shift;
    my ($ip,$mask) = split(/\//,$line);
    return($ip,$mask);
}

my ($ip,$net_mask) = normalization(@ARGV[0]);

my $host_mask = 32-$net_mask;
my $max_host = POSIX::pow(2,$host_mask)-2;
my $bin_mask = &quot;1&quot; x $net_mask . &quot;0&quot; x $host_mask;

my $bin_ip ;
$bin_ip .= hex2bin($_) foreach (split(/\./,$ip));

my $min = $bin_ip &amp; $bin_mask;
my $max_mask = &quot;0&quot; x $net_mask . &quot;1&quot; x $host_mask;
my $max = $bin_ip  | $max_mask;

my $min_ip = bin2ip($min);
my $min_host_ip = bin2ip($min,'first');
my $max_ip = bin2ip($max);
my $max_host_ip = bin2ip($max,'broad');

print \&lt;\&lt; EOF
IP ADDRESS:\t $ip\t $bin_ip
NETWORK MASK:\t \/$net_mask\t\t $bin_mask
WILDCARD:\t \/$host_mask\t\t $max_mask
NETWORK IP:\t $min_ip\/$net_mask\t $min
BROADCAST ADDR:\t $max_ip\t $max
FIRST ADDR:\t $min_host_ip
LAST ADDR:\t $max_host_ip
MAX HOSTS\t $max_host
EOF
</pre>
<p>Run scripts with one argument ip/netmask&#8230; Unfortunately, you can use only short network mask type :</p>
<pre>./ipcalc 192.157.9.3/21</pre>
<p>the output looks like this:</p>
<pre>
IP ADDRESS:      192.157.9.3     11000000100111010000100100000011
NETWORK MASK:    /21             11111111111111111111100000000000
WILDCARD:        /11             00000000000000000000011111111111
NETWORK IP:      192.157.8.0/21  11000000100111010000100000000000
BROADCAST ADDR:  192.157.15.255  11000000100111010000111111111111
FIRST ADDR:      192.157.8.1
LAST ADDR:       192.157.15.254
MAX HOSTS        2046
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/03/ipcalc-on-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

