详解python执行shell脚本创建用户及相关操作

用户发送请求,返回帐号和密码

###利用框架flask
整体思路:

  1. # 目的:实现简单的登录的逻辑
  2. # 1需要get和post请求方式 需要判断请求方式
  3. # 2获取参数
  4. # 3执行shell
  5. # 4如果判断都没问题,就返回结果

 导包

...

给模版传递消息 用flash --需要对内容加密,因此需要设置 secret_key , 做加密消息的混淆

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

使用wtf实现表单,需要自定义一个表单类

#validators=[DataRequired()保证填写了内容

class LoginForm(FlaskForm):
username = StringField('用户名:', validators=[DataRequired()])
submit = SubmitField('提交')

设置游标,并且方法为get和post

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

定义一个方法

def login():
  login_form = LoginForm()

c判断请求方式

if request.method == 'POST':

获取请求参数

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

验证参数,wtf一句话实现校验,没有CSRF token将报错,在表单中添加{{ form.csrf_token }}

if login_form.validate_on_submit():

执行shell

username = username
  print('创建用户...')
	# 创建用户
  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 "您好,您的账号为:" %(name)s ",密码为:" %(pwd)s' % {'name': username, 'pwd': password})

  return ('您好,您的账号为:' + username + ',密码为:' + password)

全部代码

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('用户名:', validators=[DataRequired()])
  submit = SubmitField('提交')

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

  # 1判断请求方式
  if request.method == 'POST':
    # 2获取请求参数
    username = request.form.get('username')
    # 3验证参数 没有CSRF token 会报错
    if login_form.validate_on_submit():

      # 执行shell
      username = username
      print('创建用户...')
      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 "您好,您的账号为:" %(name)s ",密码为:" %(pwd)s' % {'name': username, 'pwd': password})

      return ('您好,您的账号为:' + username + ',密码为:' + password)
    else:
      flash('请输入用户名')
  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>

 

相关推荐