网络设备模拟登录,并执行ping命令检测对应链路
- 我以为网络设备跟linux服务器一样可以用ssh远程执行命令,但是发现我错了,
- 第一版脚本如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#!/bin/bash
#20230130
#by lipeichao
cat list|while read line
do
ip=$(echo $line |awk '{print $1}')
user=$(echo $line |awk '{print $2}')
passwd=$(echo $line |awk '{print $3}')
fip=$(echo $line |awk '{print $4}')
system=$(echo $line |awk '{print $5}')
time=$(date "+%Y/%m/%d %H:%M:%S")
echo $time
if sshpass -p "$passwd" ssh $user@$ip "ping -q -i 0.1 -c 5 $fip" < /dev/null
then
echo "$ip $system $fip is ok"
else
echo "$ip $system $fip is fail"
fi
done - 发现并不返回执行后的结果,还卡死了
- 第二版脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#!/bin/bash
cat /home/monitor/list |while read ip fip system
do
time=$(date "+Y/%m/%d %H:%M:%S")
echo $time
{
/usr/bin/expect <<-EOF
spawn ssh xunjian@$ip
expect{
"yes/no"{send "yes\n";exp_continue}
"*password:"{send "$passwd"}
}
expect "*>"
send "ping $fip\n"
sleep 5
send "quit\n"
interact
EOF
}
echo \n
echo $system
done