python3+telnetlib實現簡單自動測試示例詳解
1 telnetlib介紹
1.1 簡介
官方介紹文檔:telnetlib – Telnet 客戶端 — Python 3.9.6 文檔
telnetlib 模塊提供一個實現Telnet協(xié)議的類 Telnet。
1.2 庫常用函數及使用
1.2.1 建立連接
建立連接有兩種方式:1、實例化函數的時候,將可選參數 host 和 port 傳遞給構造函數,在這種情況下,到服務器的連接將在構造函數返回前建立。2、使用telnetlib.Telnet類的open函數建立連接。
如以下兩種方式是等同的,參數timeout表示阻塞的時間(單位為秒),默認為一直阻塞:
import telnetlib HOST = "10.102.1.12" #方式1 tn = telnetlib.Telnet(HOST, port=21, timeout=10) #方式2 tn = telnetlib.Telnet() tn.open(HOST, port=21)
1.2.2 發(fā)送命令
發(fā)送命令使用的是Telnet類的write方法,注意參數buffer是字節(jié)字符串byte string,網絡數據傳輸都是使用的byte string,也就是字節(jié)流,在發(fā)送的字符串前面加一個b,就可以將字符串轉換為字節(jié)流。
Telnet.write(buffer)
例如,發(fā)送一個“exit”命令給服務器,也就是退出telnet連接。
tn.write(b"exit\n")
1.2.3 讀取返回數據
Telnet類提供的讀取返回結果的函數比較多,這里列舉3個:
Telnet.read_until(expected, timeout=None) 讀取直到遇到給定字節(jié)串 expected 或 timeout 秒已經過去。默認為阻塞性的讀。
Telnet.read_all() 讀取數據,直到遇到 EOF;連接關閉前都會保持阻塞。
Telnet.read_very_eager() 在不阻塞 I/O 的情況下讀取所有的內容(eager)。
1.2.4 關閉連接
關閉telnet連接直接使用Telnet.close()函數,或者發(fā)送"exit"命令,以下兩種用法是一樣的。
tn = telnetlib.Telnet() #方式1 tn.close() #方式2 tn.write(b"exit\n")
1.3 使用示例
首先,我們先使用IPOP創(chuàng)建一個FTP服務,端口為21,用戶名為admin,密碼為admin。
然后,編寫一個簡單的測試用例,連接telnet服務器,然后退出。
import getpass import telnetlib HOST = "10.102.1.12" user = input("Enter your remote account: ") password = getpass.getpass() tn = telnetlib.Telnet(HOST, port=21, timeout=10) tn.write(user.encode('ascii') + b"\n") if password: tn.write(password.encode('ascii') + b"\n") print(tn.read_very_eager().decode('ascii')) tn.write(b"exit\n") print ("exit")
直接執(zhí)行,結果如下,可以看出,連接了一次telnet服務器,然后退出了:
2 自動測試
參考代碼:Python3+telnetlib實現telnet客戶端 - 諸子流 - 博客園 (cnblogs.com)
先簡單說明代碼實現的功能,首先先運行一個程序,這個程序會創(chuàng)建一個telnet服務;然后使用python編寫一個telnet客戶端,連接telnet服務,并輸入命令,獲取命令返回結果,根據結果來判斷命令是否執(zhí)行正確。
命令及期望結果:命令和期望的結果存放在excel中,期望結果用來從命令的返回數據中進行字符串查找的,如果找到了,表示命令執(zhí)行成功,否則認為執(zhí)行失敗。格式如下:
執(zhí)行失敗結果保存:如果命令執(zhí)行失敗,則將命令和得到的返回數據存放到一個單獨的文件中。
下面說明代碼目錄結構:
1078885-20210817232240481-1025625638
C_parse_excel.py類用于解析excel,獲取命令及返回結果: # -*- coding: utf-8 -*- import os import sys import re import xlrd import logging logging.basicConfig(level=logging.NOTSET, format='[%(filename)s:%(lineno)d]-%(levelname)s %(message)s') class CCsrConfig(object): def __init__(self, excelName): self._registerDict = {} self._excelName = excelName def OpenExcel(self): if self._excelName == "": self._excelName = None else: self._excelfd = xlrd.open_workbook(self._excelName) for sheetName in self._excelfd.sheet_names(): pass def ReadCSRCfg(self): return_dict = {} #{sheetName: [cmdlist]} for sheetName in self._excelfd.sheet_names(): tmp_list = [] sheet = self._excelfd.sheet_by_name(sheetName) if None != sheet: if sheet.nrows == 0: # no content continue sheetName = str(sheetName.strip()).lower() logging.debug(sheetName) row_start = 0 for row in range(sheet.nrows): if sheet.cell(row, 0).value.strip() == u"command": row_start = row + 1 break for row in range(row_start, sheet.nrows, 1): cmd = str(sheet.cell(row, 0).value).strip() exp_ret = str(sheet.cell(row, 1).value).strip() tmp_list.append([cmd, exp_ret]) return_dict[sheetName.lower()] = tmp_list return return_dict
C_telnet.py類實現telnet連接,以及發(fā)送命令和獲取結果,并解析結果信息:
# -*- coding:utf-8 -*- import logging import telnetlib import time class TelnetClient(): def __init__(self,): self.tn = telnetlib.Telnet() # 此函數實現telnet登錄主機 def login_host(self, host_ip, remote_port, username, password): try: self.tn.open(host_ip, port = remote_port) except: logging.warning('%s網絡連接失敗' % host_ip) return False # 等待login出現后輸入用戶名,最多等待10秒 self.tn.read_until(b'login: ', timeout=2) self.tn.write(username.encode('ascii') + b'\n') # 等待Password出現后輸入用戶名,最多等待10秒 self.tn.read_until(b'Password: ', timeout=2) self.tn.write(password.encode('ascii') + b'\n') # 延時兩秒再收取返回結果,給服務端足夠響應時間 time.sleep(2) # 獲取登錄結果 command_result = self.tn.read_very_eager().decode('ascii') if 'Login incorrect' not in command_result: logging.debug(u'%s登錄成功' % host_ip) return True else: logging.warning(u'%s登錄失敗,用戶名或密碼錯誤' % host_ip) return False def start_test_cmd(self, cmd_dict): for sheet_item in cmd_dict: for sheet in sheet_item: cmd_list = sheet_item[sheet] tmp_err_list = [] for cmd in cmd_list: cmd_in = cmd[0] exp_ret = cmd[1] self.tn.write(cmd_in.encode('ascii')+b'\n') time.sleep(1) # 獲取命令結果 command_result = self.tn.read_very_eager().decode('ascii') if command_result.find(exp_ret) == -1:tmp_err_list.append([cmd_in, command_result]) else:print('%s' % command_result) if len(tmp_err_list) != 0: # 將錯誤信息記錄到文件中 with open("./out_file/%s_err_log.txt" % sheet, "w+", newline="") as f:for err_item in tmp_err_list: logging.debug(err_item[0]) f.write("%s" % err_item[0]) f.write("%s" % err_item[1]) # 退出telnet def logout_host(self): self.tn.write(b"exit\n")
main_func.py是主函數入口:
# -*- coding:utf-8 -*- import logging import os import sys from C_telnet import * from C_parse_excel import * Host_ip = '192.168.17.128' Username = 'admin' Password = 'admin' Remote_port = 8000 def parse_cmd_excel(dir_name): objList = [] list_f = os.listdir(dir_name) for item in list_f: item = dir_name + item if os.path.isfile(item) and (item[-5:] == '.xlsx' or item[-5:] == '.xlsm'): if item.find("$") != -1: continue csrConfig = CCsrConfig(item) csrConfig.OpenExcel() tmp = csrConfig.ReadCSRCfg() objList.append(tmp) elif os.path.isdir(item): item = item + '/' new_obj_list = [] new_obj_list = parse_cmd_excel(item) for each in new_obj_list: objList.append(each) return objList if __name__ == '__main__': # 從表格中獲取測試的命令 all_cmd_dict = {} all_cmd_dict = parse_cmd_excel("./src_file/") #啟動telnet客戶端連接,并進行測試 telnet_client = TelnetClient() if telnet_client.login_host(Host_ip, Remote_port, Username, Password) == False: print("Telnet disconnected!\n") else: telnet_client.start_test_cmd(all_cmd_dict) telnet_client.logout_host()
這樣就能實現一個簡單的自動測試命令的方式。
到此這篇關于python3+telnetlib實現簡單自動測試的文章就介紹到這了,更多相關python3 telnetlib自動測試內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯(lián)網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。