python运维脚本交换机更改密码_python脚本 批量修改cisco交换机密码

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

阅读(3047) | 评论(0) | 转发(2) |

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值