[英]Shell script : Displaying network interface and their corresponding ip address in a table format
I want to display network interface and their ip address in a table format using shell script.我想使用 shell 脚本以表格格式显示网络接口及其 IP 地址。 I have this code
我有这个代码
#! /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
The output of this code is这段代码的输出是
eth0 vmnet1 vmnet8
10.30.10.226 192.168.142.1 192.168.3.1
I want like this我想要这样
eth0 10.30.10.226
vmnet1 192.168.142.1
vmnet8 192.168.3.1
Thank you in advance先感谢您
Rearrange it like this:像这样重新排列它:
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
Here is my shell script (hope it helps) :这是我的 shell 脚本(希望它有帮助) :
#!/bin/bash
clear
echo -e "+-----------+-----------------+\n| Interface | IP Address |\n+-----------+-----------------+"
for iface in $(ip -o -4 addr list | awk '{print $2}' | tr '\n' ' ')
do
ipaddr=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1)
printf "|%10s | %-16s|\n" $iface $ipaddr
done
echo "+-----------+-----------------+"
Output:输出:
+-----------+-----------------+
| Interface | IP Address |
+-----------+-----------------+
| lo | 127.0.0.1 |
| enp0s31f6 | 10.10.10.100 |
| wlp61s0 | 10.11.11.111 |
| tun0 | 10.20.20.200 |
+-----------+-----------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.