Pārlūkot izejas kodu

添加前置命令

tianyunperfect 5 gadi atpakaļ
vecāks
revīzija
c24cca5d23
3 mainītis faili ar 19 papildinājumiem un 4 dzēšanām
  1. 3 1
      app.py
  2. 1 1
      execUtil.py
  3. 15 2
      static/index.html

+ 3 - 1
app.py

@@ -18,7 +18,9 @@ def exe_str_cmd():
         req_json = request.json
         client = get_client(req_json)
         # 转换执行字符串
-        exec_str = "&&".join(list(filter(lambda x: not str.startswith(x, "#"), req_json.get('exec_str').splitlines())))
+        exec_str = "&&".join(list(filter(lambda x: not str.startswith(x, "#"),
+                                         (req_json.get('exec_str_before') + '\n' + req_json.get(
+                                             'exec_str')).splitlines())))
         result_str = run_cmd(client, exec_str)
         client.close()
         return result_str

+ 1 - 1
execUtil.py

@@ -11,7 +11,7 @@ def run_cmd(ssh_client, cmd):
     """
     # bash -l -c解释:-l(login)表示bash作为一个login shell;-c(command)表示执行后面字符串内的命令,这样执行的脚本,可以获取到/etc/profile里的全局变量,包括我们搜索命令的目录PATH
     print("执行命令: " + cmd)
-    stdin, stdout, stderr = ssh_client.exec_command(cmd)
+    stdin, stdout, stderr = ssh_client.exec_command('. /etc/profile && . ~/.bashrc && ' + cmd)
     res, err = stdout.read(), stderr.read()
     result = res if res else err
     result = result.decode('utf-8')

+ 15 - 2
static/index.html

@@ -63,6 +63,13 @@
             <div class="layui-tab-item layui-show">
                 <form class="layui-form" action="">
 
+                    <div class="layui-form-item layui-form-text">
+                        <label class="layui-form-label">前置命令</label>
+                        <div class="layui-input-block">
+                            <textarea placeholder="前置命令" class="layui-textarea" autoHeight="true" id="exec_str_before" style="min-height: 35px"></textarea>
+                        </div>
+                    </div>
+
                     <div class="layui-form-item layui-form-text">
                         <label class="layui-form-label">命令</label>
                         <div class="layui-input-block">
@@ -102,7 +109,12 @@
             $("#password").val(hostInfo["password"]);
             $("#timeout").val(hostInfo["timeout"]);
             $("#exec_str").val(hostInfo["exec_str"]);
+            $("#exec_str_before").val(hostInfo["exec_str_before"]);
         }
+
+        // 设置高度
+        $("#return_str").css('height','500px');
+
     });
 
     // 文本域自动扩充,参考:https://fly.layui.com/jie/64353/
@@ -141,6 +153,7 @@
             "password": $("#password").val(),
             "timeout": $("#timeout").val(),
             "exec_str": $("#exec_str").val(),
+            "exec_str_before": $("#exec_str_before").val(),
         };
         localStorage.setItem('hostInfo', JSON.stringify(data));
     });
@@ -156,6 +169,7 @@
             "username": $("#username").val(),
             "password": $("#password").val(),
             "timeout": $("#timeout").val(),
+            "exec_str_before": $("#exec_str_before").val(),
             "exec_str": selectText,
         };
         $("#exec").addClass('layui-btn-disabled');
@@ -167,7 +181,6 @@
             }
             historyList.unshift(data);
             $("#return_str").val(historyList.join('\n'));
-            $("#return_str").css('height','500px');
             $("#exec").removeClass('layui-btn-disabled');
         });
 
@@ -175,4 +188,4 @@
 
 </script>
 </body>
-</html>
+</html>