Python自动化批量修改网络设备密码

前提:

https://blog.csdn.net/weixin_50299079/article/details/109300864

好嘞,废话不多说,开始搞

本文以Cisco CSR1000V设备配合Python来完成

编写Python脚本【changepasword.py】,实现对设备的密码修改
[root@alex-Ansible ~]# cat changepassword.py 
import paramiko
import time
import getpass

username = input('Username:')
password = input('Password:')

for i in range(1):
    ip="10.32.132.86"
    ssh_client=paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip,username=username,password=password)
    command=ssh_client.invoke_shell()
    command.send("configure terminal" +"\n")
    command.send("aaa"+"\n")
    command.send("username admin privilege 15 password Aa123456"+"\n")
    command.send("end"+"\n")
    command.send("write"+"\n")
    command.send("\n")

    time.sleep(2)
    output=command.recv(65535)
    print (output)
ssh_client.close()
在设备上查看用户名和密码
Router#show running-config | se username 
username admin privilege 15 password 0 admin
执行changepassword.py文件
[root@alex-Ansible ~]# python3 changepassword.py 
Username:admin
Password:admin
b'\r\n\r\n\r\nRouter#configure terminal\r\nEnter configuration commands, one per line.  End with CNTL/Z.\r\nRouter(config)#aaa\r\n% Incomplete command.\r\n\r\nRouter(config)#username admin privilege 15 password Aa123456\r\n WARNING: Command has been added to the configuration using a type 0 password. However, type 0 passwords will soon be deprecated. Migrate to a supported password type\r\nRouter(config)#end\r\nRouter#write\r\nBuilding configuration...\r\n[OK]\r\nRouter#\r\nRouter#'
设备有如下打印信息
Router#
*Oct 24 04:11:58.944: %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success [user: admin] [Source: 10.32.132.125] [localport: 22] at 04:11:58 UTC Sat Oct 24 2020
*Oct 24 04:11:59.009: %AAAA-4-CLI_DEPRECATED: WARNING: Command has been added to the configuration using a type 0 password. However, type 0 passwords will soon be deprecated. Migrate to a supported password type
*Oct 24 04:11:59.011: %SYS-5-CONFIG_I: Configured from console by admin on vty1 (10.32.132.125)
*Oct 24 04:12:00.970: %SYS-6-LOGOUT: User admin has exited tty session 2(10.32.132.125)
使用ssh和修改之后的密码登录设备
[root@alex-Ansible ~]# ssh admin@10.32.132.86
Password: 



Router#
设备有如下打印信息
*Oct 24 04:13:27.022: %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success [user: admin] [Source: 10.32.132.125] [localport: 22] at 04:13:27 UTC Sat Oct 24 2020
函数详解
[root@alex-Ansible ~]# cat changepassword.py 
import paramiko                             //导入paramiko模块
import time                                 //导入time模块
import getpass                              //导入getpass模块

username = input('Username:')               //通过input()函数获取用户输入的SSH用户名并赋值给username【python2调用函数为raw_input】
password = input('Password:')               //同上

for i in range(1,5):                          //通过for i in range(1,5)和ip="10.32.132."+str(i)语句实现循环登录交换机设备1到设备4
    ip="10.32.132."+str(i)
    ssh_client=paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip,username=username,password=password)
    command=ssh_client.invoke_shell()
    command.send("configure terminal" +"\n")              //调度交换机命令行执行命令
    command.send("aaa"+"\n")
    command.send("username admin privilege 15 password Aa123456"+"\n")
    command.send("end"+"\n")
    command.send("write"+"\n")
    command.send("\n")

    time.sleep(2)                   //暂停2秒,并将命令执行过程赋值给output对象,通过print output语句回显内容
    output=command.recv(65535)
    print (output)
 ssh_client.close()                 //退出SSH

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex-Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值