Spaces:
Paused
Paused
File size: 2,134 Bytes
e80ff99 7095e83 a0cb61a 7095e83 e80ff99 7095e83 e80ff99 7095e83 4ce8c1f 2bb1fde 4ce8c1f 7095e83 e80ff99 7095e83 e80ff99 7095e83 e80ff99 |
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 |
import os
import json
import tornado.web
import subprocess
# New handler for your custom page
class AppHandler(tornado.web.RequestHandler):
# Define the path to the templates directory
HandlerPath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'WebSSH', 'templates')
template_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'WebSSH', 'templates')
def get(self):
self.render(os.path.join(self.template_folder, 'index.html'))
def post(self):
username = self.get_argument("username")
password = self.get_argument("password")
# Check if the user already exists
user_exists = subprocess.run(["id", "-u", username], capture_output=True)
if user_exists.returncode == 0:
# User exists, read the existing SSH key
ssh_dir = f"/home/{username}/.ssh"
private_key_path = f"{ssh_dir}/id_rsa"
if os.path.exists(private_key_path):
with open(private_key_path, "r") as file:
private_key = file.read()
else:
self.set_status(404)
self.write("SSH key not found for existing user.")
return
else:
"""
# Create the user directory and .ssh directory manually
user_home = f"/home/{username}"
ssh_dir = f"{user_home}/.ssh"
os.makedirs(ssh_dir, exist_ok=True)
# Generate SSH key pair for the new user
subprocess.run(["ssh-keygen", "-t", "rsa", "-b", "2048", "-f", f"{ssh_dir}/id_rsa", "-N", ""])
# Read the private key
with open(f"{ssh_dir}/id_rsa", "r") as file:
private_key = file.read()
"""
self.set_status(404)
self.write("SSH key not found for existing user.")
return
# Return the private key to the user
self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', f'attachment; filename=id_rsa')
self.write(private_key) |