【过滤出IP地址】
ifconfig eth0 |grep -o "\<[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\>"|head -n1
说明:
0-9的数字出现1-3次
.把.转义为“.”,否则会判断为任意字符
ifconfig eth0 |grep -o "\<\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\>"|head -n1
说明:
( 表示分组
{3}表示重复3次
<表示单词首
>表示单词尾
【获取IP地址方法2】
数字 表示方法
0-9 [0-9]
10-99 [1-9][0-9]
[1-9]?[0-9] //表示0-99
100-199 1[0-9]{2}
200-249 2[0-4][0-9]
250-255 25[0-5]
IP地址用正则表达式表示如下:
(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
ifconfig |grep -Eo "(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
【获取子网掩码】
ifconfig eth1|grep HWaddr|tr -s " " "%"|cut -d% -f5
ifconfig eth1 |grep -o "\<[0-9A-F]\{1,2\}:[0-9A-F]\{1,2\}:[0-9A-F]\{1,2\}:[0-9A-F]\{1,2\}:[0-9A-F]\{1,