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

歡迎訪問 生活随笔!

生活随笔

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

python

Python连接telnet客户端连接服务端程序

發布時間:2025/3/15 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python连接telnet客户端连接服务端程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import logging import telnetlib import timeclass TelnetClient():def __init__(self,):self.tn = telnetlib.Telnet()# 此函數實現telnet登錄主機def login_host(self,host_ip,username,password):try:# self.tn = telnetlib.Telnet(host_ip,port=23)self.tn.open(host_ip,port=23)except:logging.warning('%s網絡連接失敗'%host_ip)return False# 等待login出現后輸入用戶名,最多等待10秒self.tn.read_until(b'login: ',timeout=10)self.tn.write(username.encode('ascii') + b'\n')# 等待Password出現后輸入用戶名,最多等待10秒self.tn.read_until(b'Password: ',timeout=10)self.tn.write(password.encode('ascii') + b'\n')# 延時兩秒再收取返回結果,給服務端足夠響應時間time.sleep(2)# 獲取登錄結果# read_very_eager()獲取到的是的是上次獲取之后本次獲取之前的所有輸出command_result = self.tn.read_very_eager().decode('ascii')if 'Login incorrect' not in command_result:logging.warning('%s登錄成功'%host_ip)return Trueelse:logging.warning('%s登錄失敗,用戶名或密碼錯誤'%host_ip)return False# 此函數實現執行傳過來的命令,并輸出其執行結果def execute_some_command(self,command):# 執行命令self.tn.write(command.encode('ascii')+b'\n')time.sleep(2)# 獲取命令結果command_result = self.tn.read_very_eager().decode('ascii')logging.warning('命令執行結果:\n%s' % command_result)# 退出telnetdef logout_host(self):self.tn.write(b"exit\n")def StartCheckWeak(): host_ip = '192.168.32.171' username = 'root' password = 'abcd1234' command = 'whoami' telnet_client = TelnetClient() # 如果登錄結果返加True,則執行命令,然后退出 if telnet_client.login_host(host_ip,username,password): telnet_client.execute_some_command(command) telnet_client.execute_some_command("cd /bin && ls") telnet_client.execute_some_command("uname -a") telnet_client.logout_host() if __name__ == '__main__': threads = [] for i in range(0, 10): t = threading.Thread(target=StartCheckWeak) t.setDaemon(True) threads.append(t) t.start() for i in range(0, len(threads)): threads[i].join() """ host_ip = '192.168.32.171' username = 'root' password = 'abcd1234' command = 'whoami' telnet_client = TelnetClient() # 如果登錄結果返加True,則執行命令,然后退出 if telnet_client.login_host(host_ip,username,password): telnet_client.execute_some_command(command) telnet_client.execute_some_command("cd /bin && ls") telnet_client.execute_some_command("uname -a") telnet_client.logout_host() """ """ if __name__ == '__main__':host_ip = '192.168.220.129'username = 'root'password = 'abcd1234'command = 'whoami'telnet_client = TelnetClient()# 如果登錄結果返加True,則執行命令,然后退出if telnet_client.login_host(host_ip,username,password):telnet_client.execute_some_command(command)telnet_client.logout_host() """

?

總結

以上是生活随笔為你收集整理的Python连接telnet客户端连接服务端程序的全部內容,希望文章能夠幫你解決所遇到的問題。

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