亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

本文實(shí)例講述了Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能。分享給大家供大家參考,具體如下:

一. 前言

要求:

使用python的socket套接字編寫(xiě)服務(wù)器/客戶機(jī)模式的遠(yuǎn)程命令執(zhí)行腳本。

serverCmd.py 遠(yuǎn)程機(jī)器上用來(lái)執(zhí)行客戶端發(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=36

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

相關(guān)文章

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

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

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

    TensorFlow繪制loss/accuracy曲線的實(shí)例

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

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

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

    Python中的if、else、elif語(yǔ)句用法簡(jiǎn)明講解

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

    Python爬取豆瓣視頻信息代碼實(shí)例

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

    python對(duì)綁定事件的鼠標(biāo)、按鍵的判斷實(shí)例

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

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

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

    解決python3.x安裝numpy成功但import出錯(cuò)的問(wèn)題

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

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

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

    Python Pandas的簡(jiǎn)單使用教程

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

最新評(píng)論