自动登录网络设备刷配置脚本
#!/bin/bash for p in 27 28;do for i in `seq 0 1 2`;do fping 172.${p}.${i}.1 > /dev/null sleep 1 if [ $? -eq 0 ];then echo 172.${p}.${i}.1 >> online-ip.log else echo 172.${p}.${i}.1 >> offline-ip.log fi done done while read line;do expect /root/network/loginsw.exp $line done < /root/network/online-ip.log
#!/bin/bash for p in 27 28;do for i in `seq 0 1 1`;do fping 172.${p}.${i}.1 > /dev/null if [ $? -eq 0 ];then expect /root/network/loginsw.exp 172.${p}.${i}.1 >expect.log echo 172.${p}.${i}.1 >> online-ip.log else echo 172.${p}.${i}.1 >> offline-ip.log fi usleep 500 done done #while read line;do # expect /root/network/loginsw.exp $line #$done < /root/network/online-ip.log
#!/usr/bin/expect set ipaddr [lrange $argv 0 0] set timeout 5 spawn telnet $ipaddr expect "Username:" send "admin\r" expect "Password:" send "admin\r" expect "*>" send "enable\r" expect "Password:" send "admin\r" expect "*#" send "conf t\r" expect "*(config)" send "no ntp server 172.29.1.250\r" send "ntp server 172.30.100.87 source f0/1\r" expect "*marker.*" send "ntp server 172.30.100.87 source g0/1\r" send "end\r" send "exit\r" expect eof
#对接口的判断
注意事项:expect的脚本运行需./*exp运行
set ipaddr [lrange $argv 0 0] 的理解
注:$argv0是脚本名,$argc表示参数的个数
[lindex $argv n]用来接收输入的脚本参数,n从0开始,分别表示第一个,第二个,第三个....参数,
[lindex $argv 0]是第一个参数也可以用[lrange $argv 0 0]表示, [lindex $argv 1]或者[lrange $argv 1 1]都可以表示第二个参数, 以此类推
[lrange $argv 0 4]表示第一个参数到第五个参数
send_user 用来显示信息到父进程(一般为用户的shell)的标准输出。