Created
April 20, 2023 09:25
-
-
Save foru17/aac60fbc4d2e251259d44dbd20428324 to your computer and use it in GitHub Desktop.
Auto check and renew dhcp ip
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 | |
# 记录日志时间 | |
log_time=$(TZ=UTC-8 date +"%Y-%m-%d-%H:%M:%S") | |
log_dir="/www/wwwlogs/" | |
server_name="DHCP" | |
# 获取 ens192 网卡对应的 IP 地址和子网掩码 | |
ipaddr=$(ip addr show ens192 | grep -w inet | awk '{print $2}') | |
netmask=$(ip addr show ens192 | grep -w inet | awk '{print $2}' | awk -F '/' '{print $2}') | |
# 判断 IP 地址是否为局域网 IPV4 地址(以 10.x.x.x、172.16.x.x 至 172.31.x.x、192.168.x.x 开头) | |
if [[ $ipaddr =~ ^10\.|^172\.(1[6-9]|2[0-9]|3[0-1])\.|^192\.168\. ]] | |
then | |
echo "$log_time | 💚 | 在线 | $server_name" >> "$log_dir/dhcp.watch.log" | |
echo "ens192 网卡已有局域网 IPV4 地址,无需更新。" | |
else | |
echo "$log_time | ⚠️ | 离线 | $server_name" >> "$log_dir/dhcp.watch.log" | |
echo "ens192 网卡没有局域网 IPV4 地址,正在更新 IP,请稍候..." | |
dhclient ens192 | |
echo "IP 更新完成。" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment