Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能案例
本文實(shí)例講述了Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能。分享給大家供大家參考,具體如下:
一. 前言
要求:
使用python的socket套接字編寫(xiě)服務(wù)器/客戶(hù)機(jī)模式的遠(yuǎn)程命令執(zhí)行腳本。
serverCmd.py 遠(yuǎn)程機(jī)器上用來(lái)執(zhí)行客戶(hù)端發(fā)送命令的腳本
clientCmd.py 本地機(jī)器上,向遠(yuǎn)程服務(wù)器發(fā)送命令的腳本
servers.txt 本地機(jī)器上,存放所有的遠(yuǎn)程服務(wù)器IP地址文件(僅支持第一個(gè)IP)發(fā)送:cmd [command]形式消息,讓遠(yuǎn)程主機(jī)執(zhí)行命令(本地主機(jī)無(wú)回顯)
發(fā)送:close session消息,雙方關(guān)閉會(huì)話。
二. 源碼
下載地址: 點(diǎn)擊此處本站下載。
注:
1. 代碼注釋較少,建議有一定套接字編程基礎(chǔ)。
2. 或者直接簡(jiǎn)單部分修改IP使用。
3. clientCmd.py和servers.txt(修改IP地址后)放在同一目錄。
4.程序?yàn)楹?jiǎn)單Demo,僅為學(xué)習(xí)記錄。
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
三. 運(yùn)行效果
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)部或外部命令,也不是可運(yùn)行的程序
或批處理文件。[+] Receive:cmd ipconfig
Windows IP 配置
以太網(wǎng)適配器 本地連接:
連接特定的 DNS 后綴 . . . . . . . :
本地鏈接 IPv6 地址. . . . . . . . : ****::****:****:****:*******
IPv4 地址 . . . . . . . . . . . . : 192.168.0.139
子網(wǎng)掩碼 . . . . . . . . . . . . : 255.255.255.0
默認(rèn)網(wǎng)關(guān). . . . . . . . . . . . . : 192.168.0.1隧道適配器 isatap.{****-6122-4F83-8828-****}:
媒體狀態(tài) . . . . . . . . . . . . : 媒體已斷開(kāi)
連接特定的 DNS 后綴 . . . . . . . :[+] Receive:cmd ping www.baidu.com
正在 Ping www.a.shifen.com [180.97.33.108] 具有 32 字節(jié)的數(shù)據(jù):
來(lái)自 180.97.33.108 的回復(fù): 字節(jié)=32 時(shí)間=66ms TTL=36
來(lái)自 180.97.33.108 的回復(fù): 字節(jié)=32 時(shí)間=66ms TTL=36
來(lái)自 180.97.33.108 的回復(fù): 字節(jié)=32 時(shí)間=64ms TTL=36
來(lái)自 180.97.33.108 的回復(fù): 字節(jié)=32 時(shí)間=65ms TTL=36180.97.33.108 的 Ping 統(tǒng)計(jì)信息:
數(shù)據(jù)包: 已發(fā)送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),
往返行程的估計(jì)時(shí)間(以毫秒為單位):
最短 = 64ms,最長(zhǎng) = 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)容可查看本站專(zhuān)題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
- Python socket 套接字實(shí)現(xiàn)通信詳解
- python使用原始套接字發(fā)送二層包(鏈路層幀)的方法
- python 基于TCP協(xié)議的套接字編程詳解
- Python網(wǎng)絡(luò)編程之TCP套接字簡(jiǎn)單用法示例
- Python網(wǎng)絡(luò)編程之TCP與UDP協(xié)議套接字用法示例
- python利用socketserver實(shí)現(xiàn)并發(fā)套接字功能
- Python網(wǎng)絡(luò)編程 Python套接字編程
- 詳解python3中socket套接字的編碼問(wèn)題解決
- 淺析Python中的套接字編程
相關(guān)文章
Python實(shí)現(xiàn)數(shù)據(jù)透視表詳解
今天小編就為大家分享一篇用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-10-10
詳解pandas中MultiIndex和對(duì)象實(shí)際索引不一致問(wèn)題
這篇文章主要介紹了詳解pandas中MultiIndex和對(duì)象實(shí)際索引不一致問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
python使用super()出現(xiàn)錯(cuò)誤解決辦法
這篇文章主要介紹了python使用super()出現(xiàn)錯(cuò)誤解決辦法的相關(guān)資料,對(duì)于TypeError: must be type, not classobj的錯(cuò)誤進(jìn)行處理,需要的朋友可以參考下2017-08-08
Python網(wǎng)絡(luò)爬蟲(chóng)技術(shù)高階用法
網(wǎng)絡(luò)爬蟲(chóng)成為了自動(dòng)化數(shù)據(jù)抓取的核心工具,Python?擁有強(qiáng)大的第三方庫(kù)支持,在網(wǎng)絡(luò)爬蟲(chóng)領(lǐng)域的應(yīng)用尤為廣泛,本文將深入探討?Python?網(wǎng)絡(luò)爬蟲(chóng)的高階用法,包括處理反爬蟲(chóng)機(jī)制、動(dòng)態(tài)網(wǎng)頁(yè)抓取、分布式爬蟲(chóng)以及并發(fā)和異步爬蟲(chóng)等技術(shù),幫助讀者掌握高級(jí)Python爬蟲(chóng)技術(shù)2024-12-12
Flask項(xiàng)目中實(shí)現(xiàn)短信驗(yàn)證碼和郵箱驗(yàn)證碼功能
這篇文章主要介紹了Flask項(xiàng)目中實(shí)現(xiàn)短信驗(yàn)證碼和郵箱驗(yàn)證碼功能,需本文通過(guò)截圖實(shí)例代碼的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-12-12
pandas改變df列的順序的方法實(shí)現(xiàn)
本文主要介紹了pandas改變df列的順序的方法實(shí)現(xiàn),主要使用 Pandas 中的 reindex() 方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03
Python enumerate函數(shù)功能與用法示例
這篇文章主要介紹了Python enumerate函數(shù)功能與用法,結(jié)合實(shí)例形式分析了enumerate函數(shù)針對(duì)列表、字符串遍歷操作相關(guān)使用技巧,需要的朋友可以參考下2019-03-03
Python項(xiàng)目打包成apk或者其他端的應(yīng)用程序
本文主要介紹了使用Kivy和Buildozer將Python項(xiàng)目打包成Android APK文件的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
python回歸分析邏輯斯蒂模型之多分類(lèi)任務(wù)詳解
這篇文章主要為大家介紹了python回歸分析邏輯斯蒂模型之多分類(lèi)任務(wù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

