簡體   English   中英

Shell腳本:以表格形式顯示網絡接口及其對應的IP地址

[英]Shell script : Displaying network interface and their corresponding ip address in a table format

我想使用 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 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 腳本(希望它有幫助)

#!/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 "+-----------+-----------------+"

輸出:

+-----------+-----------------+
| 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM