nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Simple ipcalc perl script

This is a discussion on Simple ipcalc perl script within the Shell scripting forums, part of the Development/Scripting category; Sorry for posting a perl script in Shell scripting section . Though this will be useful for sysadmins.. This is ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-24-2008, 06:59 PM
Junior Member
User
 
Join Date: Dec 2007
My distro: Redhat
Posts: 3
Rep Power: 0
unixfoo is on a distinguished road
Default Simple ipcalc perl script

Sorry for posting a perl script in Shell scripting section . Though this will be useful for sysadmins..

This is a simple perl script to calculate network address and broadcast address. User should input ip and netmask.

More linux tips @ unix - linux - storage

----------
Perl code:
----------
#!/usr/bin/perl

# Get ipaddress and netmask from user

my $ipaddr=$ARGV[0];
my $nmask=$ARGV[1];


my @addrarr=split(/\./,$ipaddr);
my ( $ipaddress ) = unpack( "N", pack( "C4",@addrarr ) );

my @maskarr=split(/\./,$nmask);
my ( $netmask ) = unpack( "N", pack( "C4",@maskarr ) );


# Calculate network address by logical AND operation of addr & netmask
# and convert network address to IP address format
my $netadd = ( $ipaddress & $netmask );
my @netarr=unpack( "C4", pack( "N",$netadd ) );
my $netaddress=join(".",@netarr);

print "Network address : $netaddress \n";


# Calculate broadcase address by inverting the netmask
# and do a logical or with network address
my $bcast = ( $ipaddress & $netmask ) + ( ~ $netmask );
my @bcastarr=unpack( "C4", pack( "N",$bcast ) ) ;
my $broadcast=join(".",@bcastarr);

print "Broadcast address: $broadcast\n";

# END of program



Here is the output of it.

[root@unixfoo tmp]# ./ipcalc.pl 139.18.16.119 255.255.254.0
Network address : 139.18.16.0
Broadcast address: 139.18.17.255
[root@
unixfoo tmp]#
Reply With Quote
Sponsored Links
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Change root password using perl script? Raj1 Solaris/OpenSolaris 8 06-13-2008 11:01 PM
Perl simple html mail chiku Coding in General 3 08-17-2007 07:59 PM
Quick improvements to avoid hacking to a Perl form Script mariolima Coding in General 0 07-10-2007 06:57 PM
let users change password using perl script? james bond Getting started tutorials 9 05-24-2007 10:32 PM
Need help writing simple copy script z3cka Shell scripting 4 08-27-2006 02:29 PM


All times are GMT +5.5. The time now is 12:20 PM.


Powered by vBulletin® Version 3.7.2 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36