expect远程登陆hw交换机--测试通过1

#!/bin/bash
#connect_the_hw_switch
#by osx1260@163.com 


swip=172.16.56.2
swport=2081
swpasswd='yourswpasswd'
swusername=yourswusername
time=$(date +"%F")
echo ".............please wating 9s............"
echo ".............This is $time..............."
/usr/bin/expect -c "
set timeout 3
spawn  ssh $swip -l $swusername -p $swport

expect "*(yes/no)? " { 
        send "yes
"
        expect "password:
"                               #这里如果是第一次登陆,判断yes/no
        send "$swpasswd
 "                   
}


expect "password:
"
send "$swpasswd
 "

expect "*>
"
send "dis int g0/0/1 
 
"
#########################################
#You cat input here like follow:
#expect "*>
"
#send "dis int g0/0/1 
 
"
#########################################



expect "*>
"
send "quit"

"
echo "Bye!!! "

此脚本加入计划任务,作监控,可以将结果放入到其他文件。

通过awk,sed等取出结果。

或者新建一个脚本做while循环。

原文地址:https://www.cnblogs.com/osxlinux/p/3517765.html