溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用shell腳本查看網(wǎng)絡(luò)接口信息

發(fā)布時(shí)間:2022-02-17 15:34:18 來源:億速云 閱讀:186 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“怎么使用shell腳本查看網(wǎng)絡(luò)接口信息”,在日常操作中,相信很多人在怎么使用shell腳本查看網(wǎng)絡(luò)接口信息問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用shell腳本查看網(wǎng)絡(luò)接口信息”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

怎么使用shell腳本查看網(wǎng)絡(luò)接口信息

系統(tǒng)環(huán)境

Centos7

1)檢查可用的網(wǎng)絡(luò)接口

使用ipawk命令,過濾出狀態(tài)為UP的網(wǎng)絡(luò)接口。

[root@localhost ~]# ip ad|awk '/state UP/ {print $2}'ens33:
ens38:
怎么使用shell腳本查看網(wǎng)絡(luò)接口信息

2)查看網(wǎng)絡(luò)接口的IP地址

使用下面命令過濾出每個接口的ip地址:

[root@localhost ~]# ip -o addr |awk '/inet/{print $2,$4}'lo 127.0.0.1/8
lo ::1/128
ens33 192.168.43.138/24
ens33 fe80::214e:53b4:43f6:5495/64
ens38 172.25.254.130/24
ens38 fe80::c2ff:9dbc:76be:6dd9/64
或者只查看IPv4地址:
[root@localhost ~]# ip addr | grep inet|grep -v 'inet6'|awk '{print $NF, $2}'lo 127.0.0.1/8
ens33 192.168.43.138/24
ens38 172.25.254.130/24
怎么使用shell腳本查看網(wǎng)絡(luò)接口信息

3)查看網(wǎng)卡的MAC地址

如果只想查看網(wǎng)絡(luò)接口名稱和相應(yīng)的MAC地址,請使用以下命令。檢查特定的網(wǎng)絡(luò)接口的MAC地址:

[root@localhost ~]# ip link show ens33 | awk '/link/{print $2}'00:0c:29:99:ee:d9

查看所有網(wǎng)絡(luò)接口的MAC地址,可以寫一個腳本來實(shí)現(xiàn):

[root@localhost ~]# cat mac-address.sh#!/bin/baship addr |awk '/state UP/{print $2}' | sed 's/://' | while read outputdoecho $output:
ethtool -P $outputdone

查看一下運(yùn)行結(jié)果:

  怎么使用shell腳本查看網(wǎng)絡(luò)接口信息

4)查看網(wǎng)絡(luò)接口的速度

如果要在Linux上檢查網(wǎng)絡(luò)接口端口速度,可以使用ethtool工具。下面是查看特定網(wǎng)絡(luò)接口的速度: [root@localhost ~]# ethtool ens33|grep “Speed:” Speed: 1000Mb/s

查看所有接口的網(wǎng)絡(luò)速度,可以寫一個腳本來實(shí)現(xiàn):

[root@localhost ~]# cat port-speed.sh#!/bin/baship addr |awk '/state UP/{print $2}' | sed 's/://' | while read outputdoecho $output:
ethtool $output |grep "Speed:"done

查看一下運(yùn)行結(jié)果:

  怎么使用shell腳本查看網(wǎng)絡(luò)接口信息

5)查看網(wǎng)絡(luò)接口信息的Shell腳本

下面這個腳本,我們來實(shí)現(xiàn)查看主機(jī)名、IPv4、IPv6、MAC地址、網(wǎng)絡(luò)接口速度信息:

[root@localhost ~]# cat nic-info.sh#!/bin/bashhostnameecho "-------------"for iname in $(ip addr |awk '/state UP/{print $2}')doecho "$iname"ip addr show $iname | grep inet | awk '{printf "%s:\t%s\n",$1,$2}'ip link show $iname | grep link | awk '{printf "MAC:\t%s\n",$2}'ethtool ens33 | awk '/Speed/{printf "%s\t%s\n",$1,$2}'done
怎么使用shell腳本查看網(wǎng)絡(luò)接口信息

到此,關(guān)于“怎么使用shell腳本查看網(wǎng)絡(luò)接口信息”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI