Last active
December 18, 2025 13:37
-
-
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 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 |
Author
Author
#!/bin/bash
# Root kontrolü
if [[ $EUID -ne 0 ]]; then
echo "❌ Bu scripti root olarak çalıştırın."
exit 1
fi
# Kullanım bilgisi
usage() {
echo "Kullanım:"
echo " $0 IP1 IP2 IP3 ... # Argüman olarak"
echo " $0 -f dosya.txt # Dosyadan oku"
echo " echo 'IP1 IP2' | $0 - # Stdin'den oku"
echo ""
echo "Örnekler:"
echo " $0 185.83.147.152 185.83.147.153 185.83.147.154"
echo " $0 -f ip_listesi.txt"
echo " $0 185.83.147.152/24 # CIDR ile"
echo ""
echo "Opsiyonlar:"
echo " -m, --mask MASK Varsayılan subnet mask (default: 24)"
echo " -i, --iface IFACE Ağ arayüzü (default: otomatik)"
echo " -f, --file FILE IP listesi dosyası"
echo " -h, --help Bu yardım mesajı"
exit 1
}
# Varsayılanlar
DEFAULT_MASK="24"
IFACE=""
IP_FILE=""
IP_LIST=()
# Argüman parse
while [[ $# -gt 0 ]]; do
case $1 in
-m|--mask)
DEFAULT_MASK="$2"
shift 2
;;
-i|--iface)
IFACE="$2"
shift 2
;;
-f|--file)
IP_FILE="$2"
shift 2
;;
-h|--help)
usage
;;
-)
# Stdin'den oku
while read -r line; do
for ip in $line; do
[[ -n "$ip" ]] && IP_LIST+=("$ip")
done
done
shift
;;
-*)
echo "❌ Bilinmeyen opsiyon: $1"
usage
;;
*)
IP_LIST+=("$1")
shift
;;
esac
done
# Dosyadan oku
if [[ -n "$IP_FILE" ]]; then
if [[ ! -f "$IP_FILE" ]]; then
echo "❌ Dosya bulunamadı: $IP_FILE"
exit 1
fi
while read -r line; do
# Yorum ve boş satırları atla
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
for ip in $line; do
[[ -n "$ip" ]] && IP_LIST+=("$ip")
done
done < "$IP_FILE"
fi
# IP kontrolü
if [[ ${#IP_LIST[@]} -eq 0 ]]; then
echo "❌ Hiç IP adresi girilmedi!"
usage
fi
# OS tespiti
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
VERSION=$VERSION_ID
fi
# Arayüz tespiti
if [[ -z "$IFACE" ]]; then
IFACE=$(ip route | grep default | awk '{print $5}' | head -n1)
fi
if [[ -z "$IFACE" ]]; then
echo "❌ Ağ arayüzü tespit edilemedi. -i ile belirtin."
exit 1
fi
echo "🔌 Arayüz: $IFACE"
echo "📝 Eklenecek IP sayısı: ${#IP_LIST[@]}"
# IP doğrulama ve CIDR ekleme
VALID_IPS=()
for ip in "${IP_LIST[@]}"; do
# Boşlukları temizle
ip=$(echo "$ip" | tr -d '[:space:]')
[[ -z "$ip" ]] && continue
# CIDR yoksa ekle
if [[ ! "$ip" =~ "/" ]]; then
ip="$ip/$DEFAULT_MASK"
fi
# IPv4 doğrulama
if [[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$ ]]; then
VALID_IPS+=("$ip")
echo " ✓ $ip"
# IPv6 doğrulama
elif [[ "$ip" =~ ^([0-9a-fA-F:]+:+)+[0-9a-fA-F]+/[0-9]{1,3}$ ]]; then
VALID_IPS+=("$ip")
echo " ✓ $ip"
else
echo " ✗ Geçersiz: $ip"
fi
done
if [[ ${#VALID_IPS[@]} -eq 0 ]]; then
echo "❌ Geçerli IP bulunamadı!"
exit 1
fi
echo ""
echo "🚀 ${#VALID_IPS[@]} IP ekleniyor..."
# ============================================
# AlmaLinux/Rocky/CentOS 8+ (NetworkManager)
# ============================================
if [[ ("$OS" == "almalinux" || "$OS" == "rocky" || "$OS" == "centos") && "$VERSION" =~ ^(8|9)(\.[0-9]+)?$ ]]; then
echo "📦 NetworkManager (nmcli) kullanılıyor..."
for ip in "${VALID_IPS[@]}"; do
if [[ "$ip" =~ ":" ]]; then
nmcli connection modify "$IFACE" +ipv6.addresses "$ip"
else
nmcli connection modify "$IFACE" +ipv4.addresses "$ip"
fi
done
nmcli connection up "$IFACE" 2>/dev/null || nmcli connection reload
# ============================================
# Ubuntu 17.10+ / Debian with Netplan
# ============================================
elif [[ "$OS" == "ubuntu" && "$(echo "$VERSION >= 17.10" | bc)" -eq 1 ]] || [[ "$OS" == "debian" && -d "/etc/netplan" ]]; then
echo "📦 Netplan kullanılıyor..."
cd /etc/netplan || { echo "❌ /etc/netplan bulunamadı!"; exit 1; }
# Netplan dosyasını bul
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)
[[ -z "$NETPLAN_FILE" ]] && NETPLAN_FILE="99-extra-ips.yaml"
echo "📄 Dosya: $NETPLAN_FILE"
# Yedek al
[[ -f "$NETPLAN_FILE" ]] && cp "$NETPLAN_FILE" "$NETPLAN_FILE.bak.$(date +%s)"
# Mevcut IP'leri oku
EXISTING_IPS=""
if [[ -f "$NETPLAN_FILE" ]]; then
EXISTING_IPS=$(grep -A 50 "addresses:" "$NETPLAN_FILE" | grep "^\s*-" | grep "/" | sed 's/^\s*-\s*//' | head -20)
GATEWAY=$(grep "gateway4:" "$NETPLAN_FILE" | awk '{print $2}' | head -1)
GATEWAY6=$(grep "gateway6:" "$NETPLAN_FILE" | awk '{print $2}' | head -1)
if grep -q "nameservers:" "$NETPLAN_FILE"; then
NAMESERVERS=$(grep -A 10 "nameservers:" "$NETPLAN_FILE" | grep "^\s*-" | sed 's/^\s*-\s*//')
fi
fi
# Yeni config yaz
cat > "$NETPLAN_FILE" <<EOF
network:
version: 2
ethernets:
$IFACE:
addresses:
EOF
# Mevcut IP'leri ekle
if [[ -n "$EXISTING_IPS" ]]; then
while read -r old_ip; do
[[ -n "$old_ip" ]] && echo " - $old_ip" >> "$NETPLAN_FILE"
done <<< "$EXISTING_IPS"
fi
# Yeni IP'leri ekle
for ip in "${VALID_IPS[@]}"; do
echo " - $ip" >> "$NETPLAN_FILE"
done
# Gateway
[[ -n "$GATEWAY" ]] && echo " gateway4: $GATEWAY" >> "$NETPLAN_FILE"
[[ -n "$GATEWAY6" ]] && echo " gateway6: $GATEWAY6" >> "$NETPLAN_FILE"
# Nameservers
if [[ -n "$NAMESERVERS" ]]; then
echo " nameservers:" >> "$NETPLAN_FILE"
echo " addresses:" >> "$NETPLAN_FILE"
while read -r ns; do
[[ -n "$ns" ]] && echo " - $ns" >> "$NETPLAN_FILE"
done <<< "$NAMESERVERS"
fi
netplan apply
# ============================================
# Eski Ubuntu/Debian (interfaces)
# ============================================
elif [[ "$OS" == "ubuntu" || "$OS" == "debian" ]]; then
echo "📦 /etc/network/interfaces kullanılıyor..."
IFACE_FILE="/etc/network/interfaces.d/$IFACE-extra.cfg"
for ip in "${VALID_IPS[@]}"; do
if [[ "$ip" =~ ":" ]]; then
echo "up ip -6 addr add $ip dev $IFACE" >> "$IFACE_FILE"
else
echo "up ip addr add $ip dev $IFACE" >> "$IFACE_FILE"
fi
done
systemctl restart networking 2>/dev/null || { ifdown "$IFACE"; ifup "$IFACE"; }
# ============================================
# CentOS/RHEL 7 (ifcfg)
# ============================================
elif [[ "$OS" =~ ^(centos|rhel|rocky|almalinux)$ && "$VERSION" =~ ^7 ]]; then
echo "📦 ifcfg dosyası kullanılıyor..."
IFCFG="/etc/sysconfig/network-scripts/ifcfg-$IFACE"
[[ -f "$IFCFG" ]] && cp "$IFCFG" "$IFCFG.bak.$(date +%s)"
# Mevcut en yüksek index'i bul
COUNT=$(grep -c "^IPADDR" "$IFCFG" 2>/dev/null || echo 0)
for ip in "${VALID_IPS[@]}"; do
if [[ ! "$ip" =~ ":" ]]; then
echo "IPADDR$COUNT=${ip%/*}" >> "$IFCFG"
echo "PREFIX$COUNT=${ip#*/}" >> "$IFCFG"
else
echo "IPV6ADDR_SECONDARIES=\"\${IPV6ADDR_SECONDARIES} $ip\"" >> "$IFCFG"
fi
((COUNT++))
done
systemctl restart network
# ============================================
# Desteklenmeyen OS - Sadece geçici ekle
# ============================================
else
echo "⚠️ OS tanınmadı, IP'ler geçici olarak ekleniyor..."
for ip in "${VALID_IPS[@]}"; do
if [[ "$ip" =~ ":" ]]; then
ip -6 addr add "$ip" dev "$IFACE" 2>/dev/null
else
ip addr add "$ip" dev "$IFACE" 2>/dev/null
fi
done
echo "⚠️ Bu IP'ler reboot sonrası kaybolacak!"
fi
# ============================================
# Sonuç
# ============================================
echo ""
echo "✅ İşlem tamamlandı!"
echo "📋 Eklenen IP'ler:"
for ip in "${VALID_IPS[@]}"; do
echo " • $ip"
done
echo ""
echo "🔍 Doğrulama:"
ip addr show "$IFACE" | grep -E "inet6? " | head -10
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
EOFWith 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
EOFMethod 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
EOFThen run:
./add-extra-ips.sh -f ips.txtWith custom subnet mask:
./add-extra-ips.sh -m 25 -f ips.txtOptions
| 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
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!