Skip to content

Instantly share code, notes, and snippets.

@boranbar
Last active December 18, 2025 13:37
Show Gist options
  • Select an option

  • Save boranbar/1a982967faff0944f33d721a889b36f6 to your computer and use it in GitHub Desktop.

Select an option

Save boranbar/1a982967faff0944f33d721a889b36f6 to your computer and use it in GitHub Desktop.
Add extra IPs script
#!/bin/bash
# Root kontrolü
if [[ $EUID -ne 0 ]]; then
echo "❌ Lütfen bu scripti root olarak çalıştırın."
exit 1
fi
# OS tespiti
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
VERSION=$VERSION_ID
fi
SUPPORTED=false
if [[ "$OS" == "ubuntu" && "$(echo "$VERSION >= 14.04" | bc)" -eq 1 ]]; then
SUPPORTED=true
elif [[ "$OS" == "debian" ]]; then
SUPPORTED=true
elif [[ "$OS" == "almalinux" || "$OS" == "rocky" || "$OS" == "centos" ]]; then
if [[ "$VERSION" =~ ^(7|8|9)(\.[0-9]+)?$ ]]; then
SUPPORTED=true
fi
fi
if [[ "$SUPPORTED" != "true" ]]; then
echo "❌ Unsupported OS: $OS $VERSION"
exit 1
fi
# IP doğrulama fonksiyonları
is_valid_ipv4() { [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$ ]]; }
is_valid_ipv6() { [[ "$1" =~ ^([0-9a-fA-F:]+:+)+[0-9a-fA-F]+/[0-9]{1,3}$ ]]; }
# IP girişi
echo "📝 Eklemek istediğiniz IP adreslerini girin (örnek: 192.168.1.10/24 veya 2001:db8::1/64)"
echo "✔️ Girişi bitirmek için Ctrl+D tuşlayın:"
IP_LIST=()
while read -r ip; do
[[ -z "$ip" ]] && continue
if is_valid_ipv4 "$ip" || is_valid_ipv6 "$ip"; then
IP_LIST+=("$ip")
else
echo "⚠️ Geçersiz IP atlandı: $ip"
fi
done
if [[ ${#IP_LIST[@]} -eq 0 ]]; then
echo "⚠️ Geçerli IP girilmedi. Çıkılıyor."
exit 1
fi
# Varsayılan arayüz
IFACE=$(ip route | grep default | awk '{print $5}' | head -n1)
if [[ -z "$IFACE" ]]; then
echo "❌ Ağ arayüzü tespit edilemedi."
exit 1
fi
echo "🔌 IP'ler $IFACE arayüzüne eklenecek..."
# AlmaLinux/Rocky 8+ ve CentOS 8+ özel durum (NetworkManager kullanıyor)
if [[ ("$OS" == "almalinux" || "$OS" == "rocky" || "$OS" == "centos") && "$VERSION" =~ ^(8|9)(\.[0-9]+)?$ ]]; then
COUNT=0
for ip in "${IP_LIST[@]}"; do
if is_valid_ipv4 "$ip"; then
nmcli connection modify "$IFACE" +ipv4.addresses "$ip"
nmcli connection modify "$IFACE" ipv4.method manual
elif is_valid_ipv6 "$ip"; then
nmcli connection modify "$IFACE" +ipv6.addresses "$ip"
nmcli connection modify "$IFACE" ipv6.method manual
fi
((COUNT++))
done
nmcli connection up "$IFACE"
# Ubuntu/Debian (netplan kullanan sürümler)
elif [[ "$OS" == "ubuntu" && "$(echo "$VERSION >= 17.10" | bc)" -eq 1 ]] || [[ "$OS" == "debian" && -d "/etc/netplan" ]]; then
cd /etc/netplan || { echo "❌ /etc/netplan dizini bulunamadı!"; exit 1; }
# 00-* dosyaları öncelikli ara
NETPLAN_FILE=$(ls 00-*.yaml 00-*.yml 2>/dev/null | head -1)
[[ -z "$NETPLAN_FILE" ]] && NETPLAN_FILE=$(ls *.yaml *.yml 2>/dev/null | head -1)
if [[ -z "$NETPLAN_FILE" ]]; then
NETPLAN_FILE="99-extra-ips.yaml"
echo "📄 Yeni netplan dosyası oluşturuluyor: $NETPLAN_FILE"
else
echo "📄 Mevcut netplan dosyası düzenleniyor: $NETPLAN_FILE"
fi
# Yedek al
[[ -f "$NETPLAN_FILE" ]] && cp "$NETPLAN_FILE" "$NETPLAN_FILE.bak.$(date +%s)"
# --- DÜZELTİLEN KISIM ---
# Mevcut IP'leri oku – yalnızca CIDR içeren satırlar alınır
EXISTING_IPS=$(grep -A 20 "^ *addresses:" "$NETPLAN_FILE" \
| grep "^\s*-" \
| grep '/' \
| sed 's/^\s*-\s*//')
# Gateway ve nameserver bilgileri
GATEWAY=$(grep "gateway4:" "$NETPLAN_FILE" | awk '{print $2}')
if grep -q "nameservers:" "$NETPLAN_FILE"; then
NAMESERVERS=$(grep -A 10 "nameservers:" "$NETPLAN_FILE" \
| grep "^\s*-" \
| sed 's/^\s*-\s*//')
HAS_NAMESERVERS=true
else
HAS_NAMESERVERS=false
fi
# --- /DÜZELTİLEN KISIM ---
# Yeni netplan dosyasını yaz
cat > "$NETPLAN_FILE" <<EOF
# Generated by IP add script
network:
ethernets:
$IFACE:
addresses:
EOF
# Eski + yeni IP'leri ekle
[[ -n "$EXISTING_IPS" ]] && while read -r old; do
[[ -n "$old" ]] && echo " - $old" >> "$NETPLAN_FILE"
done <<< "$EXISTING_IPS"
for ip in "${IP_LIST[@]}"; do
echo " - $ip" >> "$NETPLAN_FILE"
done
[[ -n "$GATEWAY" ]] && echo " gateway4: $GATEWAY" >> "$NETPLAN_FILE"
if [[ "$HAS_NAMESERVERS" == "true" && -n "$NAMESERVERS" ]]; then
echo " nameservers:" >> "$NETPLAN_FILE"
echo " addresses:" >> "$NETPLAN_FILE"
while read -r ns; do
[[ -n "$ns" ]] && echo " - $ns" >> "$NETPLAN_FILE"
done <<< "$NAMESERVERS"
echo " search: []" >> "$NETPLAN_FILE"
fi
echo " version: 2" >> "$NETPLAN_FILE"
# Ubuntu/Debian eski yapı
elif [[ "$OS" == "ubuntu" || "$OS" == "debian" ]]; then
IFACE_FILE="/etc/network/interfaces.d/$IFACE.cfg"
cp "$IFACE_FILE" "$IFACE_FILE.bak.$(date +%s)" 2>/dev/null
cat > "$IFACE_FILE" <<EOF
auto $IFACE
iface $IFACE inet manual
EOF
for ip in "${IP_LIST[@]}"; do
if is_valid_ipv4 "$ip"; then
echo "up ip addr add $ip dev $IFACE" >> "$IFACE_FILE"
elif is_valid_ipv6 "$ip"; then
echo "up ip -6 addr add $ip dev $IFACE" >> "$IFACE_FILE"
fi
done
# RHEL/CentOS/AlmaLinux 7 klasik yapı
else
IFCFG="/etc/sysconfig/network-scripts/ifcfg-$IFACE"
cp "$IFCFG" "$IFCFG.bak.$(date +%s)" 2>/dev/null
COUNT=0
for ip in "${IP_LIST[@]}"; do
if is_valid_ipv4 "$ip"; then
echo "IPADDR$COUNT=${ip%/*}" >> "$IFCFG"
echo "PREFIX$COUNT=${ip#*/}" >> "$IFCFG"
elif is_valid_ipv6 "$ip"; then
echo "IPV6ADDR$COUNT=$ip" >> "$IFCFG"
fi
((COUNT++))
done
fi
# Ağ servisini yeniden başlat
echo "🔄 Ağ servisi yeniden başlatılıyor..."
if [[ "$OS" == "ubuntu" && "$(echo "$VERSION >= 17.10" | bc)" -eq 1 ]] || [[ "$OS" == "debian" && -d "/etc/netplan" ]]; then
echo "🔧 Netplan uygulanıyor..."
netplan apply
elif [[ "$OS" == "ubuntu" || "$OS" == "debian" ]]; then
echo "🔧 Ağ servisi yeniden başlatılıyor (legacy)..."
systemctl restart networking || { ifdown "$IFACE"; ifup "$IFACE"; }
elif [[ "$OS" =~ ^(centos|rocky|almalinux)$ && "$VERSION" =~ ^7(\.[0-9]+)?$ ]]; then
echo "🔧 network.service yeniden başlatılıyor..."
systemctl restart network
elif [[ ("$OS" == "rocky" || "$OS" == "almalinux" || "$OS" == "centos") && "$VERSION" =~ ^(8|9)(\.[0-9]+)?$ ]]; then
echo "🔧 NetworkManager yeniden başlatılıyor..."
systemctl reload NetworkManager || systemctl restart NetworkManager
else
echo "⚠️ Ağ servisi elle yeniden başlatılmalı."
fi
# Sonuç
echo "✅ Eklenen IP adresleri:"
for ip in "${IP_LIST[@]}"; do
echo " - $ip"
done
@boranbar
Copy link
Author

Usage

Method 1: Here Document (Recommended)

Paste multiple IPs directly in the terminal:

./add-extra-ips.sh - <<'EOF'
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
EOF

With custom subnet mask (e.g., /25):

./add-extra-ips.sh -m 25 - <<'EOF'
192.168.1.10
192.168.1.11
192.168.1.12
EOF

Method 2: Text File

Create a file with IP addresses (one per line):

cat > ips.txt <<'EOF'
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
EOF

Then run:

./add-extra-ips.sh -f ips.txt

With custom subnet mask:

./add-extra-ips.sh -m 25 -f ips.txt

Options

Option Description
-m, --mask Subnet mask (default: 24)
-i, --iface Network interface (default: auto-detect)
-f, --file Read IPs from file
-h, --help Show help message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment