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\r\" expect \"password:\n\" #这里如果是第一次登陆,判断yes/no send \"$swpasswd\r \" } expect \"password:\n\" send \"$swpasswd\r \" expect \"*>\n\" send \"dis int g0/0/1 \r \n\" ######################################### #You cat input here like follow: #expect \"*>\n\" #send \"dis int g0/0/1 \r \n\" ######################################### expect \"*>\n\" send \"quit\" " echo "Bye!!! "
此脚本加入计划任务,作监控,可以将结果放入到其他文件。
通过awk,sed等取出结果。
或者新建一个脚本做while循环。