Chinaunix

标题: IP地址如何精确截取呢 [打印本页]

作者: last_day_1983    时间: 2014-03-05 12:37
标题: IP地址如何精确截取呢
[oracle@SAAS2 liang]$ cd
[oracle@SAAS2 ~]$ /sbin/ifconfig -a
eth0      Link encap:Ethernet  HWaddr 08:00:271:8C:91  
          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fed1:8c91/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1173 errors:0 dropped:0 overruns:0 frame:0
          TX packets:696 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:145556 (142.1 KiB)  TX bytes:103190 (100.7 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:40 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:6114 (5.9 KiB)  TX bytes:6114 (5.9 KiB)

sit0      Link encap:IPv6-in-IPv4  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

如我要得到红色部分。谢谢,另外有的服务器上可能有多个IP。
作者: 这个冬天不冷    时间: 2014-03-05 12:46
  1. [root@syh test]# ifconfig |awk  '/inet addr/{gsub(/addr:/,"",$2);print $2}'
  2. 192.168.11.231
  3. 127.0.0.1
  4. [root@syh test]#
复制代码

作者: yestreenstars    时间: 2014-03-05 12:54
  1. grep -Po '(?<=inet addr:)\S+'
复制代码

作者: rogantianwz    时间: 2014-03-05 13:04
回复 3# yestreenstars


    这个不错,很简洁。肯定逆序环视
作者: rdcwayx    时间: 2014-03-05 13:10
本帖最后由 rdcwayx 于 2014-03-05 13:11 编辑

可以再短些 (只适合GNU grep):
  1. grep -Po 'inet addr:\K\S+' file
复制代码

作者: jason680    时间: 2014-03-05 13:12
本帖最后由 jason680 于 2014-03-05 13:15 编辑

回复 1# last_day_1983

# /sbin/ifconfig -a | awk -F"[ \t:]+" -vdev=eth0 '$2=="Link"{d=$1==dev?1:0}d&&/inet addr:/{print $4}'
192.168.1.111

# /sbin/ifconfig eth0 | awk -F"[ \t:]+" '/inet addr:/{print $4}'
192.168.1.111



作者: yestreenstars    时间: 2014-03-05 13:13
回复 5# rdcwayx

\K是什么?
   
作者: huang6894    时间: 2014-03-05 13:19
本帖最后由 huang6894 于 2014-03-05 13:20 编辑
  1. => /sbin/ifconfig -a|awk '/eth/{t=1};/lo/{t=0};/inet addr/&&t{gsub(/addr:/,"",$2);print $2}'
  2. 10.1.10.62
  3. 192.168.9.107
复制代码
菜菜来一个
作者: rdcwayx    时间: 2014-03-05 13:20
本帖最后由 rdcwayx 于 2014-03-05 13:26 编辑
yestreenstars 发表于 2014-03-05 13:13
回复 5# rdcwayx

\K是什么?
  1. \K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
复制代码
就是个重置刷新的标志,\K前的都不被包括在输出匹配里。

请看这里的演示:
http://regex101.com/r/gW3qP0
作者: yestreenstars    时间: 2014-03-05 13:25
回复 9# rdcwayx

学习了,我能猜到它大概的作用,不过我想知道从哪可以获取这些正则的解释,既然有忽略前面的,那应该也有对应忽略后面的吧?
   
作者: rdcwayx    时间: 2014-03-05 13:28
yestreenstars 发表于 2014-03-05 13:25
回复 9# rdcwayx

学习了,我能猜到它大概的作用,不过我想知道从哪可以获取这些正则的解释,既然有忽略 ...

我的理解是,这个是从perl里借来的概念, 因为(-P)参数。忽略后面的参数就不知道了,或许去perl问问,有人能回答。
作者: jackhuntcn    时间: 2014-03-05 13:31
  1. ifconfig | awk -vRS='inet addr:' 'NR!=1{print $1}'
复制代码

作者: yestreenstars    时间: 2014-03-05 13:47
回复 11# rdcwayx

Anyway, thanks.
   
作者: Shell_HAT    时间: 2014-03-05 14:03
回复 5# rdcwayx


可能GNU的高版本才支持
[root]# grep -V | head -1
grep (GNU grep) 2.5.1
[root]# ifconfig eth0 | grep -Po 'inet addr:\K\S+'
[root]#

作者: Shell_HAT    时间: 2014-03-05 14:08
  1. ifconfig | sed -n '/eth0/{N;s/.*inet addr://;s/ .*//p}'
复制代码
  1. ifconfig | awk '/eth0/{getline;gsub(/addr:/,"",$2);print $2}'
复制代码
  1. ifconfig eth0 | sed '/inet addr/!d; s/.*inet addr://; s/ .*//'
复制代码
  1. ifconfig eth0 | awk -F '[: ]+' '/inet addr/{print $4}'
复制代码
  1. ifconfig eth0 | awk '/inet addr/{gsub(/addr:/,"",$2);print $2}'
复制代码
  1. ip r | awk 'NR==1{print $NF}'
复制代码

作者: haokoo    时间: 2014-03-05 14:10
ifconfig | awk -F "[ :]+" '$0~/inet addr/{print $4}'
作者: shangjieok    时间: 2014-03-05 14:26
不错。。。。
作者: last_day_1983    时间: 2014-03-05 14:40
bash-3.00$ ifconfig -a
en5: flags=1e080863,c0<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(INACTIVE),LARGESEND,CHAIN>
        inet 10.0.0.13 netmask 0xffffff00 broadcast 10.0.0.255
en8: flags=5e080863,c0<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),PSEG,LARGESEND,CHAIN>
        inet 172.16.19.67 netmask 0xffffffc0 broadcast 172.16.19.127
        inet 172.16.19.68 netmask 0xffffff00 broadcast 172.16.19.255
         tcp_sendspace 131072 tcp_recvspace 65536 rfc1323 0
en9: flags=5e080863,c0<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),PSEG,LARGESEND,CHAIN>
        inet 192.168.0.2 netmask 0xffffffc0 broadcast 192.168.0.63
         tcp_sendspace 131072 tcp_recvspace 65536 rfc1323 0
lo0: flags=e08084b<UP,BROADCAST,LOOPBACK,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT>
        inet 127.0.0.1 netmask 0xff000000 broadcast 127.255.255.255
        inet6 ::1/0
         tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1

这上面的红色IP如何出来啊。AIX系统的
作者: yestreenstars    时间: 2014-03-05 14:45
回复 18# last_day_1983

稍微改一下就好了:
  1. grep -Po 'inet \K\S+'
复制代码

作者: Shell_HAT    时间: 2014-03-05 15:46
回复 19# yestreenstars


    AIX估计不支持
作者: yestreenstars    时间: 2014-03-05 15:55
回复 20# Shell_HAT

啊,我没有注意到LZ有提到这一点,AIX这个奇葩系统,我真是没头绪了~
   
作者: rogantianwz    时间: 2014-03-05 16:04
回复 14# Shell_HAT


    我的
  1. grep (GNU grep) 2.5.1
复制代码
不好使
作者: jason680    时间: 2014-03-05 18:22
本帖最后由 jason680 于 2014-03-05 18:27 编辑

回复 18# last_day_1983

try this one (it works on Solaris)

# ifconfig -a | awk '/inet /{print $2}' | grep -v "127.0.0.1"
   
作者: jeffreyst    时间: 2014-03-05 19:14
ifconfig | sed -r '/\binet\b/!d; s/.*addr\S+) .*/\1/'
作者: prcardin    时间: 2014-03-05 19:45
ifconfig eth0 | grep "inet addr"|cut -d":" -f2|cut -d" " -f1
作者: tzjz_8    时间: 2014-03-13 11:13
本帖最后由 tzjz_8 于 2014-03-12 19:13 编辑

ifconfig | grep -oP '(?<=r:).*(?= B)|(?<=k:).*(?=.?)' | xargs -n2 | tr ' ' '/'
作者: yuloveban    时间: 2014-03-13 11:21
回复 3# yestreenstars


    这个能解释下吗? 看不懂啊~
作者: yestreenstars    时间: 2014-03-13 11:25
回复 27# yuloveban

去看一下零宽断言吧~
   
作者: runintostar    时间: 2014-03-13 11:31
本帖最后由 runintostar 于 2014-03-13 11:33 编辑

回复 1# last_day_1983
我想说holy shit啊.
用着这么好的操作系统,却要去找什么精确匹配?

LZ咱们能不能给点力?
  1. lsattr -l en0 -E -a netaddr -F "value"
复制代码
  1. lsdev -C -c if -s EN -S a -F "name"
复制代码
  1. for a in `lsdev -C -t en -S a -F "name"`
  2. do echo "${a} ip address is:`lsattr -E -l $a -a netaddr -F "value"`"
  3. done
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2