Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1730514
  • 博文数量: 234
  • 博客积分: 4966
  • 博客等级: 上校
  • 技术积分: 3322
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-13 01:03
文章分类

全部博文(234)

文章存档

2017年(2)

2016年(1)

2015年(8)

2014年(11)

2013年(44)

2012年(27)

2011年(22)

2010年(30)

2009年(37)

2008年(6)

2007年(45)

2006年(1)

分类: 系统运维

2013-05-07 21:39:38

原来写过一个同样作用的脚本,是采用shell脚本循环expect脚本来完成批量变更交换机密码的完成任务的。
具体可以看我原来的博文:http://blog.chinaunix.net/uid-8874157-id-3448298.html
原脚本测试过:
主要有有以下问题,
1.性能问题,需要修改的交换机较多时偶尔会造成个别交换机修改密码不成功,原因未知,但单个执行脚本会成功
2.相关提示还不够好,如密码错误或者修改远程交换机密码超时会造成脚本挂起,但后台查看进程还是能查到具体是修改哪个交换机造成的。

最近在学python,所以又把这个脚本用python重写了一下
稍微测试了下,执行是没问题的,应该是比原来的好一些了

具体代码如下:
#-------------------------------------------------------------------------------
# Name:        modify_cisco_password
# Purpose:
#
# Author:      Edward
#
# Created:     07/05/2013
# Copyright:   (c) Edward 2013
# Licence:    
#-------------------------------------------------------------------------------

# -*- coding: utf-8 -*-

import ciscolib

def main():
    PASSWORD="123456"
    USERNAME="123456"
    ENABLE_PWD="123456"

    for ip in open('sw.txt').readlines():
        ip = ip.strip()

        if USERNAME != "":
            switch = ciscolib.Device(ip, PASSWORD, USERNAME, ENABLE_PWD)
        else:
            switch = ciscolib.Device(ip, PASSWORD, enable_password=ENABLE_PWD)

        try:
            switch.connect()
            print("Logged into %s" % ip)
        except ciscolib.AuthenticationError as e:
            print("Couldn't connect to %s: %s" % (ip, e.value))
            continue
        except Exception as e:
            print("Couldn't connect to %s: %s" % (ip, str(e)))
            continue

        switch.enable(ENABLE_PWD)
        switch.cmd("conf t")
        switch.cmd("enable secret 123456")
        switch.cmd("line con 0")
        switch.cmd("password 123456")
        switch.cmd("line vty 0 4")
        switch.cmd("password 123456")
        switch.cmd("end")
        switch.cmd("wr")
        switch.disconnect()


if __name__ == '__main__':
    main()



阅读(4545) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~