python批量修改交換機(jī)密碼的示例
1.通過(guò)pip安裝python第三方模塊paramiko
pip install paramiko
2.創(chuàng)建腳本
##導(dǎo)入paramiko、time、getpass模塊#!/usr/bin/pythonimport paramikoimport timeimport getpass##通過(guò)raw_input()函數(shù)獲取用戶(hù)輸入的SSH用戶(hù)名并賦值給usernameusername = raw_input(’Username:’)##通過(guò)getpass模塊中的getpass()函數(shù)獲取用戶(hù)輸入字符串作為密碼賦值給passwordpassword = getpass.getpass(’Password:’)##通過(guò)for i in range(1,5)和ip='192.168.100.'+str(i)語(yǔ)句實(shí)現(xiàn)循環(huán)登錄交換機(jī)SW1-SW4:100.1-4for i in range(1,5): ip='192.168.100.'+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()##調(diào)度交換機(jī)命令行執(zhí)行命令 command.send('system-view' +'n') command.send('aaa'+'n') command.send('local-user admin password cipher Jan16@Hw'+'n')##更改登錄密碼結(jié)束后,返回用戶(hù)視圖并保存配置 command.send('return'+'n') command.send('save'+'n') command.send('Y'+'n') command.send('n')##暫停2秒,并將命令執(zhí)行過(guò)程賦值給output對(duì)象,通過(guò)print output語(yǔ)句回顯內(nèi)容 time.sleep(2) output=command.recv(65535) print output##退出SSHssh_client.close()
3.執(zhí)行腳本
python changepassword.py Username:admin #手動(dòng)輸入SSH用戶(hù)名,這里是adminPassword: #手動(dòng)輸入SSH用戶(hù)密碼,這里是原先密碼
以上就是python批量修改交換機(jī)密碼的示例的詳細(xì)內(nèi)容,更多關(guān)于python批量修改交換機(jī)密碼的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python+unittest+requests 接口自動(dòng)化測(cè)試框架搭建教程2. Python的文本常量與字符串模板之string庫(kù)3. 利用CSS制作3D動(dòng)畫(huà)4. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼5. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題6. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解8. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程9. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法10. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)
