在 BASH 中,有没有办法准确找到所有接口的 IP 地址?并删除所有其他信息? [英] In BASH, Is there any way to find exactly the IP address for all interfaces? And remove all other information's?
问题描述
我怎样才能只得到IP和它是哪个接口IP?这样我就可以保留一个记录文件,例如 realtime.ini
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
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
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
Follow up: Just for single ip: $ ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' 192.168.1.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:
ifconfig | awk 'BEGIN { FS = "\n"; RS = "" } { print $1 $2 }' | sed -e 's/ .*inet addr:/,/' -e 's/ .*//'
我也不会打赌它非常便携,所以也许有人有更好的答案.
I wouldn't bet on this being hugely portable either, so maybe someone has a better answer.
这篇关于在 BASH 中,有没有办法准确找到所有接口的 IP 地址?并删除所有其他信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!