Calvin's Tech Space

          成于堅(jiān)忍,毀于浮躁

             :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
          Linux下:

          ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' |
          cut -d: -f2 | awk '{ print $1}'

          先獲取含有IP的行,再去掉含有127.0.0.1的行。最后獲取IP所在的列

          #!/bin/sh
          # Shell script scripts to read ip address
          # -------------------------------------------------------------------------
          # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
          # This script is licensed under GNU GPL version 2.0 or above
          # -------------------------------------------------------------------------
          # This script is part of nixCraft shell script collection (NSSC)
          # Visit http://bash.cyberciti.biz/ for more information.
          # -------------------------------------------------------------------------
          # Get OS name
          OS=`uname`
          IO="" # store IP
          case $OS in
          Linux) IP=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
          FreeBSD|OpenBSD) IP=`ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
          SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
          *) IP="Unknown";;
          esac
          echo "$IP"

          posted on 2010-04-03 14:26 calvin 閱讀(3719) 評論(0)  編輯  收藏 所屬分類: Linux
          主站蜘蛛池模板: 宜丰县| 肇庆市| 区。| 拜城县| 江都市| 扎囊县| 佛冈县| 阳高县| 城口县| 本溪| 麦盖提县| 湖南省| 永德县| 西昌市| 大丰市| 弋阳县| 商城县| 旺苍县| 喀什市| 九台市| 萝北县| 平远县| 合阳县| 天气| 沙田区| 小金县| 青州市| 弋阳县| 清苑县| 白水县| 云梦县| 平泉县| 大连市| 赤水市| 南阳市| 合水县| 崇左市| 肇东市| 会东县| 焉耆| 通化县|