#!/bin/bash
p=`pwd`
file=$p/localip.txt
if [ -f "$file" ];then
rm $file
touch $file
fi#Ubuntu系统
if [[ -f /etc/debian_version ]]
then
for IP in $(ip a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | cut -d "/" -f1)
do
echo ${IP} >> localip.txt
done
#centos系统
else [[ -f /etc/centos-release ]]
for IP in $(ip a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{pri nt $2}' | cut -d "/" -f1)
do
echo ${IP} >> localip.txt
done
fi
获取本地IP脚本
本文介绍了一种使用bash脚本自动获取Ubuntu和CentOS系统下所有非回环网卡的有效IPv4地址的方法,并将这些IP地址保存到一个名为localip.txt的文件中。此脚本适用于需要自动化获取服务器IP地址的场景。

被折叠的 条评论
为什么被折叠?



