在 BASH 中,有什么方法可以准确找到所有接口的 IP 地址吗?并删除所有其他信息?
我怎样才能只获取IP以及它是哪个接口IP?这样我就可以保留一个记录文件,例如 realtime.ini
1 - test.sh
#!/bin/bash
ipstring ='inet (.*)'
for i in $(ip addr);
do
echo $i #on found it write down to my realtime.ini as a list for future query
done
2 - realtime.ini
em1,192.168.1.2
lo,127.0.0.1
wlan0,<not found>
后续: 仅针对单个 ip: $ ifconfig eth0 | grep 'inet 地址:' |剪切-d:-f2 | awk '{打印$1}' 192.168.1.2
How can i get only IP and which interface IP it is? So that i can keep a record file such as realtime.ini
1 - test.sh
#!/bin/bash
ipstring ='inet (.*)'
for i in $(ip addr);
do
echo $i #on found it write down to my realtime.ini as a list for future query
done
2 - realtime.ini
em1,192.168.1.2
lo,127.0.0.1
wlan0,<not found>
Follow up: Just for single ip:
$ ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
192.168.1.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是非常优雅,bash 也不是,但是如果您同时拥有 awk 和 sed,您可以执行以下操作:
我也不认为它具有巨大的可移植性,所以也许有人有更好的答案。
This is not terribly elegant, nor is bash, but you can do the following if you have both awk and sed:
I wouldn't bet on this being hugely portable either, so maybe someone has a better answer.
如果您安装
moreutils
软件包,您可以使用方便的ifdata
命令:5 个接口的示例输出,而只有
eth0
和lo 已启动:
if you install
moreutils
package, you can use handyifdata
command:example output for 5 interfaces, while only
eth0
andlo
are up: