码迷,mamicode.com
首页 > 其他好文 > 详细

expect脚本远程登陆主机并记录日志

时间:2018-05-25 11:11:08      阅读:290      评论:0      收藏:0      [点我收藏+]

标签: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脚本远程登陆主机并记录日志

标签:Expect脚本

原文地址:http://blog.51cto.com/13765598/2120052

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!