Last active
August 6, 2024 18:35
-
-
Save DeeprajPandey/8260fb242735b57244ffccc6768e4666 to your computer and use it in GitHub Desktop.
Convert a device's public IP address to CIDR notation and update ingress rules on Oracle Cloud Infrastructure Network List to allow ingress from current device to devices configured on our VCN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Deepraj Pandey | |
# 06 August 2024 | |
# | |
# Convert a device's public IP address to CIDR notation and update ingress rules on Oracle Cloud Infrastructure | |
# Network List to allow ingress from current device to devices configured on our VCN | |
# | |
# Dependencies: OCI cli tool installed and configured with a ~/.oci/config file | |
# Enable alias expansion and load custom aliases (oci setup) | |
shopt -s expand_aliases | |
source ~/.shell_local_after | |
public_ip_in_cidr() { | |
local ip | |
local public_ip | |
local octet | |
local binary_octet | |
local leading_zeros | |
local cidr_prefix | |
ip=$(dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com) | |
# Remove the double quotes from the IP address | |
public_ip=$(echo "$ip" | tr -d '"') | |
# echo "[DEBUG] Preprocessed IP Address: ${public_ip}" | |
# local public_ip=$(dig +short myip.opendns.com @resolver1.opendns.com) | |
# echo "[DEBUG] Public IP Address: ${public_ip}" | |
octet=$(echo "$public_ip" | awk -F '.' '{print $4}') | |
# echo "[DEBUG] octet=${octet}" | |
binary_octet=$(printf "%08d" $(echo "obase=2; ${octet}" | bc)) | |
# echo "[DEBUG] binary octet=${binary_octet}" | |
leading_zeros=$(echo "${binary_octet}" | grep -o "^0*" | wc -c) | |
leading_zeros=$((leading_zeros - 1)) # Correct for extra space | |
# echo "[DEBUG] leading zeros=${leading_zeros}" | |
cidr_prefix=$((32 - (8 - leading_zeros))) | |
# Ensure the CIDR prefix >= 24 | |
if [ "$cidr_prefix" -lt 24 ]; then | |
cidr_prefix=24 | |
fi | |
# echo "[DEBUG] CIDR prefix=${cidr_prefix}" | |
# echo "[DEBUG] IP Address in CIDR notation" | |
echo "${public_ip%.*}.0/${cidr_prefix}" | |
} | |
# Get the public IP in CIDR notation | |
source_ip=$(public_ip_in_cidr) | |
# Update source in ingress rules json | |
sed -i -e '32s|"source": "[^"]*"|"source": "'"${source_ip}"'"|' ~/.oci/ingress-security-rules.json | |
# Parse the -d flag for debug mode | |
debug_mode=false | |
while getopts "d" opt; do | |
case $opt in | |
d) | |
debug_mode=true | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
# Run the OCI command with or without debug mode | |
if $debug_mode; then | |
oci network security-list update --security-list-id ocid1.securitylist.oc1.ap-mumbai-1.aaaaaaaa6zsni7mcr75ixcvhdadgbwspi3dl7njrdfudi3sijlxunt54pkya --ingress-security-rules "file://~/.oci/ingress-security-rules.json" --debug | |
else | |
oci network security-list update --security-list-id ocid1.securitylist.oc1.ap-mumbai-1.aaaaaaaa6zsni7mcr75ixcvhdadgbwspi3dl7njrdfudi3sijlxunt54pkya --ingress-security-rules "file://~/.oci/ingress-security-rules.json" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment