日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python expect模块_Python基础教程:用Python怎么telnet到网络设备

發布時間:2025/3/11 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python expect模块_Python基础教程:用Python怎么telnet到网络设备 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python基礎教程:用Python怎么telnet到網絡設備

0.前言

Telnet協議屬于TCP/IP協議族里的一種,對于我們這些網絡攻城獅來說,再熟悉不過了,常用于遠程登陸到網絡設備進行操作,但是,它的缺陷太明顯了,就是不安全,信息明文傳送,極容易被攻擊竊取信息,不推薦使用,但本節我還是先從它入手哈。

1. 測試環境及關鍵代碼解釋

1.1 簡單測試環境

  • 使用python3環境
  • 使用內置telnetlib模塊
  • 簡單的實驗環境

說明:

cmd.txt文件里面命令如下:

terminal length 0 show clock show ip interface brief

list.txt文件里面的IP如下:

192.168.1.101 192.168.1.102 192.168.1.103

1.2 關鍵代碼

import xx:導入模塊

class xx:定義類

def xx: 定義函數

try-except :處理可能引發的異常

tn.read_until(expected, timeout=None):等待預期字符串或等待超時

tn.write(buffer):寫入的字符串(意思發送給命令給設備)

tn.expect(list, timeout=None):讀顯,list采用正則表達式(意思把執行過程顯示出來)

tn.read_very_eager():讀顯(意思把執行過程顯示出來)

tn.open(host, port=0[, timeout]):連接主機

tn.close():關閉連接

Tips:終端與網絡設備交付的信息是以byte類型,所以要把終端上的字符串encode編碼轉換為byte對象,網絡設備回顯的byte信息要decode解碼。

2. 完整代碼

'''歡迎關注:'333' 此平臺是網路工程師個人日常技術、項目案例經驗分享, 為鞏固及提升技術能力乃至共享所學所知技術 也歡迎各位工程師一起分享、一起成長。'''#!/usr/bin/env python#coding:utf-8'導入模塊'from telnetlib import Telnetimport timeimport logging'定義類'class TelnetClient(): '初始化屬性' def __init__(self): self.tn = Telnet() '定義login_host函數,用于登陸設備' def login_host(self,ip,username,password,enable=None,verbose=True): '連接設備,try-except結構' try: self.tn.open(ip,port=23) except: logging.warning('%s網絡連接失敗' %ip) return False '輸入用戶名' self.tn.read_until(b'Username:', timeout=1) self.tn.write(b'') self.tn.write(username.encode() + b'') rely = self.tn.expect([], timeout=1)[2].decode().strip() #讀顯 if verbose: print(rely) '輸入用戶密碼' self.tn.read_until(b'Password:', timeout=1) self.tn.write(password.encode() + b'') rely = self.tn.expect([], timeout=1)[2].decode().strip() if verbose: print(rely) '進去特權模式' if enable is not None: self.tn.write(b'enable') self.tn.write(enable.encode() + b'') if verbose: rely = self.tn.expect([], timeout=1)[2].decode().strip() print(rely) time.sleep(1) rely = self.tn.read_very_eager().decode() if 'Login invalid' not in rely: logging.warning('%s登陸成功' % ip) return True else: logging.warning('%s登陸失敗,用戶名或密碼錯誤' % ip) return False '定義do_cmd函數,用于執行命令' def do_cmd(self,cmds): '讀取文件,for語句循環執行命令' with open(cmds) as cmd_obj: for cmd in cmd_obj: self.tn.write(cmd.encode().strip() + b'') time.sleep(2) rely = self.tn.read_very_eager().decode() logging.warning('命令執行結果: %s' %rely) '定義logout_host函數,關閉程序' def logout_host(self): self.tn.close()if __name__ == '__main__': username = 'cisco' #用戶名 password = 'cisco' #密碼 enable = 'cisco' #特權密碼 lists = 'list.txt' #存放IP地址文件,相對路徑 cmds = 'cmd.txt' #存放執行命令文件,相對路徑 telnet_client = TelnetClient() '讀取文件,for語句循環登陸IP' with open(lists,'rt') as list_obj: for ip in list_obj: '如果登錄結果為True,則執行命令,然后退出' if telnet_client.login_host(ip.strip(),username,password,enable): telnet_client.do_cmd(cmds) telnet_client.logout_host() time.sleep(2)

3. 運行效果

備注:這個運行的效果我只存放了192.168.1.101這個IP,精簡一下,為了效果。

4. 報錯效果

  • 遠程連接不上
  • 用戶名和密碼錯誤

5. 碎碎念

這些只是一些簡單的代碼,待優化的地方還是很多,先給小伙伴們學習一下,telnet協議是個不安全的,基本網絡環境很少用了,ssh為常用的協議,安全又好用!伙伴們有需要 補充的歡迎留言!

總結

以上是生活随笔為你收集整理的python expect模块_Python基础教程:用Python怎么telnet到网络设备的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。