Linux命令行获取本机IP

  1. # perl
  2. [root@Nagios ~]# ifconfig|perl -nle'/dr:([^ ]+)/&&print$1'
  3. 192.168.0.200
  4. 127.0.0.1
  5.  
  6. # perl,够帅
  7. [root@Nagios ~]# ip addr|perl -pe '$_=/t (.*)\//?$1.$/:""'
  8. 127.0.0.1
  9. 192.168.0.200
  10.  
  11. # awk
  12. [root@Nagios ~]# ifconfig|awk -F':| *' '/inet /{print $4}'
  13. 192.168.0.200
  14. 127.0.0.1
  15.  
  16. # sed
  17. [root@Nagios ~]# ifconfig|sed -rn 's/.*r:([^ ]+) .*/\1/p'
  18. 192.168.0.200
  19. 127.0.0.1
  20.  
  21. # shell
  22. [root@Nagios ~]# ifconfig|grep 'inet addr:'|while read X Y Z; do echo $Y;done|(IFS=':';while read X Y;do echo $Y ;done)
  23. 192.168.0.200
  24. 127.0.0.1
1 1