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!