【Golang】 编写一个powershell交互展示界面

使用Golang编写一个powershell交互展示界面

文件结构

└─go-cmd
    │  go.mod
    │  main.go
    ├─static
    │  └─css
    │       style.css
    └─templates
            index.html

项目构成

前端

  • templates/index.html
  • 后端返回的数据保存到output中,html根据res回复的信息重定义为data的dom,使用getElementById取出并再定义为id=output
<!DOCTYPE html>
<html lang="en">
<head>
    <meta  http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="static/css/style.css">
    <title>查询小工具</title>
</head>
<body>
<div class="container">
    <h1>查询工具集</h1>
    <button onclick="executeCommand()">查询按钮</button>
    <pre id="output"></pre>
    <script>
        async function executeCommand() {
            const response = await fetch('/execute-command');
            const data = await response.text();
            document.getElementById('output').textContent = data;
        }
    </script>
</div>
</body>
</html>
  • 一些css样式
  • static/css/style.css
/*外置样式文件*/
body {
    font-family: Arial, sans-serif;
}

.container {
    margin: 20px auto;
    max-width: 600px;
}

button {
    padding: 10px 20px;
    margin-right: 10px;
}

pre {
    background-color: #f0f0f0;
    padding: 10px;
}

后端

  • main.go
  • 需要自定义的ps命令也在这里
package main

import (
    "html/template"
    "log"
    "net/http"
    "os/exec"
)

// 引用模板
func renderIndex(w http.ResponseWriter, r *http.Request) {
    tmpl := template.Must(template.ParseFiles("templates/index.html"))
    //检查一下模板是否链接有误
    err := tmpl.ExecuteTemplate(w, "index.html", nil)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

// 将powershell 模块封装为方法
func executeCommand(w http.ResponseWriter, r *http.Request) {
    //你想要执行的ps命令
    psCommand := "Get-NetIPAddress | Where-Object { $_.AddressFamily -eq 'IPv4' } | Select-Object IPAddress, InterfaceAlias, AddressFamily"
    cmd := exec.Command("powershell", "-Command", psCommand)

    // 执行命令并获取结果
    output, err := cmd.CombinedOutput()
    if err != nil {
        log.Printf("Command execution failed: %s", err)
        http.Error(w, "Internal Server Error", http.StatusInternalServerError)
        return
    }

    // 发送到客户端output
    w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    w.Write(output)
}

func main() {
    // 设置静态文件目录
    fs := http.FileServer(http.Dir("static"))
    http.Handle("/static/", http.StripPrefix("/static/", fs))

    // 设置主页路由
    http.HandleFunc("/", renderIndex)

    // 设置执行命令的路由
    http.HandleFunc("/execute-command", executeCommand)

    // 10.168.1.*:8999 这里是本机测试地址端口 替换成你需要查询的所在主机
    log.Println("Server is running on port 10.168.1.*:8999")
    if err := http.ListenAndServe("10.168.1.*:8999", nil); err != nil {
        log.Fatal(err)
    }
}

使用启动

go run   .\main.go
2024/03/19 23:41:38 Server is running on port 10.168.1.*:8999

file

  • 后续编译成exe或者linux的二进制文件放到目标服务器即可
转载请注明-MrZ-个人博客
THE END
分享
二维码
海报
【Golang】 编写一个powershell交互展示界面
使用Golang编写一个powershell交互展示界面 项目地址: http://git.mrzsir.cn/mrzsir/go-tools-cmd.git 使用 html-template 文件结构 └─go-cmd │ go.mod │ main.go ├─static │ └─css │ ……
<<上一篇
下一篇>>