Shell正则表达式取IP地址

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Gao_ning/article/details/79431899

Shell正则表达式匹配取IP地址

  • 演示服务器为虚拟服务器:
[root@GaoServer ~]# ifconfig 
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.47.140  netmask 255.255.255.0  broadcast 192.168.47.255
        inet6 fe80::20c:29ff:fe73:14b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:73:01:4b  txqueuelen 1000  (Ethernet)
        RX packets 991  bytes 81056 (79.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 474  bytes 64248 (62.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 取IP地址:
[root@GaoServer ~]# ifconfig eno16777736 | grep -Eoe "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" -Eoe '^(e|b)[[:lower:]]+[[:digit:]]+?'
eno16777736
192.168.47.140
255.255.255.0
192.168.47.255
  • 部分物理服务器以em1,或bond等网卡名,可根据需求修改;
    主要以正则:
    (([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])来过滤合理IP地址;
阅读更多

没有更多推荐了,返回首页