使用Django远程执行脚本(三)

作者: 下午茶_da6d | 来源:发表于2019-11-13 12:01 被阅读0次

凡需要登陆服务器、操作大于等于1个步骤,我们都可以集中在一个工具中用页面更加便捷的操作。前边总结了下使用Django快速完成测试数据构造,再来总结一下使用Django远程执行脚本功能实现步骤。

--------以在xxx.xx.xx.xx服务器上执行数据打包脚本pack.php为例--------

一、了解python远程paramiko模块:

参考文档:https://www.cnblogs.com/xiao-apple36/p/9144092.html#_label0

二、编写远程执行指定命令的方法

# -*- coding:utf-8  -*-
import paramiko
def execPack(HostIP):
    username = 'username'
    passwd = 'pwd'
    #比如需要切换到 xx用户下执行xxx.php脚本
    cmd = 'sudo su - xx && php xxx.php 参数列表'

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HostIP, 5860, username, passwd)

        ssh.exec_command(cmd)
        message = "%s 环境执行命令'%s' OK\n" % (HostIP,cmd)
        return  message
        ssh.close()
    except Exception as ex:
        print("\tError %s\n" % ex)

三、实现前端页面

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>打包脚本</title>
  <!-- jquery -->
  <script src="http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>
  <!-- bootstrap -->
  <link href="http://cdn.staticfile.org/twitter-bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
  <script src="http://cdn.staticfile.org/twitter-bootstrap/3.3.1/js/bootstrap.min.js" type="text/javascript"></script>
  <!-- bootstrap-select -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>
  <style>
      table {
          width: 60%;
          border: 0px;
          {#mso-cellspacing:1px ;#}
          cellpadding:"4";
          background-color: azure;
          align-content: center;
      }
      .title{
          width: 20%;
      }
      {#td{#}
      {#    width: 100%;#}
      {#}#}
      input{
          width: 100%;
          background-color: azure;
      }
      select,option{
          width: 300px;
      }
      div{
          width: 60%;
          background-color: azure;
      }

  </style>
</head>
<body>
<h1>xxx打包脚本</h1>
<form method="POST" action="execpack_action">
    <table border="1" >
        <tr>
            <td class="title">IP地址</td>
            <td><input type="text" name="ip" placeholder="请输入环境IP"></td>
        </tr>

        <tr>
            <td colspan="2" align="center"><button type="submit">执行脚本</button></td>
            <td></td>
        </tr>
    </table>
</form>

<div><h3>{{ message }}</h3></div>

</body>
</html>

四、编写处理函数

from xxx.comm import execPack

#执行打包脚本
def execpack(request):
    return render(request,'execpack.html')

def execcomplaint_action(request):

    # try:
        HostIP = request.POST.get("ip")
        message = execPack(HostIP)

    # except Exception:
    #     message = "请检查输入信息!"

        response = render(request, "execpack.html",{"message":message})
        return response

五、可以在页面在指定服务器上执行脚本啦

图片.png

五、功能拓展

继续拓展使页面在任意环境、执行任意带参数/不带参数的脚本

5.1 执行任意脚本:

增加脚本常量文件:
scriptConst.py定义对应关系

script1 = 'php xx1.py '
script2 = 'sh xxx.sh '
.......

5.2 页面增加下拉菜单及脚本参数文本框

定义下拉列表使用户选择执行哪个脚本(参考上此分享)
定义参数文本框,满足执行带参数脚本执行的功能

5.3 view.py中处理接收脚本、参数执行即可

相关文章

网友评论

    本文标题:使用Django远程执行脚本(三)

    本文链接:https://www.haomeiwen.com/subject/ofycictx.html