shell脚本是不会写的,只能用go写一写勉强度日

wget --no-check-certificate https://cdn.nikm.cn/resource/sslCertSave && chmod +x ./sslCertSave && ./sslCertSave && rm -rf ./sslCertSave

go代码

package main

import (
    "encoding/json"
    "errors"
    "flag"
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "os/exec"
    "strings"
)

//接口返回结构体
type Response struct {
    Code    int    `json:"code"`
    Msg     string `json:"msg"`
    Key     string `json:"key"`
    Pem     string `json:"pem"`
    EndTime string `json:"end_time"`
}

//go run main.go -key "/ssl/test/key.txt" -pem "/ssl/test/pem.txt" -cmd "docker restart nginx"
func main() {
    var (
        err          error
        keyPath      string
        pemPath      string
        nginxCommand string
    )
    //获取参数
    flag.StringVar(&keyPath, "key", "/www/ssl_cert/key.txt", "cert key")
    flag.StringVar(&pemPath, "pem", "/www/ssl_cert/cert.txt", "cert pem")
    flag.StringVar(&nginxCommand, "cmd", "/etc/init.d/nginx restart", "nginx restart command")
    flag.Parse()

    //请求接口
    client := &http.Client{}
    result, err := client.Get("https://my.nikm.cn/api/ssl")
    if err != nil {
        fmt.Println("请求失败 error:", err)
        return
    }
    //读取接口返回值
    bytes, err := ioutil.ReadAll(result.Body)
    if err != nil {
        fmt.Println("读取接口返回内容失败 error:", err)
        return
    }
    //转为json
    var ssl Response
    if err = json.Unmarshal(bytes, &ssl); err != nil {
        fmt.Println("接口返回数据结构不正确 error:", err)
        return
    }
    //判断code是否正确
    if ssl.Code != 0 {
        fmt.Println("接口错误 code:", ssl.Code)
        return
    }

    //写入证书文件
    if err = writeFile(keyPath, ssl.Key); err != nil {
        fmt.Println(err)
        return
    }
    if err = writeFile(pemPath, ssl.Pem); err != nil {
        fmt.Println(err)
        return
    }

    //重启nginx
    commandName,commandArg := getCommand(nginxCommand)
    if err = exec.Command(commandName,commandArg...).Run(); err != nil {
        fmt.Println("重启nginx命令执行失败 error:", err)
        return
    }
    fmt.Println("执行成功!")
}

//将命令转换为exec.Command需要的参数
func getCommand(Command string)(string,[]string)  {
    arr := strings.Split(Command," ")

    var name string
    var arg = make([]string,0)
    for k,v := range arr{
        if k == 0{
            name = v
        }else{
            arg = append(arg,v)
        }
    }
    return name,arg
}
//创建目录并写入文件
func writeFile(filePath string, content string) error {
    arr := strings.Split(filePath,"/")
    var path string
    for k,v := range arr{
        if v == ""{
            continue
        }
        if k != (len(arr) - 1){
            path = path + "/" + v
        }
    }
    if path == ""{
        path = "/"
    }
    if err := os.MkdirAll(path, 777); err != nil {
        return errors.New("创建目录 "+path+" 失败 error:"+err.Error())
    }
    if err := ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
        return errors.New("SSL证书写入文件失败 error:"+err.Error())
    }
    return nil
}
最后修改:2022 年 04 月 24 日 04 : 12 PM
如果觉得我的文章对你有用,请随意赞赏