您現在的位置是:首頁 > 垂釣
網路自動化運維——如何自動化修改網路裝置密碼?
- 由 因特汪 發表于 垂釣
- 2022-02-28
掌上網管怎樣設定密碼
1、配置過程
1)在網管計算機聯網狀態下安裝Python3和paramiko;
[root@manage ~]#yum install -y python3 //安裝Python3和相應依賴工具
[root@manage ~]# pip3 install paramiko //透過pip安裝python第三方模組paramiko
2)編寫Python指令碼【changepassword。py】,實現對交換機SW1~SW4的密碼修改:
[root@manage ~]# vi changepassword。py
##匯入paramiko、time、getpass模組
import paramiko
import time
import getpass
##透過raw_input()函式獲取使用者輸入的SSH使用者名稱並賦值給username
username = input(‘Username:’)
##透過getpass模組中的getpass()函式獲取使用者輸入字串作為密碼賦值給password
password = getpass。getpass(prompt=‘Password:’,stream=None)
##透過for i in range(1,5)和ip=“192。168。100。”+str(i)語句實現迴圈登入交換機SW1-SW4
for 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()
##排程交換機命令列執行命令
command。send(“system-view” +“\n”)
command。send(“aaa”+“\n”)
command。send(“local-user admin password cipher Jan16@Hw”+“\n”)
##更改登入密碼結束後,返回使用者檢視並儲存配置
command。send(“return”+“\n”)
command。send(“save”+“\n”)
command。send(“Y”+“\n”)
command。send(“\n”)
##暫停2秒,並將命令執行過程賦值給output物件,透過print output語句回顯內容
time。sleep(2)
output=command。recv(65535)。decode()
print (output)
##退出SSH
ssh_client。close()
3)在網管計算機執行指令碼【changepassword。py】:
[root@manage ~]# python3 changepassword。py //排程python執行指令碼
Username:admin //手動輸入SSH使用者名稱,這裡是admin
Password: //手動輸入SSH使用者密碼,這裡是HwEdu12#$
2、任務驗證
1)在網管計算機檢視指令碼的回顯內容:
Info: The max number of VTY users is 5, and the number
of current VTY users on line is 1。
The current login time is 2020-01-18 15:12:48。
Enter system view, return user view with Ctrl+Z。
[SW1]aaa
[SW1-aaa]local-user admin password cipher Jan16@Hw
[SW1-aaa]return
The current configuration will be written to the device。
Are you sure to continue?[Y/N]Y
Now saving the current configuration to the slot 0。
Save the configuration successfully。
2)在網管計算機使用【ssh admin@192。168。100。1】命令重新連線SW1
[root@manage ~]# ssh admin@192。168。100。1
admin@192。168。100。1‘s password: //這裡輸入新密碼Jan16@Hw
Info: The max number of VTY users is 5, and the number
of current VTY users on line is 2。
The current login time is 2020-01-18 15:15:48。