背景:
记录下之前的写过的 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 [email protected] "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 [email protected]$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 脚本方面的知识.
来源: http://www.bubuko.com/infodetail-3338560.html