Skip to content

Instantly share code, notes, and snippets.

@superstes
Created October 8, 2024 17:19
Show Gist options
  • Select an option

  • Save superstes/9dfbc24728c0c40334c1d560a762cb55 to your computer and use it in GitHub Desktop.

Select an option

Save superstes/9dfbc24728c0c40334c1d560a762cb55 to your computer and use it in GitHub Desktop.
Bash Script to check if IP is Public (using Python3 inline)
#!/bin/bash
IP="1.1.1.1"
ip4="$(python3 -c "from ipaddress import IPv4Address; from sys import argv; ip=argv[1]; print(ip) if IPv4Address(ip).is_global else print('1')" "$IP" 2>/dev/null || echo '0')"
if [[ "$ip4" == '0' ]]
then
ip6="$(python3 -c "from ipaddress import IPv6Address; from sys import argv; ip=argv[1]; print(ip) if IPv6Address(ip).is_global else print('1')" "$IP" 2>/dev/null || echo '0')"
if [[ "$ip6" == '0' ]]
then
echo "${IP} is no valid IP"
elif [[ "$ip6" == '1' ]]
then
echo "${IP} is private IPv6"
else
echo "${IP} is public IPv6"
fi
elif [[ "$ip4" == '1' ]]
then
echo "${IP} is private IPv4"
else
echo "${IP} is public IPv4"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment