初识awk-获取当前机器的global ipv6地址

获取当前机器的global ipv6地址

使用方法
awk 'pattern {action}' {filenames}

首先,用ifconfig命令可以查看当前机器的所有ip地址
[root@lc ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 38:83:45:F1:33:33
          inet addr:192.168.1.13  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fc00:0:190::13/64 Scope:Global
          inet6 addr: fe80::3a83:45ff:fef1:3333/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3097 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3404 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:380337 (371.4 KiB)  TX bytes:400481 (391.0 KiB)
          Interrupt:10 Base address:0xe000


eth1      Link encap:Ethernet  HWaddr 50:E5:49:CF:97:BE 
          inet addr:192.168.106.239  Bcast:192.168.106.255  Mask:255.255.255.0
          inet6 addr: fc00:0:190::221/64 Scope:Global
          inet6 addr: fe80::52e5:49ff:fecf:97be/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:74167 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25655 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:8107031 (7.7 MiB)  TX bytes:25823486 (24.6 MiB)
          Interrupt:97

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:13416 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13416 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:6212889 (5.9 MiB)  TX bytes:6212889 (5.9 MiB)

[root@lc ~]#

然后,用awk搜索Global所在的行:
[root@lc ~]# ifconfig | awk -F: '/Global/'
          inet6 addr: fc00:0:190::13/64 Scope:Global
          inet6 addr: fc00:0:190::221/64 Scope:Global
[root@lc ~]#

接着以"addr:"或者"/64"为分隔符,打印第二列:

$0变量是指整条记录。$1表示当前行的第一个域,$2表示当前行的第二个域,......以此类推。
[root@lc ~]# ifconfig |awk -F: '/Global/'| awk -F'addr:|/64' '{print $2}'
  fc00:0:190::13
  fc00:0:190::221
[root@lc ~]#

成功!!

Monday, October 19, 2015 | IPv6 Networking

文章评论

No comments posted yet.

发表评论

Please add 6 and 3 and type the answer here: