python executes shell scripts to create users and related operations

User sends request, returns account number and password

###Utilize Framework flask
Overall thinking:

#Purpose: Logic for simple login
 # 1 Requires get and post request modes Need to determine request modes
 # 2 Get parameters
 # 3 Execute shell
 # 4 If the judgement is OK, the result is returned

Guide Pack

...

flash messaging to templates -- content needs to be encrypted, so secret_key needs to be set to confuse encrypted messages

app = Flask(__name__)
app.secret_key = 'kingdomai'

To implement a form using wtf, you need to customize a form class

#validators=[DataRequired() guarantees that the content is filled in

class LoginForm(FlaskForm):
username = StringField('User name:', validators=[DataRequired()])
submit = SubmitField('Submit')

Set cursor with get and post

@app.route('/form', methods=['GET', 'POST'])

Define a method

def login():
    login_form = LoginForm()

Decide how to request

if request.method == 'POST':

Get Request Parameters

username = request.form.get('username')

Validation parameters, one sentence of wtf validates, no CSRF token will error, add {{form.csrf_token}} to the form

    if login_form.validate_on_submit():

Execute shell

    username = username
    print('Create User...')
	# Create User
    os.system('useradd  %(name)s -s /home/work/%(name)s' % {'name': username})
	# Random one password
    password = str((random.randint(100000, 999999)))
	# Set Password
    os.system('echo %(name)s:%(pwd)s |chpasswd' % {'name': username, 'pwd': password})
	# Place interview questions in the newly created user path
    os.system('cp -r /home/renligeng/exam/ /home/work/%(name)s' % {'name': username})
	# Set the path permissions
    os.system('chmod 700 /home/work/%(name)s' % {'name': username})
	# Set User Permissions
    os.system('setfacl -m u:%(name)s:rwx /home/work/%(name)s' % {'name': username})
	# output
    os.system('echo "Hello,Your account number is:" %(name)s ",Password is:" %(pwd)s' % {'name': username, 'pwd': password})

    return ('Hello,Your account number is:' + username + ',Password is:' + password)

###All Code

mianshi.py
from flask import Flask, render_template, request, flash
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired

import os
import random

app = Flask(__name__)
app.secret_key = 'kingdomai'

class LoginForm(FlaskForm):
    username = StringField('User name:', validators=[DataRequired()])
    submit = SubmitField('Submit')

@app.route('/form', methods=['GET', 'POST'])
def login():
    login_form = LoginForm()

    # 1 Decide how to request
    if request.method == 'POST':
        # 2 Get request parameters
        username = request.form.get('username')
        # 3 Verify parameter without CSRF token will error
        if login_form.validate_on_submit():

            # Execute shell
            username = username
            print('Create User...')
            os.system('useradd  %(name)s -s /home/work/%(name)s' % {'name': username})
            password = str((random.randint(100000, 999999)))
            os.system('echo %(name)s:%(pwd)s |chpasswd' % {'name': username, 'pwd': password})
            os.system('cp -r /home/renligeng/exam/ /home/work/%(name)s' % {'name': username})
            os.system('chmod 700 /home/work/%(name)s' % {'name': username})
            os.system('setfacl -m u:%(name)s:rwx /home/work/%(name)s' % {'name': username})
            os.system('echo "Hello,Your account number is:" %(name)s ",Password is:" %(pwd)s' % {'name': username, 'pwd': password})

            return ('Hello,Your account number is:' + username + ',Password is:' + password)
        else:
            flash('enter one user name')
    return render_template('index.html', form=login_form)

if __name__ == '__main__':
    app.run(
        debug=True
    )

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post">
    {{ form.csrf_token }}
    {{ form.username.label }}{{ form.username }}<br>
    {{ form.submit }}<br>

    {% for message in get_flashed_messages() %}
        {{message}}
    {% endfor %}
    <hr>

</form>
</body>
</html>

Tags: shell

Posted on Wed, 04 Dec 2019 02:48:53 -0800 by kesmithjr