I am writing one of my first perl scripts and I need to create a new variable with the elements of an array. I need to take an IP address entered by a user and manipulate the last octet so it can be used in a router configuration. I am able to split the address into it's four octets. I can also make changes to the array elements as needed, but I can not reassemble the new address after I manipulate the octet I want. Here is my sample code:
# tests the use of ipcalc function in linux with matching
print "Enter IP address: e.g. 22.214.171.124/32\n";
$ip_addr = <>;
my @ipc_arr=`ipcalc -n -m -b -p $ip_addr`;
print $_ . "\n";
($net) = $ipc_arr =~ m/=(.+)/;
print "The network is $net \n";
($mask) = $ipc_arr =~ /=(.+)/;
print "The Subnet mask is $mask \n";
($cidr) = $ipc_arr =~ /=(.+)/;
print "The CIDR notation is /$cidr \n";
print "You entered $net/$cidr which equals $net $mask\n";
#my @pe_arr = $net =~ /\.(\d+)$/ ; #only grabs last octet.
#my @pe_arr = $net =~ /(^\d+)\.(\d+)/ ; # returns first two octets
my @pe_arr = $net =~ /(^\d+)\.(\d+)\.(\d+)\.(\d+)/ ; # grabs all 4 octets!!!!!!!
print $_ . "\n";
($pe) = $pe_arr;
$pe = ($pe + 1);
#my ($nw_addr) = ($pe_arr"."$pe_arr"."$pe_arr"."$pe); #doesn't work
print "the last octect is $pe on the PE \n";
my ($nw_addr) = $pe_arr.$pe_arr.$pe_arr.$pe; #doesn't work
print $nw_addr "\n";
which yields the output:
Enter IP address: e.g. 126.96.36.199/32
The network is 188.8.131.52
The Subnet mask is 255.255.255.248
The CIDR notation is /29
You entered 184.108.40.206/29 which equals 220.127.116.11 255.255.255.248
the last octect is 137 on the PE
Can't use string ("675499137") as a symbol ref while "strict refs" in use at ./ipcalc_test.pl line 47, <> line 1.
So, how can I assign $nw_addr with some of the first three elements of @pe_arr and the variable $pe? Additionally I get errors trying to add in the periods between the octets. The script fails and tells me "Scalar found where operator expected at...."
I have tried all kinds of combinations, but can not get the script to work.
Any help is appreciated.