Python如何telnet到網(wǎng)絡(luò)設(shè)備
0.前言
Telnet協(xié)議屬于TCP/IP協(xié)議族里的一種,對于我們這些網(wǎng)絡(luò)攻城獅來說,再熟悉不過了,常用于遠程登陸到網(wǎng)絡(luò)設(shè)備進行操作,但是,它的缺陷太明顯了,就是不安全,信息明文傳送,極容易被攻擊竊取信息,不推薦使用,但本節(jié)我還是先從它入手哈。
1. 測試環(huán)境及關(guān)鍵代碼解釋
1.1 簡單測試環(huán)境
- 使用python3環(huán)境
- 使用內(nèi)置telnetlib模塊
- 簡單的實驗環(huán)境
說明: 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 關(guān)鍵代碼
import xx:導入模塊 class xx:定義類 def xx: 定義函數(shù) try-except :處理可能引發(fā)的異常 tn.read_until(expected, timeout=None):等待預(yù)期字符串或等待超時 tn.write(buffer):寫入的字符串(意思發(fā)送給命令給設(shè)備) tn.expect(list, timeout=None):讀顯,list采用正則表達式(意思把執(zhí)行過程顯示出來) tn.read_very_eager():讀顯(意思把執(zhí)行過程顯示出來) tn.open(host, port=0[, timeout]):連接主機 tn.close():關(guān)閉連接
Tips:終端與網(wǎng)絡(luò)設(shè)備交付的信息是以byte類型,所以要把終端上的字符串encode編碼轉(zhuǎn)換為byte對象,網(wǎng)絡(luò)設(shè)備回顯的byte信息要decode解碼。
2. 完整代碼
'''
歡迎關(guān)注微信公眾號:'diandijishu'
此平臺是網(wǎng)路工程師個人日常技術(shù)、項目案例經(jīng)驗分享,
為鞏固及提升技術(shù)能力乃至共享所學所知技術(shù)
也歡迎各位工程師一起分享、一起成長。
'''
#!/usr/bin/env python
#coding:utf-8
'導入模塊'
from telnetlib import Telnet
import time
import logging
'定義類'
class TelnetClient():
'初始化屬性'
def __init__(self):
self.tn = Telnet()
'定義login_host函數(shù),用于登陸設(shè)備'
def login_host(self,ip,username,password,enable=None,verbose=True):
'連接設(shè)備,try-except結(jié)構(gòu)'
try:
self.tn.open(ip,port=23)
except:
logging.warning('%s網(wǎng)絡(luò)連接失敗' %ip)
return False
'輸入用戶名'
self.tn.read_until(b'Username:', timeout=1)
self.tn.write(b'\n')
self.tn.write(username.encode() + b'\n')
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'\n')
rely = self.tn.expect([], timeout=1)[2].decode().strip()
if verbose:
print(rely)
'進去特權(quán)模式'
if enable is not None:
self.tn.write(b'enable\n')
self.tn.write(enable.encode() + b'\n')
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函數(shù),用于執(zhí)行命令'
def do_cmd(self,cmds):
'讀取文件,for語句循環(huán)執(zhí)行命令'
with open(cmds) as cmd_obj:
for cmd in cmd_obj:
self.tn.write(cmd.encode().strip() + b'\n')
time.sleep(2)
rely = self.tn.read_very_eager().decode()
logging.warning('命令執(zhí)行結(jié)果:\n %s' %rely)
'定義logout_host函數(shù),關(guān)閉程序'
def logout_host(self):
self.tn.close()
if __name__ == '__main__':
username = 'cisco' #用戶名
password = 'cisco' #密碼
enable = 'cisco' #特權(quán)密碼
lists = 'list.txt' #存放IP地址文件,相對路徑
cmds = 'cmd.txt' #存放執(zhí)行命令文件,相對路徑
telnet_client = TelnetClient()
'讀取文件,for語句循環(huán)登陸IP'
with open(lists,'rt') as list_obj:
for ip in list_obj:
'如果登錄結(jié)果為True,則執(zhí)行命令,然后退出'
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. 報錯效果
4.1 遠程連接不上
4.2 用戶名和密碼錯誤

5. 碎碎語
這些只是一些簡單的代碼,待優(yōu)化的地方還是很多,先給小伙伴們學習一下,telnet協(xié)議是個不安全的,基本網(wǎng)絡(luò)環(huán)境很少用了,ssh為常用的協(xié)議,安全又好用,下個文章我給大家介紹python如何使用ssh模塊哈。
本人代碼功夫不深,如有缺陷望指教,多謝。
以上就是Python如何telnet到網(wǎng)絡(luò)設(shè)備的詳細內(nèi)容,更多關(guān)于python telnet到網(wǎng)絡(luò)設(shè)備的資料請關(guān)注腳本之家其它相關(guān)文章!
- 如何在Python3中使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備
- Python telnet登陸功能實現(xiàn)代碼
- 使用python telnetlib批量備份交換機配置的方法
- python 處理telnet返回的More,以及get想要的那個參數(shù)方法
- 對python使用telnet實現(xiàn)弱密碼登錄的方法詳解
- 使用python Telnet遠程登錄執(zhí)行程序的方法
- Python判斷telnet通不通的實例
- Python實現(xiàn)telnet服務(wù)器的方法
- Python實現(xiàn)的使用telnet登陸聊天室實例
- python實現(xiàn)telnet客戶端的方法
- Python實現(xiàn)Telnet自動連接檢測密碼的示例
相關(guān)文章
python+requests接口自動化框架的實現(xiàn)
這篇文章主要介紹了python+requests接口自動化框架的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
python實現(xiàn)字符串加密 生成唯一固定長度字符串
這篇文章主要為大家詳細介紹了python實現(xiàn)字符串加密,生成唯一固定長度字符串,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
python numpy 反轉(zhuǎn) reverse示例
今天小編就為大家分享一篇python numpy 反轉(zhuǎn) reverse示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python實現(xiàn)找到同名文件并復制到其他文件夾中
這篇文章主要為大家介紹了如何基于Python語言,實現(xiàn)依據(jù)某一文件夾中大量文件的名稱復制另一文件夾中的同名文件,文中的示例代碼簡潔易懂,需要的可以參考一下2023-05-05

