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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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脚本远程登陆主机并记录日志的主要内容,如果未能解决你的问题,请参考以下文章

Telnet/Send/Expect - 自动从远程主机拉取日志

利用spawn和expect自动交互并返回交互结果

分发系统 expect

expect批量scp脚本

expect批量scp脚本

第六周