Linux bash脚本提取IP地址 [英] Linux bash script to extract IP address
问题描述
我想在我的 Debian 7.3 上制作大脚本(类似翻译和更新的用户友好环境).我有个问题.我只想使用命令给我的一些信息.例如我的 ifconfig 看起来像:
I want to make big script on my Debian 7.3 ( something like translated and much more new user friendly enviroment ). I have a problem. I want to use only some of the informations that commands give me. For example my ifconfig looks like:
eth0 Link encap:Ethernet HWaddr 08:00:27:a3:e3:b0
inet addr:192.168.1.103 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fea3:e3b0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1904 errors:0 dropped:0 overruns:0 frame:0
TX packets:2002 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1309425 (1.2 MiB) T
我只想在行中显示 IP 地址:echo "Your IP address is: (IP_ADDRESS)".是否有任何命令可以让我做这样的事情,在流中搜索我想要获取的信息?.我知道 grep
和 sed
但我对它们并不擅长.
I want to display only the IP address in line: echo "Your IP address is: (IP_ADDRESS )". Is there any command that allow me to do such a thing, to search in stream for informations I want to get?. I know about grep
and sed
but I am not really good with them.
首先感谢您帮助我解决这个问题,现在我知道的更多了.其次说项目在进行中.如果有人对它感兴趣,请pm我.
Firstly to say thank you for helping me with this problem, now I know much more. Secondly to say project is in progress. If anyone would be interested in it just pm me.
推荐答案
只需获取您的 IP 地址:
To just get your IP address:
echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`
这会给你 eth0 的 IP 地址.
This will give you the IP address of eth0.
由于最近版本的 Ubuntu 中接口的名称更改,这不再起作用.相反,你可以使用这个:
Due to name changes of interfaces in recent versions of Ubuntu, this doesn't work anymore. Instead, you could just use this:
hostname --all-ip-addresses
或 hostname -I
,它们做同样的事情(给你主机的所有 IP 地址).
hostname --all-ip-addresses
or hostname -I
, which does the same thing (gives you ALL IP addresses of the host).
这篇关于Linux bash脚本提取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!