Python socket套接字實現(xiàn)C/S模式遠程命令執(zhí)行功能案例
本文實例講述了Python socket套接字實現(xiàn)C/S模式遠程命令執(zhí)行功能。分享給大家供大家參考,具體如下:
一. 前言
要求:
使用python的socket套接字編寫服務器/客戶機模式的遠程命令執(zhí)行腳本。
serverCmd.py 遠程機器上用來執(zhí)行客戶端發(fā)送命令的腳本
clientCmd.py 本地機器上,向遠程服務器發(fā)送命令的腳本
servers.txt 本地機器上,存放所有的遠程服務器IP地址文件(僅支持第一個IP)發(fā)送:cmd [command]形式消息,讓遠程主機執(zhí)行命令(本地主機無回顯)
發(fā)送:close session消息,雙方關閉會話。
二. 源碼
下載地址: 點擊此處本站下載。
注:
1. 代碼注釋較少,建議有一定套接字編程基礎。
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)關. . . . . . . . . . . . . : 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 的回復: 字節(jié)=32 時間=66ms TTL=36
來自 180.97.33.108 的回復: 字節(jié)=32 時間=66ms TTL=36
來自 180.97.33.108 的回復: 字節(jié)=32 時間=64ms TTL=36
來自 180.97.33.108 的回復: 字節(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
更多關于Python相關內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
- Python 網(wǎng)絡編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
- Python socket 套接字實現(xiàn)通信詳解
- python使用原始套接字發(fā)送二層包(鏈路層幀)的方法
- python 基于TCP協(xié)議的套接字編程詳解
- Python網(wǎng)絡編程之TCP套接字簡單用法示例
- Python網(wǎng)絡編程之TCP與UDP協(xié)議套接字用法示例
- python利用socketserver實現(xiàn)并發(fā)套接字功能
- Python網(wǎng)絡編程 Python套接字編程
- 詳解python3中socket套接字的編碼問題解決
- 淺析Python中的套接字編程
相關文章
Python實現(xiàn)數(shù)據(jù)透視表詳解
今天小編就為大家分享一篇用Python實現(xiàn)數(shù)據(jù)的透視表的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-10-10
詳解pandas中MultiIndex和對象實際索引不一致問題
這篇文章主要介紹了詳解pandas中MultiIndex和對象實際索引不一致問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Python enumerate函數(shù)功能與用法示例
這篇文章主要介紹了Python enumerate函數(shù)功能與用法,結(jié)合實例形式分析了enumerate函數(shù)針對列表、字符串遍歷操作相關使用技巧,需要的朋友可以參考下2019-03-03

