#!/bin/bash
# Create a script file that checks for duplicate IP addresses,
# make it executable, and run it.
# File name
SCRIPT_NAME="check_duplicate_ip.sh"
# Create the script file with the code inside
cat << 'EOF' > $SCRIPT_NAME
#!/bin/bash
# Script to check for duplicate IP addresses on local network interfaces
# Works on Ubuntu and other Linux distributions
echo "Checking for duplicate IP addresses..."
# Extract IPv4 addresses assigned to interfaces
ip_list=$(ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
if [ -z "$ip_list" ]; then
echo "No IPv4 addresses found on this system."
exit 0
fi
echo "Found the following IP addresses:"
echo "$ip_list"
echo "---------------------------------"
# Check for duplicates
duplicates=$(echo "$ip_list" | sort | uniq -d)
if [ -n "$duplicates" ]; then
echo " Duplicate IP addresses detected on this machine:"
echo "$duplicates"
exit 1
else
echo "No duplicate IP addresses detected on this machine."
exit 0
fi
EOF
# Make the script executable
chmod +x $SCRIPT_NAME
# Run the script
./$SCRIPT_NAME