File size: 4,767 Bytes
81aa0cb 58e8f4d 8037bf7 81aa0cb db1a0dc 81aa0cb 8037bf7 58e8f4d 90674f3 |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
from flask import Flask, request, jsonify, render_template_string
import subprocess
app = Flask(__name__)
# Route to the homepage with embedded HTML
@app.route('/')
def index():
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Administrator Terminal</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.container {
width: 80%;
margin: 50px auto;
padding: 20px;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
#terminal {
width: 100%;
height: 300px;
background-color: #000;
color: #0f0;
font-family: "Courier New", Courier, monospace;
padding: 10px;
overflow-y: scroll;
white-space: pre-wrap;
border-radius: 5px;
margin-bottom: 10px;
}
#command {
width: 100%;
padding: 10px;
font-size: 1em;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 20px;
font-size: 1em;
border: none;
border-radius: 5px;
background-color: #28a745;
color: white;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
</style>
</head>
<body>
<div class="container">
<h2>Administrator Terminal</h2>
<div id="terminal"></div>
<input type="text" id="command" placeholder="Enter command..." autofocus>
<button onclick="sendCommand()">Execute</button>
</div>
<script>
// Function to send command to the Flask server
function sendCommand() {
const command = document.getElementById('command').value;
if (command.trim() === '') {
alert('Please enter a command');
return;
}
fetch('/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'command': command
})
})
.then(response => response.json())
.then(data => {
const terminal = document.getElementById('terminal');
if (data.stdout) {
terminal.innerHTML += '> ' + command + '\\n' + data.stdout + '\\n';
}
if (data.stderr) {
terminal.innerHTML += '> ' + command + '\\n' + data.stderr + '\\n';
}
if (data.error) {
terminal.innerHTML += '> ' + command + '\\n' + data.error + '\\n';
}
document.getElementById('command').value = '';
terminal.scrollTop = terminal.scrollHeight; // Scroll to the bottom
})
.catch(error => {
console.error('Error:', error);
});
}
// Allow pressing Enter to send the command
document.getElementById('command').addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
sendCommand();
}
});
</script>
</body>
</html>
"""
return render_template_string(html)
# Route to execute a command
@app.route('/execute', methods=['POST'])
def execute():
try:
# Get the command from the request
command = request.form['command']
# Execute the command and capture the output
result = subprocess.run(command, shell=True, capture_output=True, text=True)
# Return the output (stdout and stderr)
return jsonify({
'stdout': result.stdout,
'stderr': result.stderr
})
except Exception as e:
return jsonify({
'error': str(e)
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True) |