千家信息网

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

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,背景:记录下之前的写过的shell脚本,需要整理出各个主机的各个网卡速率,网卡名称为bond0到bond3,使用ethtool bond1命令可以查看相应网卡的速率。因为有几十台主机,所以考虑使用sh
千家信息网最后更新 2025年02月03日shell脚本输出多个主机的网卡速率
背景:

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

具体思路:

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

ethtool bond1 | grep SpeedSpeed: 20000Mb/s

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

for i in {0..3};do echo bond$i `/usr/sbin/ethtool bond$i 2 > /dev/null | grep Speed`;donebond0bond1 Speed: 20000Mb/sbond2 Speed: 20000Mb/sbond3 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/bashcat /etc/hosts | grep 192 | while read linedoecho $lineip=`echo $line | cut -d' ' -f1`/usr/bin/expect <<-EOFspawn 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 eofEOFdone
总结

对shell脚本格式还不太熟,脚本格式跟直接执行命令出来的结果还是有不少区别的,还是需要多学习shell脚本方面的知识。

0