Get the App
SLTechnology News&Howtos  ›  Servers  › 

Go language realizes nailing to send notification

Shulou Source: shulou.com Published: 2022-06-03 01:06:03 10月04日 Update

Package mainimport ("bufio"io"fmt"io/ioutil"os/exec"net/http"os"strings") func GetKey () (timestamp string, sign string, err error) {cmd: = exec.Command ("/ bin/python", "/ application/scripts/hezhong_host_status/aaa.py") stdout Err: = cmd.StdoutPipe () if err! = nil {fmt.Println ("Error:can not obtain stdout pipe for command:%s\ n", err) return timestamp, sign, err} if err: = cmd.Start () Err! = nil {fmt.Println ("Error:The command is err", err) return timestamp, sign,err} var Key [] string / / read all output bytes: = bufio.NewReader (stdout) for {line, err:= bytes.ReadString ('\ n') if err = = io.EOF {break} if err! = nil {fmt.Println ("Read Err:" Err)} Key = append (Key,line)} if err: = cmd.Wait () Erratic = nil {fmt.Println ("Wait", err.Error ()) return timestamp, sign, err} timestamp = Key [0] sign = Key [1] return timestamp, sign, nil} func SendDingMsg (msg string) {/ / request address template timestamp, sign, err: = GetKey () if errata = nil {fmt.Println ("GetKey Err:", err)} timestamp = strings.TrimRight (timestamp) "\ n") sign= strings.TrimRight (sign, "\ n") / / timestamp: = time.Now (). UnixNano () / 1e6 / / timestamp: = int64 (1572870805748) / / sign: = "oJ5SHD3IwCWVIcL78k3pgX0tfQnjDfcDZMPXAI%2BvrFE%3D" webHook: = "https://oapi.dingtalk.com/robot/send?access_token=628d11124aef5f9efe2a8c8a6b5afa2b67ab01dxxxxxxxxxxxxxxxxxxxxx&"+" timestamp= "+ timestamp +" & sign= "+ sign content: = `{" msgtype ":" text " "text": {"content": "`+ msg +`"}, "at": {"atMobiles": ["18301371817"], "isAtAll": true}} `/ / create a request req Err: = http.NewRequest ("POST", webHook, strings.NewReader (content)) if err! = nil {fmt.Println (err)} client: = & http.Client {} / / set request header req.Header.Set ("Content-Type", "application/json") req.Header.Set ("User-agent", "firefox") / / send request resp Err: = client.Do (req) / / close request defer resp.Body.Close () fmt.Println (resp.StatusCode) body,_: = ioutil.ReadAll (resp.Body) fmt.Println (string (body)) if err! = nil {fmt.Println ("handle error")}} func main () {SendDingMsg (os.Args [1])}

Secondary Python script:

#! python 2.7import timeimport hmacimport hashlibimport base64import urllibtimestamp = long (round (time.time () * 1000)) secret = 'this is secret'secret_enc = bytes (secret) .encode (' utf-8') string_to_sign ='{}\ n {} '.format (timestamp, secret) string_to_sign_enc = bytes (string_to_sign) .encode (' utf-8') hmac_code = hmac.new (secret_enc, string_to_sign_enc) Digestmod=hashlib.sha256) .digest () sign = urllib.quote_plus (base64.b64encode (hmac_code)) print (timestamp) print (sign)

Tags: Address template script assist output language Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB MySQL vpn Apple