Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active March 19, 2024 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/ca0b0e9a5aec7e827a574c34dce8aec1 to your computer and use it in GitHub Desktop.
Save iloveitaly/ca0b0e9a5aec7e827a574c34dce8aec1 to your computer and use it in GitHub Desktop.
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
import subprocess
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
client_ip = self.client_address[0]
if self.path == '/logout':
user_id = subprocess.check_output("id -u assistant", shell=True).decode().strip()
command = f"launchctl bootout gui/{user_id}"
# Log the client IP and the command before executing
log_message = f"Request from {client_ip}: Executing command '{command}'"
print(log_message)
# Execute the command
subprocess.run(command, shell=True)
# Respond to the client
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
response_message = f'Logout command executed for IP: {client_ip}'
self.wfile.write(response_message.encode())
else:
self.send_response(404)
self.end_headers()
self.wfile.write(b'Not Found')
def run(server_class=HTTPServer, handler_class=RequestHandler, port=51789):
ip = '0.0.0.0' # Bind to all available interfaces
server_address = (ip, port)
httpd = server_class(server_address, handler_class)
try:
print(f"Starting http server on {ip}:{port}")
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("Server stopped.")
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment