go語言執行grep -v grep爬坑

· · 138 次點擊 · · 開始瀏覽     編輯

使用go語言的golang.org/x/crypto/ssh包,執行遠程機器上的進程檢查命令
ps -ef | grep -s buildapp.sh | grep -v grep一直報錯

Error: Process exited with status 1

package main

import (
    "fmt"
    "github.com/pkg/sftp"
    "golang.org/x/crypto/ssh"
    "os"
    "path"
    "time"
)

func main() {

    localFile := "/test"
    remoteDir := "/test/test"

    user := "wanglulu"
    pass := "123123"
    host := "192.168.7.100"
    port := 22

    //創建SSH Client
    sshClient, err := GetSSHClient(user, pass, host, port)
    if err != nil {
        fmt.Println("Error:",err)
        return
    }
    defer sshClient.Close()

    //創建SSH Session
    session, err := GetSSHSession(sshClient)
    if err != nil {
        fmt.Println("Error:",err)
        return
    }
    defer session.Close()

    //檢查是否有構建任務正在執行
    c := `ps -ef  | grep -s buildapp.sh  |  grep -v "grep"`

    resp, err := session.Output(c)
    if err != nil {
        fmt.Println("Error:",err)
        return
    }

    fmt.Println("[INFO]", string(resp))

    //創建SSH Session
    sftpClient, err := GetSFTPClient(sshClient)
    if err != nil {
        fmt.Println("Error:",err)
        return
    }
    defer sftpClient.Close()

    //傳輸構建APP需要的資源文件
    srcFile, err := os.Open(localFile)
    if err != nil {
        fmt.Println("Error:",err)
        return
    }
    defer srcFile.Close()

    remoteFileName := path.Base(localFile)
    dstFile, err := sftpClient.Create(path.Join(remoteDir, remoteFileName))
    if err != nil {
        fmt.Println("Error:",err)
        return
    }
    defer dstFile.Close()

    buf := make([]byte, 1024)
    for {
        n, _ := srcFile.Read(buf)
        if n == 0 {
            break
        }
        dstFile.Write(buf[0:n])
    }
}


func GetSSHClient(user, password, host string, port int) (*ssh.Client, error) {
    var (
        auth         []ssh.AuthMethod
        addr         string
        clientConfig *ssh.ClientConfig
        client       *ssh.Client
        err          error
    )

    // get auth method
    auth = make([]ssh.AuthMethod, 0)
    auth = append(auth, ssh.Password(password))

    clientConfig = &ssh.ClientConfig{
        User:            user,
        Auth:            auth,
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
        Timeout:         60 * time.Second,
    }

    // connect to ssh
    addr = fmt.Sprintf("%s:%d", host, port)

    if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
        return nil, err
    }

    return client, nil
}

func GetSFTPClient(sshClient *ssh.Client) (*sftp.Client, error) {
    var (
        sftpClient *sftp.Client
        err        error
    )

    // create sftp client
    if sftpClient, err = sftp.NewClient(sshClient); err != nil {
        return nil, err
    }

    return sftpClient, nil
}

func GetSSHSession(sshClient *ssh.Client) (*ssh.Session, error) {
    var (
        session *ssh.Session
        err     error
    )

    // create session
    if session, err = sshClient.NewSession(); err != nil {
        return nil, err
    }

    return session, nil
}

本文來自:簡書

感謝作者:

查看原文:go語言執行grep -v grep爬坑

入群交流(和以上內容無關):Go中文網 QQ 交流群:729884609 或加微信入微信群:274768166 備注:入群;關注公眾號:Go語言中文網

138 次點擊  
加入收藏 微博
暫無回復
添加一條新回復 (您需要 登錄 后才能回復 沒有賬號 ?)
  • 請盡量讓自己的回復能夠對別人有幫助
  • 支持 Markdown 格式, **粗體**、~~刪除線~~、`單行代碼`
  • 支持 @ 本站用戶;支持表情(輸入 : 提示),見 Emoji cheat sheet
  • 圖片支持拖拽、截圖粘貼等方式上傳