前提:
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