shell腳本輸出多個主機的網(wǎng)卡速率的方法
背景:
記錄下之前的寫過的shell腳本,需要整理出各個主機的各個網(wǎng)卡速率,網(wǎng)卡名稱為bond0到bond3,使用ethtool bond1命令可以查看相應(yīng)網(wǎng)卡的速率。因為有幾十臺主機,所以考慮使用shell腳本去查詢。
具體思路:
查詢單臺主機單網(wǎng)卡速率命令:
ethtool bond1 | grep Speed Speed: 20000Mb/s
查詢單臺主機所有bond網(wǎng)卡速率命令,輸出網(wǎng)卡名稱和對應(yīng)的網(wǎng)卡速率:
for i in {0..3};do echo bond$i `/usr/sbin/ethtool bond$i 2 > /dev/null | grep Speed`;done
bond0
bond1 Speed: 20000Mb/s
bond2 Speed: 20000Mb/s
bond3 Speed: 2000Mb/s
查詢遠程主機所有bond網(wǎng)卡速率命令,可以使用ssh -tt遠程執(zhí)行命令:
ssh -tt user@192.168.1.1 "command "
需要查詢的IP都在/etc/hosts文件,
文件格式:
- 192.168.1.1 compute-1
- 192.168.1.2 compute-2
篩選出192網(wǎng)段的IP
cat /etc/hosts | grep 192 | cut -d' ' -f1
使用expect自動輸入密碼
完整腳本:
#!/bin/bash
cat /etc/hosts | grep 192 | while read line
do
echo $line
ip=`echo $line | cut -d' ' -f1`
/usr/bin/expect <<-EOF
spawn ssh -tt user@$ip "for i in {0..3};do echo bond\$\i \`/usr/sbin/ethtool bond\$\i 2>/dev/null | grep Speed\`;done "
expect {
"(yes/no)?" { send "yes\n";exp_continue }
"*assword:" { send "password\n";}
}
expect eof
EOF
done
總結(jié)
對shell腳本格式還不太熟,腳本格式跟直接執(zhí)行命令出來的結(jié)果還是有不少區(qū)別的,還是需要多學習shell腳本方面的知識。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Shell腳本一鍵安裝Nginx服務(wù)自定義Nginx版本
這篇文章主要為大家介紹了Shell腳本一鍵安裝Nginx服務(wù),用戶可自定義Nginx版本的腳本示例,有需要的朋友可以借鑒參考下,希望能夠參考下2022-03-03
linux下使用ssh遠程執(zhí)行命令批量導(dǎo)出數(shù)據(jù)庫到本地
這篇文章主要介紹了linux下使用ssh遠程執(zhí)行命令批量導(dǎo)出數(shù)據(jù)庫到本地,需要的朋友可以參考下2015-04-04
shell腳本實現(xiàn)服務(wù)器進程監(jiān)控的方法
這篇文章主要介紹了shell腳本實現(xiàn)服務(wù)器進程監(jiān)控的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2018-04-04

