1 telnetlib介紹
1.1 簡介
官方介紹檔案:telnetlib – Telnet 客戶端 — Python 3.9.6 檔案
telnetlib 模塊提供一個實作Telnet協議的類 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 發送命令
發送命令使用的是Telnet類的write方法,注意引數buffer是位元組字串byte string,網路資料傳輸都是使用的byte string,也就是位元組流,在發送的字串前面加一個b,就可以將字串轉換為位元組流,
Telnet.write(buffer)
例如,發送一個“exit”命令給服務器,也就是退出telnet連接,
tn.write(b"exit\n")
1.2.3 讀取回傳資料
Telnet類提供的讀取回傳結果的函式比較多,這里列舉3個:
Telnet.read_until(expected, timeout=None) 讀取直到遇到給定位元組串 expected 或 timeout 秒已經過去,默認為阻塞性的讀,
Telnet.read_all() 讀取資料,直到遇到 EOF;連接關閉前都會保持阻塞,
Telnet.read_very_eager() 在不阻塞 I/O 的情況下讀取所有的內容(eager),
1.2.4 關閉連接
關閉telnet連接直接使用Telnet.close()函式,或者發送"exit"命令,以下兩種用法是一樣的,
tn = telnetlib.Telnet()
#方式1
tn.close()
#方式2
tn.write(b"exit\n")
1.3 使用示例
首先,我們先使用IPOP創建一個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")
直接執行,結果如下,可以看出,連接了一次telnet服務器,然后退出了:


2 自動測驗
參考代碼:Python3+telnetlib實作telnet客戶端 - 諸子流 - 博客園 (cnblogs.com)
先簡單說明代碼實作的功能,首先先運行一個程式,這個程式會創建一個telnet服務;然后使用python撰寫一個telnet客戶端,連接telnet服務,并輸入命令,獲取命令回傳結果,根據結果來判斷命令是否執行正確,
命令及期望結果:命令和期望的結果存放在excel中,期望結果用來從命令的回傳資料中進行字串查找的,如果找到了,表示命令執行成功,否則認為執行失敗,格式如下:

執行失敗結果保存:如果命令執行失敗,則將命令和得到的回傳資料存放到一個單獨的檔案中,
下面說明代碼目錄結構:
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連接,以及發送命令和獲取結果,并決議結果資訊:
# -*- 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()
這樣就能實作一個簡單的自動測驗命令的方式,
如果文章對你有幫助,麻煩伸出發財小手點個贊,感謝您的支持,你的點贊是我持續更新的動力,今天就到這里,學廢了,記得在評論區留言:打卡,
好文推薦
2021軟體測驗工程師面試題匯總(內含答案)-看完BATJ面試官對你豎起大拇指!
什么樣的人適合從事軟體測驗作業?
軟體測驗和軟體開發哪個發展更好
那個準點下班的人,比我先升職了…
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/294886.html
標籤:其他
上一篇:憑什么他26歲就年薪30W+?
