Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Need to calculate IP address

by Anonymous Monk
on Dec 29, 2017 at 17:47 UTC ( [id://1206427]=note: print w/replies, xml ) Need Help??


in reply to Need to calculate IP address

Thanks everyone for the help. For some reason, none of the solutions below wanted to work within my script. I ended up having the script write the last octet to a file and then modifying it from there, which worked. Makes no sense as to why I would have to go thru that, but it is what it is. Below is what I ended up with that works.
print "Enter the Tunnel Interface IP "; $tunip = <>; chomp ($tunip); $test2 = check_valid_ip2($tunip); print "\n"; my $period="."; my @ip_fields = split(/\./, $tunip); open (LASTOCTET, ">lastoctet"); print LASTOCTET "$ip_fields[3]\n"; close (LASTOCTET); open (MYINPUTFILE, "lastoctet"); while (<MYINPUTFILE>) { my($line) = $_; chomp($line); $ipcheck = $line; } close (MYINPUTFILE); if ($ipcheck % 2) { $test_ip=$ipcheck+1; } else { $test_ip=$ipcheck-1; } $bgpip = $ip_fields[0].$period.$ip_fields[1].$period.$ip_fields[2].$pe +riod.$test_ip;

Replies are listed 'Best First'.
Re^2: Need to calculate IP address
by thanos1983 (Parson) on Dec 30, 2017 at 01:27 UTC

    Hello Anonymous Monk,

    For some reason, none of the solutions below wanted to work within my script.. Can you try this one, if it is working?

    It is really strange that you need to do this much work, read and write to a file for IP manipulations.

    #!/usr/bin/env perl use strict; use warnings; use NetAddr::IP; use feature 'say'; my $final; my $input = '127.0.0.3'; my @ip = split /\./, $input; if ($ip[3] % 2) { $final = join '.', @ip; $final = NetAddr::IP->new($final.'/8') + 1; } else { $final = join '.', @ip; $final = NetAddr::IP->new($final.'/8') - 1; } $final = substr $final, 0, -2; say $final; __END__ $ perl test.pl 127.0.0.4

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206427]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-29 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found