使用shell获取ip地址

    技术2022-05-19  22

    linux wiki

    s

     

    ifconfig返回的信息中包括IP地址,但要在Shell中获取当前IP地址,则要麻烦一些

    获取方法

    由于不同系统中ifconfig返回信息的格式有一定差别,故分开讨论:[1]

    Linux:

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

    FreeBSD/OpenBSD:

    LC_ALL=C ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'

    Solaris:

    LC_ALL=C ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2}'

    三段代码的原理类似,都是先获取含有IP的行,再去掉含有127.0.0.1的行。最后获取IP所在的列

    样例代码

    下面是一则示例的代码[2]

    #!/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"

    代码出处

    ↑ http://www.cyberciti.biz/tips/read-unixlinux-system-ip-address-in-a-shell-script.html↑ http://bash.cyberciti.biz/misc-shell/read-local-ip-address/ 取自" http://www.linux-wiki.cn/index.php?title=Shell中获取当前IP地址&variant=zh-cn" 使用shell获取ip地址 http://www.iyouf.info/shell-parse-ifconfig.html

    写一个shell获取ip地址顺便练习一下shell中的几个很重要的命令 //add by dengmroot@ubuntu:~# ifconfigeth0 Link encap:以太网 硬件地址 00:22:64:aa:7a:d2 inet 地址:192.168.16.94 广播:192.168.16.255 掩码:255.255.255.0inet6 地址: fe80::222:64ff:feaa:7ad2/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1接收数据包:149882 错误:0 丢弃:0 过载:0 帧数:0发送数据包:107777 错误:0 丢弃:0 过载:0 载波:0碰撞:0 发送队列长度:1000 接收字节:146616722 (146.6 MB) 发送字节:13908584 (13.9 MB)中断:26 基本地址:0xc000 lo Link encap:本地环回 inet 地址:127.0.0.1 掩码:255.0.0.0inet6 地址: ::1/128 Scope:HostUP LOOPBACK RUNNING MTU:16436 跃点数:1接收数据包:18552 错误:0 丢弃:0 过载:0 帧数:0发送数据包:18552 错误:0 丢弃:0 过载:0 载波:0碰撞:0 发送队列长度:0 接收字节:1838910 (1.8 MB) 发送字节:1838910 (1.8 MB)

    这家伙给我显示了这么一屏幕首先通过grep 取出含有inet行root@ubuntu:~# ifconfig |grep 'inet'inet 地址:192.168.16.94 广播:192.168.16.255 掩码:255.255.255.0inet6 地址: fe80::222:64ff:feaa:7ad2/64 Scope:Linkinet 地址:127.0.0.1 掩码:255.0.0.0inet6 地址: ::1/128 Scope:Host再过滤掉 127.0.0.1 这里用到 -v 这是一个很有用的参数root@ubuntu:~# ifconfig |grep 'inet'|grep -v '127.0.0.1'inet 地址:192.168.16.94 广播:192.168.16.255 掩码:255.255.255.0inet6 地址: fe80::222:64ff:feaa:7ad2/64 Scope:Linkinet6 地址: ::1/128 Scope:Hostroot@ubuntu:~# ifconfig |grep 'inet'|grep -v '127.0.0.1' |grep -v 'Link'这一步是因为有些主机的ip绑定到eth1上inet 地址:192.168.16.94 广播:192.168.16.255 掩码:255.255.255.0inet6 地址: ::1/128 Scope:Hostroot@ubuntu:~# ifconfig |grep 'inet'|grep -v '127.0.0.1'|grep -v 'Link'|sed -n '1p'inet 地址:192.168.16.94 广播:192.168.16.255 掩码:255.255.255.0sed -n '1p' 这句号是取第一行的文本到这里就可以通过分割字符的方法了 1 用awk 2 使用 cutawk:

    root@ubuntu:~# ifconfig |grep inet|grep -v "127.0.0.1"|sed -n '1p'|awk '{print $2}'地址:192.168.16.94root@ubuntu:~# ifconfig |grep inet|grep -v "127.0.0.1"|sed -n '1p'|awk '{print $2}'|awk -F ':' '{print $2}'192.168.16.94

    cut cut的用法可以看这里

    root@ubuntu:~# ifconfig |grep 'inet'|grep -v '127.0.0.1'|sed -n '1p'| cut -d ':' -f2 |awk '{print $1}'192.168.16.94 http://www.iyouf.info/shell-parse-ifconfig.html

    最新回复(0)