温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-22 16:19:27 来源:网络 阅读:315 作者:Eddy72 栏目:系统运维
背景:

记录下之前的写过的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脚本方面的知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI