`
wxl24life
  • 浏览: 290656 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Shell 命令:如何从 ifconfig 输出结果中截取 IP 地址

阅读更多

匹配的关键是正则表达式。工具可以考虑 grep 或者 sed。

 

方案1:

借助 grep:

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

 

方案2:

借助 sed:

ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

 注:sed 的 -E 选项依赖于具体的 sed 版本。如果 -E 无效,请使用 -r 替代。

 

另外,可以通过 ifconfig 指定要显示的的 interface。

ifconfig eth0 | ...

 

我在具体实践时,使用了方案2 并指定了 interface。

SERVER_IP=$(/sbin/ifconfig eth0 | sed -nr 's/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')

 

参见:SO

 

分享到:
评论

相关推荐

    从ifconfig中得到IP地址

    使用 ifconfig -a 就可以得到所有网络界面的 IP 地址,但是这些 IP 地址都混在其他信息之中。 你需要从中分离出来,下面的脚本就可以帮助你直接输出 IP 地址:

    ifconfig不显示ip, 虚拟机静态IP设置方法

    有些同学在使用虚拟机时 ifconfig 不显示ip 地址,或者每次开机ubuntu的IP 都在变化,如何解决呢?

    虚拟机里输入ifconfig命令出现错误-bash: ifconfig: command not found

    1、查看是否真设置IP地址:命令行中输入ip addr,看到ip地址。 2.确认sbin目录是否存在;在命令行中输入:cd /sbin 3、确认是否安装ifconfig命令:在sbin目录中输入ls | grep ‘if’,没有则说明没有安装。 注:...

    使用ifconfig命令查看linux系统ip地址提示命令不存在,yum安装ifconfig命令,发现了没有连接网络的问题

    新手安装linux总会遇到奇奇怪怪的bug,我使用ifconfig命令查看linux系统ip地址,提示命令不存在 遇到这个问题想到的是没有这个命令那就安装咯, 修复步骤: 1.首先确认是否是环境变量没有ifconfig引起:输入命令: echo $...

    Linux的ip命令和ifconfig

    Linux的ip命令和ifconfig

    ifconfig命令详解

    详细介绍了ifconfig命令的用法

    Shell脚本获取本地网卡IP、mac地址、子网掩码、dns IP、外网IP

    #/usr/bin/env bash # Name: get_network_info.sh # Author: Purple_Grape # This is a script to gather network information of your Linux system. # Test under Ubuntu 10.04 ...IP=`LANG=C ifconfig $NIC | awk

    Linux必学的60个命令.txt

    ◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who; ◆ 其它...

    Linux找不到ifconfig命令

    Linux找不到ifconfig命令,实现ifconfig的配置文件.

    linux必学60命令

    ◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、 finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、 who; ◆ 其它...

    学习Linux必备常用的60个命令

    ◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who; ◆ 其它命令...

    如何固定Linux系统的IP地址

    今天想连接winSCP,发现连不上,原来Linux系统的IP地址变了,为了避免IP地址的变动,我就想固定IP地址,方法如下: 1、输入ifconfig(如果CentOS是最小化安装,是没有ifconfig命令的)或ip addr ,可以查看网络相关...

    Linux必学的60个命令

    ◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who; ◆ 其它命令...

    Linux必学的60个命令.doc

    ◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who; ◆ 其它...

    linux常用命令大全.doc

    Linux是一个强大的操作系统,提供了许多常用的命令行工具,用于管理文件、目录、进程、网络和系统配置等。以下是一些Linux的常用命令: 文件管理命令: ...ifconfig或ip addr:查看或配置网络接口信息。 netstat

    linux命令大全

    ◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、 finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、 who; ◆ 其它...

    VxWorks 6.9下的ifconfig网络配置命令.pdf

    VxWorks 6.9下的ifconfig网络配置命令

    email_ifconfig:启动后通过电子邮件发送ifconfig,ip地址和任何其他CLI输出命令的服务

    email_ifconfig 启动后通过电子邮件发送ifconfig,ip地址和任何其他CLI输出命令的服务

    linux必须的60个命令

    ◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who; ◆ 其它命令...

Global site tag (gtag.js) - Google Analytics