需求分析,實現原理
需求背景:日常網絡運維中網管人員需要頻繁登錄到網絡設備進行日常巡檢等操作,如果現網設備很多,需要記錄大量的設備管理ip,這對我們運維人員很不友好python獲取設備管理器,所以有沒有一種工具或者腳本可以通過人機交互的方式幫助我們登錄網絡設備?
實現原理:簡單來說就是運維人員和腳本交互獲取到我們想登錄的網絡設備的管理ippython獲取設備管理器,然后腳本自動發起連接網絡設備請求來達到模擬登錄的作用,這里選擇使用來實現我們的腳本。
寫腳本
代碼的核心內容如下:
while True:
print('請輸入數字序號進入下一級,b返回上一級,q退出當前程序')

for i, k in enumerate(curr_layer):
print(i, k)
select_layer[i] = k
choice = input('please input your choice:').strip()
if not choice:
continue

if choice.isdigit():
if select_layer[int(choice)] in curr_layer:
if type(curr_layer[select_layer[int(choice)]]) is list:
os.environ['ip'] = curr_layer[select_layer[int(choice)]][0]
os.environ['port'] = curr_layer[select_layer[int(choice)]][1]
os.system('telnet $ip $port')

time.sleep(2)
continue
last_layer.append(curr_layer)
curr_layer = curr_layer[select_layer[int(choice)]]
else:
if choice == 'b':

if len(last_layer) > 0:
# print(last_layer) # test
curr_layer = last_layer[-1]
last_layer.pop()
# print(curr_layer) #test
# print(last_layer) #test

else:
print("This is top lever")
elif choice == 'q':
print('bye')
break
else:
print("輸入錯誤,請重新輸入")
文章還未寫完,后面會繼續更新