SoFunction
Updated on 2024-12-20

Detailed explanation of python execution shell script to create users and related operations

User sends request, returns account and password

### Utilize the framework flask
Overall Thoughts:

  1. # Purpose: Implement simple login logic
  2. # 1 Need get and post request methods Need to determine request method
  3. # 2 Getting Parameters
  4. # 3 Execute the shell
  5. # 4 If the judgment is all right, return the result

briefcase

...

Delivering messages to templates with flash - need to encrypt the content, so you need to set the secret_key , to do the obfuscation of the encrypted messages

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

Implementing a form using wtf requires customizing a form class

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

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

Set the cursor and the methods get and post.

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

Define a method

def login():
  login_form = LoginForm()

cJudging the way the request is made

if  == 'POST':

Get request parameters

username = ('username')

Validation parameters, wtf a sentence to achieve the checksum, no CSRF token will report an error, add {{ form.csrf_token }} in the form

 if login_form.validate_on_submit():

Execute the shell

 username = username
  print('Create user...')
	# Create users
  ('useradd %(name)s -s /home/work/%(name)s' % {'name': username})
	# Randomize a password
  password = str(((100000, 999999)))
	# Set the password
  ('echo %(name)s:%(pwd)s |chpasswd' % {'name': username, 'pwd': password})
	# Put interview questions under newly created user paths
  ('cp -r /home/renligeng/exam/ /home/work/%(name)s' % {'name': username})
	# Set permissions for the path
  ('chmod 700 /home/work/%(name)s' % {'name': username})
	# Setting user privileges
  ('setfacl -m u:%(name)s:rwx /home/work/%(name)s' % {'name': username})
	# Output
  ('echo "Hello, your account number is:" %(name)s ",Password:" %(pwd)s' % {'name': username, 'pwd': password})

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

All Codes


from flask import Flask, render_template, request, flash
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from  import DataRequired

import os
import random

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

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

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

  # 1 Determine how the request was made
  if  == 'POST':
    # 2 Get request parameters
    username = ('username')
    # 3 authentication parameters No CSRF token will result in an error.
    if login_form.validate_on_submit():

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

      return ('Hello, your account number is:' + username + ',Password:' + password)
    else:
      flash('Please enter a user name')
  return render_template('', form=login_form)

if __name__ == '__main__':
  (
    debug=True
  )

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

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

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

The above is a small introduction to the python execution shell script to create users and related operations detailed integration, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. I would also like to thank you very much for your support of my website!