shell腳本輸出多個主機的網卡速率

背景:

記錄下以前的寫過的shell腳本,須要整理出各個主機的各個網卡速率,網卡名稱爲bond0到bond3,使用ethtool bond1命令能夠查看相應網卡的速率。由於有幾十臺主機,因此考慮使用shell腳本去查詢。shell

具體思路:

查詢單臺主機單網卡速率命令:bash

ethtool bond1 | grep Speed
Speed: 20000Mb/s

查詢單臺主機全部bond網卡速率命令,輸出網卡名稱和對應的網卡速率:ssh

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網卡速率命令,能夠使用ssh -tt遠程執行命令:ide

ssh -tt user@192.168.1.1 "command "

須要查詢的IP都在/etc/hosts文件,
文件格式:
192.168.1.1 compute-1
192.168.1.2 compute-2
篩選出192網段的IP學習

cat /etc/hosts | grep 192  |  cut -d' ' -f1

使用expect自動輸入密碼spa

完整腳本:

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

總結

對shell腳本格式還不太熟,腳本格式跟直接執行命令出來的結果仍是有很多區別的,仍是須要多學習shell腳本方面的知識。code

相關文章
相關標籤/搜索