except的实践经验

最近爆发SMI安全问题, 公司有很多交换机都需要进行安全配置,所以要对200台交换机进行配置,手工登录的方式太慢。
现在有两个思路 :

  1. 使用Python的paramiko
  2. 使用except

except代码如下:
=== except.code===

#!/usr/bin/expect -f

# Set variables
set hostname [lindex $argv 0]
set username "user"
set passwd "password1!"

# Log results
  log_file -a ~/results.log

# Announce which device we are working on and at what time
    send_user "\n"

# Don't check keys
    spawn ssh -c 3des -x -o StrictHostKeyChecking=no -l jiaqiang_tian $hostname

# Allow this script to handle ssh connection issues
   expect {
    timeout { send_user "\nTimeout Exceeded - Check Host\n"; exit 1 }
     eof { send_user "\nSSH Connection To $hostname Failed\n"; exit 1 }
      "*#" {}
       "*assword:" {
        send "$passwd\r"
            }
          }
    expect "#"
    send "show vstack config\r"
    expect "#"
    send "conf ter\r"
    expect "#"
    send "no vstack\r"
    expect "#"
    send "end\r"
    expect "#"
    send "copy running-config startup-config\r"
    expect "*onfig]? "
    send "\r"
    expect "#"
    send "exit\n"

使用方法:
./except.coode hostname/IP

关于Password中特殊字符的说明:
如果密码中有&,#,$的, 处理的方式就是使用转移符\,比如说&-> \&
其他的符号应该不用转义,比如说:!@%^*

猜你喜欢

转载自blog.51cto.com/scantydd/2105603