Linux
[Linux] Shell Script๋ฅผ ์ด์ฉํ์ฌ ๋คํธ์ํฌ IP ์ค์
The Engineer, Lucy
2025. 6. 25. 14:23
ํ์ํ ์ ๋ณด
# ๋ฐ๋น์ ๊ณ์ด์์ ๋คํธ์ํฌ ์ค์ ๋ฐฉ๋ฒ
18.04 LTS ๋ฒ์ ๋ถํฐ netplan ํ์ผ ์ค์ ์ผ๋ก ๋ณ๊ฒฝ
# ํ๋๋ผ ๊ณ์ด์์ ๋คํธ์ํฌ ์ค์ ๋ฐฉ๋ฒ
8 ๋ฒ์ ๋ถํฐ nmcli ๋ช
๋ น์ด๋ฅผ ์ด์ฉํ์ฌ ์ค์
# ๋คํธ์ํฌ IP ์ค์ ์ ํ์ํ ์ ๋ณด๋ค
Network Interface Name
IP/CIDR
Gateway
DNS
์คํฌ๋ฆฝํธ
#!/bin/bash
ostype=$(cat /etc/*releas | grep ID_LIKE | sed "s/ID_LIKE=//;s/\"//g")
echo "=== Network Devices ==="
ip a | grep '^[0-9]' | awk '{print $1" " $2}' | grep -v -e 'lo' -e 'v' -e 't'
read -p "Please input network interface: " net_name
read -p "Please input network ip(ex:192.168.122.10/24): " net_ip
read -p "Please input network gateway: " net_gw
read -p "Please input network dns: " net_dns
if [[ -z $net_name ]] || [[ -z $net_ip ]] || [[ -z $net_gw ]] || [[ -z $net_dns ]]
then
echo "You need to input network information. Please retry this script"
exit;
fi
if [[ $ostype == "fedora" ]]
then
nmcli con add con-name $net_name type ethernet ifname $net_name ipv4.address $net_ip ipv4.gateway $net_gw ipv4.dns $net_nds ipv4.method manual
nmcli con up $net_name
elif [[ $ostype == "debian" ]]
then
ip_chk=$(grep $net_name /etc/netplan/*.yaml wc -l)
if [ $ip_chk -eq 0 ]
then
cat > /etc/netplan/${net_name}.yaml << EOF
network:
version: 2
renderer: networkd
ethernets:
$net_name:
dhcp4: no
dhcp6: no
addresses: [$net_ip]
gateway4: $net_gw
nameservers:
addresses: [$net_dns]
EOF
echo "cat /etc/netplan/${net_name}.yaml"
cat /etc/netplan/${net_name}.yaml
echo "apply netplan"
netplan apply
else
echo "This $net_name is configured already."
fi
fi
ํ๋๋ผ ๊ณ์ด์ ๋ ๋ํ์์ ๋คํธ์ํฌ IP๋ฅผ ์ค์ ํ ์ ์์ผ๋ฉฐ ๋ฐ๋น์ ๊ณ์ด์ธ ์ฐ๋ถํฌ์์๋ ๋คํธ์ํฌ IP ์ค์ ๊ฐ๋ฅ.
๋ ๋ํ์์๋ sh ๋ช
๋ น์ด๋ก ์คํ ๊ฐ๋ฅํ๋ ์ฐ๋ถํฌ์์๋ bash ๋ช
๋ น์ด๋ก ์คํํด์ผ ํจ.