shell脚本输出多个主机的网卡速率的方法

背景:

记录下之前的写过的shell脚本,需要整理出各个主机的各个网卡速率,网卡名称为bond0到bond3,使用ethtool bond1命令可以查看相应网卡的速率。因为有几十台主机,所以考虑使用shell脚本去查询。

具体思路:

查询单台主机单网卡速率命令:

ethtool bond1 | grep Speed
Speed: 20000Mb/s

查询单台主机所有bond网卡速率命令,输出网卡名称和对应的网卡速率:

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远程执行命令:

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自动输入密码

完整脚本:

#!/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脚本方面的知识。

相关推荐