On 1/5/23 08:24, coreyh@free.fr wrote:
Hello list,
I wrote this script for reversing an IP:
#!/bin/bash
IP=$1
if [ -z $IP ];then
echo "$0 IP"
exit 1
fi
REVERSE=$(echo $IP|awk -F\. '{print $4.$3.$2.$1}')
echo $REVERSE
it won't work as the output below.
$ bin/rbl.sh 61.144.56.32
325614461
The "." was lost.
If I changed the awk line to:
REVERSE=$(echo $IP|awk -F\. '{print "$4.$3.$2.$1"}')
It becomes:
$ bin/rbl.sh 61.144.56.32
$4.$3.$2.$1
Can you help with this?
Thanks
#!/bin/bash
# Function to reverse the octets of an IPv4 address
reverse_ipv4() {
local ipv4=$1
local reversed_ipv4
# Split the IPv4 address into an array using the '.' delimiter
IFS="." read -ra octets <<< "$ipv4"
# Reverse the octets and join them using the '.' delimiter
reversed_ipv4="${octets[3]}.${octets[2]}.${octets[1]}.${octets[0]}"
echo "$reversed_ipv4"
}
# Check if a command-line argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <IPv4_address>"
exit 1
fi
input_ipv4="$1"
reversed_ipv4=$(reverse_ipv4 "$input_ipv4")
echo "Input IPv4: $input_ipv4"
echo "Reversed IPv4: $reversed_ipv4"
--
Jeremy
(Lists)