Python socket套接字實現(xiàn)C/S模式遠程命令執(zhí)行功能案例
本文實例講述了Python socket套接字實現(xiàn)C/S模式遠程命令執(zhí)行功能。分享給大家供大家參考,具體如下:
一. 前言
要求:
使用python的socket套接字編寫服務(wù)器/客戶機模式的遠程命令執(zhí)行腳本。
serverCmd.py 遠程機器上用來執(zhí)行客戶端發(fā)送命令的腳本
clientCmd.py 本地機器上,向遠程服務(wù)器發(fā)送命令的腳本
servers.txt 本地機器上,存放所有的遠程服務(wù)器IP地址文件(僅支持第一個IP)發(fā)送:cmd [command]形式消息,讓遠程主機執(zhí)行命令(本地主機無回顯)
發(fā)送:close session消息,雙方關(guān)閉會話。
二. 源碼
下載地址: 點擊此處本站下載。
注:
1. 代碼注釋較少,建議有一定套接字編程基礎(chǔ)。
2. 或者直接簡單部分修改IP使用。
3. clientCmd.py和servers.txt(修改IP地址后)放在同一目錄。
4.程序為簡單Demo,僅為學習記錄。
serverCmd.py
#!/usr/bin/env python # coding:utf-8 # Build by LandGrey # import time import socket import threading import traceback import subprocess def parsecmd(strings): midsplit = str(strings).split(" ") if len(midsplit) >= 2 and midsplit[0] == "cmd": try: command = subprocess.Popen(strings[4:], shell=True) command.communicate() print "\n" except Exception, e: print e.message traceback.print_exc() def recvdata(port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('', port)) s.listen(1) print "[+] Server is running on port:%s at %s" % (str(port), time.strftime("%Y%m%d %H:%M:%S", time.localtime())) while True: mainsocket, mainhost = s.accept() print "[+] Connect success -> %s at %s" % (str(mainhost), time.strftime("%Y%m%d %H:%M:%S", time.localtime())) if mainhost: while True: data = mainsocket.recv(1024) if data: print "[+] Receive:%s" % data mainsocket.sendall("[Server]success") parsecmd(data) if data == "close session": mainsocket.close() print "[+] Quit success" break break if __name__ == "__main__": # some public variable connPort = 47091 onethreads = threading.Thread(target=recvdata, args=(connPort,)) onethreads.start()
clientCmd.py
#!/usr/bin/env python # coding:utf-8 # Build by LandGrey # import time import socket def readtarget(): global server_list with open(r"servers.txt") as f: for line in f.readlines(): if line[0:1] != "#" and len(line.split(".")) == 4: server_list.append(line) def connserver(host, port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) while True: print "\n[*] Please input command:" data = raw_input() if not data: break s.sendall(data) recvdata = s.recv(1024) print "[+] Send %s:%s -> %s" % (host, str(connPort), data) time.sleep(0) if recvdata: print "[+] Receive :%s" % recvdata if data == "close session": s.close() break if __name__ == "__main__": server_list = [] connPort = 47091 readtarget() if server_list != []: for host in server_list: connserver(host, connPort)
servers.txt
# server ip list
192.168.0.139
三. 運行效果
python serverCmd.py
[+] Server is running on port:47091 at 20161013 17:50:19
[+] Connect success -> ('192.168.0.241', 4255) at 20161013 17:50:32
[+] Receive:hello
[+] Receive:你好
[+] Receive:cmd ip
'ip' 不是內(nèi)部或外部命令,也不是可運行的程序
或批處理文件。[+] Receive:cmd ipconfig
Windows IP 配置
以太網(wǎng)適配器 本地連接:
連接特定的 DNS 后綴 . . . . . . . :
本地鏈接 IPv6 地址. . . . . . . . : ****::****:****:****:*******
IPv4 地址 . . . . . . . . . . . . : 192.168.0.139
子網(wǎng)掩碼 . . . . . . . . . . . . : 255.255.255.0
默認網(wǎng)關(guān). . . . . . . . . . . . . : 192.168.0.1隧道適配器 isatap.{****-6122-4F83-8828-****}:
媒體狀態(tài) . . . . . . . . . . . . : 媒體已斷開
連接特定的 DNS 后綴 . . . . . . . :[+] Receive:cmd ping www.baidu.com
正在 Ping www.a.shifen.com [180.97.33.108] 具有 32 字節(jié)的數(shù)據(jù):
來自 180.97.33.108 的回復(fù): 字節(jié)=32 時間=66ms TTL=36
來自 180.97.33.108 的回復(fù): 字節(jié)=32 時間=66ms TTL=36
來自 180.97.33.108 的回復(fù): 字節(jié)=32 時間=64ms TTL=36
來自 180.97.33.108 的回復(fù): 字節(jié)=32 時間=65ms TTL=36180.97.33.108 的 Ping 統(tǒng)計信息:
數(shù)據(jù)包: 已發(fā)送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),
往返行程的估計時間(以毫秒為單位):
最短 = 64ms,最長 = 66ms,平均 = 65ms[+] Receive:要結(jié)束了
[+] Receive:close session
[+] Quit success
python clientCmd.py
[*] Please input command:
hello
[+] Send 192.168.0.139:47091 -> hello
[+] Receive :[Server]success[*] Please input command:
你好
[+] Send 192.168.0.139:47091 -> 你好
[+] Receive :[Server]success[*] Please input command:
cmd ip
[+] Send 192.168.0.139:47091 -> cmd ip
[+] Receive :[Server]success[*] Please input command:
cmd ipconfig
[+] Send 192.168.0.139:47091 -> cmd ipconfig
[+] Receive :[Server]success[*] Please input command:
cmd ping www.baidu.com
[+] Send 192.168.0.139:47091 -> cmd ping www.baidu.com
[+] Receive :[Server]success[*] Please input command:
要結(jié)束了
[+] Send 192.168.0.139:47091 -> 要結(jié)束了
[+] Receive :[Server]success[*] Please input command:
close session
[+] Send 192.168.0.139:47091 -> close session
[+] Receive :[Server]success
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
- Python socket 套接字實現(xiàn)通信詳解
- python使用原始套接字發(fā)送二層包(鏈路層幀)的方法
- python 基于TCP協(xié)議的套接字編程詳解
- Python網(wǎng)絡(luò)編程之TCP套接字簡單用法示例
- Python網(wǎng)絡(luò)編程之TCP與UDP協(xié)議套接字用法示例
- python利用socketserver實現(xiàn)并發(fā)套接字功能
- Python網(wǎng)絡(luò)編程 Python套接字編程
- 詳解python3中socket套接字的編碼問題解決
- 淺析Python中的套接字編程
相關(guān)文章
Python實現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計其頁碼
pypdf2是一個Python模塊,可以用來讀取、寫入和操作PDF文件,本文就將利用該模塊實現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計其頁碼,需要的小伙伴可以了解一下2023-05-05TensorFlow繪制loss/accuracy曲線的實例
今天小編就為大家分享一篇TensorFlow繪制loss/accuracy曲線的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01解決python3.x安裝numpy成功但import出錯的問題
這篇文章主要介紹了解決python3.x安裝numpy成功但import出錯的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程
這篇文章主要給大家介紹了關(guān)于Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程,文中通過示例代碼介紹的非常詳細,對大家學習使用PyQuery具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-02-02