so.cl

Rascals are always sociable, and the chief sign that a man has any nobility in his character is the little pleasure he takes in others company. Arthur Schopenhauer


Obfuscate IPv4 address

Here is a simple bash script to obfuscate an IPv4 address, I’ll let you guess the logic from the actual code.

#!/bin/bash

read -p "IPv4 address to obfuscate: " ip

if ! echo "$ip" | grep -Pq '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'; then
	echo "Invalid IPv4 address: $ip"
	exit 1
fi

IFS='.' read -ra ipmod <<< "$ip"
first=${ipmod[0]}
hex=$(printf '%x' $first)

decimal=0
for ((i = 1; i < 4; i++)); do
	decimal=$((decimal + ipmod[$i] * 256 ** (3 - $i) ))
done

final="0x$hex.$decimal"

echo -e "\nObfuscated IPv4 address: $final"

And to test it:

$ bash skynet.sh
IPv4 address to obfuscate: 9.9.9.9

Obfuscated IPv4 address: 0x9.592137
$ ping 0x9.592137
PING 0x9.592137 (9.9.9.9): 56 data bytes
64 bytes from 9.9.9.9: icmp_seq=0 ttl=58 time=75.316 ms
64 bytes from 9.9.9.9: icmp_seq=1 ttl=58 time=77.938 ms