欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python socket套接字實現(xiàn)C/S模式遠程命令執(zhí)行功能案例

 更新時間:2018年07月06日 09:59:23   作者:LandGrey  
這篇文章主要介紹了Python socket套接字實現(xiàn)C/S模式遠程命令執(zhí)行功能,涉及Python socket套接字編寫服務(wù)器/客戶機模式數(shù)據(jù)傳輸相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了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=36

180.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è)計有所幫助。

相關(guān)文章

  • Python實現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計其頁碼

    Python實現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計其頁碼

    pypdf2是一個Python模塊,可以用來讀取、寫入和操作PDF文件,本文就將利用該模塊實現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計其頁碼,需要的小伙伴可以了解一下
    2023-05-05
  • TensorFlow繪制loss/accuracy曲線的實例

    TensorFlow繪制loss/accuracy曲線的實例

    今天小編就為大家分享一篇TensorFlow繪制loss/accuracy曲線的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python實現(xiàn)有序遍歷dict(字典)

    python實現(xiàn)有序遍歷dict(字典)

    這篇文章主要介紹了python實現(xiàn)有序遍歷dict(字典),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python中的if、else、elif語句用法簡明講解

    Python中的if、else、elif語句用法簡明講解

    這篇文章主要介紹了Python中的if、else、elif語句的用法講解,條件判斷語句是程序中流程控制的基礎(chǔ)辦法之一,需要的朋友可以參考下
    2016-03-03
  • Python爬取豆瓣視頻信息代碼實例

    Python爬取豆瓣視頻信息代碼實例

    這篇文章主要介紹了Python爬取豆瓣視頻信息代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • python對綁定事件的鼠標、按鍵的判斷實例

    python對綁定事件的鼠標、按鍵的判斷實例

    今天小編就為大家分享一篇python對綁定事件的鼠標、按鍵的判斷實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python實現(xiàn)時間序列可視化的方法

    Python實現(xiàn)時間序列可視化的方法

    matplotlib庫是一個用于創(chuàng)建出版質(zhì)量圖表的桌面繪圖包(2D繪圖庫),是Python中最基本的可視化工具。這篇文章主要介紹了Python時間序列可視化實現(xiàn),需要的朋友可以參考下
    2019-08-08
  • 解決python3.x安裝numpy成功但import出錯的問題

    解決python3.x安裝numpy成功但import出錯的問題

    這篇文章主要介紹了解決python3.x安裝numpy成功但import出錯的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程

    Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程

    這篇文章主要給大家介紹了關(guān)于Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程,文中通過示例代碼介紹的非常詳細,對大家學習使用PyQuery具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-02-02
  • Python Pandas的簡單使用教程

    Python Pandas的簡單使用教程

    Pandas 是python的一個數(shù)據(jù)分析包,最初由AQR Capital Management于2008年4月開發(fā),并于2009年底開源出來,目前由專注于Python數(shù)據(jù)包開發(fā)的PyData開發(fā)team繼續(xù)開發(fā)和維護,今天通過本文給大家介紹Python Pandas的簡單使用教程,感興趣的朋友一起看看吧
    2021-08-08

最新評論