from flask import Flask, jsonify, request, render_template
import paramiko
import asyncio
app = Flask(__name__)
# Set up SSH client outside the login function
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('', username='-01', password='!')
ssh.load_system_host_keys()
@app.route('/', methods=['GET'])
def render_form():
return render_template('index_courency.html')
@app.route('/', methods=['POST'])
def process_form():
username = request.form.get('user')
information = asyncio.run(run_ssh_command(username))
return render_template('index_courency.html', information=information['result'])
# Async function to execute SSH command
async def run_ssh_command(username):
stdin, stdout, stderr = ssh.exec_command(f'/sbin/{username}', get_pty=True)
return {'result': stdout.read().decode('utf-8')}
if __name__ == '__main__':
app.run(debug=True)