原来写过一个同样作用的脚本,是采用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()



09-13 13:55