最近为了检测公司服务器的硬盘需要开10多台服务器的僚机来检测服务器,可是这10来台都是操作一样的命令,挨个操作下去太麻烦了
然后就想到了交互式登陆 这里需要创建一个Ip文件夹把你的Ip账户密码都放进去,然后Expect执行的时候会直到循环到最后才会结束,是不是可好玩呢。
expect交互式可是需要安装的,可以直接yum install expect -y 当然你也可以编译安装,我喜欢yum的,方便,这里就不多说了,贴上代码
代码如下:
#!/usr/bin/expect -f set f [open ./ip r] while { [gets $f line ]>=0 } { set ip [lindex $line 0] set pwd [lindex $line 2] spawn ssh -o StrictHostKeyChecking=no $ip expect "*password:" { send "$pwd " } expect "#" send "ifconfig " send "iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 123 -j ACCEPT " send "service iptables save " send "yum install ntpdate -y " send "/usr/sbin/ntpdate 0.cn.pool.ntp.org " send "echo '*/10 * * * * sh /root/testdisk.sh' > /var/spool/cron/root " send "echo '10 5 * * * /usr/sbin/ntpdate 0.cn.pool.ntp.org' >> /var/spool/cron/root " send "exit " interact }
磁盘检测的脚本如下:
#!/bin/bash PATH=/bin:/etc:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH dd if=/dev/zero of=/test.dbf bs=1024k count=1000 2>writedisk.txt if [ $? -eq 0 ] then w=`tail -n 1 writedisk.txt|cut -d, -f3 |sed 's/ //g'` dd if=/test.dbf of=/dev/null bs=1024k 2>readdisk.txt if [ $? -eq 0 ] then r=`tail -n 1 readdisk.txt|cut -d, -f3 |sed 's/ //g'` s=`date -d today +"%Y-%m-%d,%T"` echo $s--$r,$w >> w.txt else echo "fail" fi else echo "fail" fi