Bash Shell脚本:以表格格式显示网络接口及其相应的ip地址
我想使用shell脚本以表格格式显示网络接口及其ip地址。我有这个密码Bash Shell脚本:以表格格式显示网络接口及其相应的ip地址,bash,sh,Bash,Sh,我想使用shell脚本以表格格式显示网络接口及其ip地址。我有这个密码 #! /bin/bash interface=$(ifconfig | cut -d ' ' -f1| tr '\n' ' ' | tee save.tmp) ip=$(ifconfig | awk -F':' '/inet addr/&&!/127.0.0.1/{split($2,_," ");print _[1]}') echo $interface echo $ip 这段代码的输出是 eth0 vmn
#! /bin/bash
interface=$(ifconfig | cut -d ' ' -f1| tr '\n' ' ' | tee save.tmp)
ip=$(ifconfig | awk -F':' '/inet addr/&&!/127.0.0.1/{split($2,_," ");print _[1]}')
echo $interface
echo $ip
这段代码的输出是
eth0 vmnet1 vmnet8
10.30.10.226 192.168.142.1 192.168.3.1
我想要这样
eth0 10.30.10.226
vmnet1 192.168.142.1
vmnet8 192.168.3.1
提前谢谢你重新安排如下:
for iface in $(ifconfig | cut -d ' ' -f1| tr '\n' ' ')
do
addr=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1)
printf "$iface\t$addr\n"
done
这是我的shell脚本(希望有帮助): 输出:
+-----------+-----------------+
| Interface | IP Address |
+-----------+-----------------+
| lo | 127.0.0.1 |
| enp0s31f6 | 10.10.10.100 |
| wlp61s0 | 10.11.11.111 |
| tun0 | 10.20.20.200 |
+-----------+-----------------+
如果发布
ifconfig
和ip
命令的输出,可能会跳过一些处理步骤
+-----------+-----------------+
| Interface | IP Address |
+-----------+-----------------+
| lo | 127.0.0.1 |
| enp0s31f6 | 10.10.10.100 |
| wlp61s0 | 10.11.11.111 |
| tun0 | 10.20.20.200 |
+-----------+-----------------+