September 28th in Linux/Unix, PHP Scripts by pbu .

How to check IPs on same subnet?

How to check IPs on same subnet?

In many cases, you might want to check whether an ip address falls under a same subnet or not. It can be done both in perl and php using the Network library.

For PHP use Net IP4 library
For PERL use Net::IP library

<?php
// check for IP falls in same subnet or not
include("Net/IPv4.php");

$objIP = new Net_IPv4();

echo $objIP->ipInNetwork("192.xx.xx.xx", "192.xx.xx.x/24") ? "Same Subnet" : "Outside the Subnet";
?>

In perl you can use Net::IP module

#!/usr/bin/perl

use NetAddr::IP;

my $netwrk  = NetAddr::IP->new('192.xx.xx.x/24');
my $ip = '192.x.x.x';

if ($ip->within($netwrk)) {
    print "IP is in same subnet";
} else {
    print "IP outside the subnet";

   }

Similar Posts:

Share and Enjoy:
  • del.icio.us
  • digg
  • StumbleUpon
  • Technorati
  • DZone
  • Facebook
  • FriendFeed
  • Reddit
  • RSS
  • Twitter

Leave A Comment.