『练习1』:将1~10台主机中开着的主机的主机名放入host_list文件
使用函数调用+expect自动应答命令+for语句实现
#!/bin/bash
AUTO_CONNECT()
{
/usr/bin/expect <<EOF
spawn ssh root@$1 $2
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$PASSWORD\r" }
}
expect eof
EOF
}
for NUM in {1..10}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null
if [ "$?" = "0" ]
then
AUTO_CONNECT 172.25.254.$NUM hostname | tail -n 1 >> /mnt/host_list
fi
done
当expect自动应答
与Shell脚本
环境结合到一起时,expect部分的语句必须顶格书写