批处理脚本
- 检测教室中开启的所有主机,并抓取所有主机的值机名称和 ip的对应列表,把列表保存在 /mnt/ip_host.list 文件中。
#!/bin/bash
Auto_SSH()
{
/usr/bin/expect <<-EOF
spawn ssh root@$1 hostname
expect {
"yes/no" { send "yes\r" ; exp_continue }
"password" { send "redhat\r" }
}
expect eof
EOF
}
for IP in {1..10}
do
ping -c1 -w1 172.25.10.$IP &> /dev/null
if [ "$?" -eq "0" ]
then
echo "`Auto_SSH 172.25.10.$IP | tail -n 1 | sed 's/^M//g'` A 172.25.10.$IP" >> /mnt/host
fi
done
执行结果: