标签:Expect脚本
实现了远程登陆,并且记录日志#!/bin/bash
user=root
exec_expect(){
expect -c "log_user 0
spawn ssh $2
expect \"assword\"
send $3\r
expect \"127\"
set timeout 5
send \"netstat -anput | grep 80 \r\"
expect -re {(.*)(:80)}
send_user \"\$expect_out(0,string)\n\"
" >> /tmp/sshlog/$2.log
}
if [ ! -d /tmp/sshlog ];then
mkdir -p /tmp/sshlog
fi
rm -rf /tmp/sshlog/*
for i in $(cat /tmp/list.txt)
do
ip=$(echo $i | awk -F: ‘{print $1}‘)
sshpsw=$(echo $i | awk -F: ‘{print $2}‘)
exec_expect ${user} ${ip} ${sshpsw}
done
for i in $(ls /tmp/sshlog/)
do
result=$(cat $i | wc -l)
prefix_name=${i%.}
prefix_name=${prefix_name##*/}
if [ $result -eq 1 ]; then
echo ${prefix_name} " is off" >>/tmp/sshlog/result.txt
else
echo ${prefix_name} " is on" >>/tmp/sshlog/result.txt
fi
done
rm -rf /tmp/sshlog/*.log
echo "done!!!!"
标签:Expect脚本
原文地址:http://blog.51cto.com/13765598/2120052