Last active
July 10, 2025 23:10
-
-
Save boranbar/1a982967faff0944f33d721a889b36f6 to your computer and use it in GitHub Desktop.
Add extra IPs script
This file contains hidden or 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 | |
# 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 9+ özel durum | |
if [[ ("$OS" == "almalinux" || "$OS" == "rocky") && "$VERSION" =~ ^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–8 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|8)$ ]]; then | |
echo "🔧 network.service yeniden başlatılıyor..." | |
systemctl restart network | |
elif [[ "$OS" == "rocky" || "$OS" == "almalinux" ]]; 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script makes changes to your network configuration files and restarts networking services.
Please make sure to back up your configuration before running it — especially on production systems.
By using this script, you acknowledge that you do so at your own risk, and the author cannot be held responsible for any unintended consequences, including loss of connectivity.
🖥️ Usage on server:
💡 Note: The script checks for root privileges at the beginning, so you don't need to use
sudo
.📦 Supported Operating Systems:
🌐 Supports IPv4 and IPv6!
You will be prompted to enter one or more IP addresses. You can mix IPv4 and IPv6.
✅ Example entries:
To finish input, press
Ctrl + D
.⚙️ The script:
Feel free to fork and improve it further!