第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 golang CLI 中在遠(yuǎn)程機(jī)器上執(zhí)行命令?

如何在 golang CLI 中在遠(yuǎn)程機(jī)器上執(zhí)行命令?

Go
不負(fù)相思意 2022-01-17 16:23:08
如何在 golang CLI 中在遠(yuǎn)程機(jī)器上執(zhí)行命令?我需要編寫(xiě)一個(gè) golang CLI,它可以通過(guò)密鑰 SSH 到遠(yuǎn)程機(jī)器并執(zhí)行 shell 命令。此外,我需要能夠做到這一點(diǎn)。例如,通過(guò) SSH 連接到一臺(tái)機(jī)器(如云堡壘),然后通過(guò) SSH 連接到另一臺(tái)內(nèi)部機(jī)器并執(zhí)行 shell 命令。我(還)沒(méi)有找到任何例子。
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

"golang.org/x/crypto/ssh"您可以使用該軟件包通過(guò) SSH 在遠(yuǎn)程計(jì)算機(jī)上運(yùn)行命令。


這是一個(gè)示例函數(shù),演示了在遠(yuǎn)程機(jī)器上運(yùn)行單個(gè)命令并返回輸出的簡(jiǎn)單用法:


//e.g. output, err := remoteRun("root", "MY_IP", "PRIVATE_KEY", "ls")

func remoteRun(user string, addr string, privateKey string, cmd string) (string, error) {

    // privateKey could be read from a file, or retrieved from another storage

    // source, such as the Secret Service / GNOME Keyring

    key, err := ssh.ParsePrivateKey([]byte(privateKey))

    if err != nil {

        return "", err

    }

    // Authentication

    config := &ssh.ClientConfig{

        User: user,

        // https://github.com/golang/go/issues/19767 

        // as clientConfig is non-permissive by default 

        // you can set ssh.InsercureIgnoreHostKey to allow any host 

        HostKeyCallback: ssh.InsecureIgnoreHostKey(),

        Auth: []ssh.AuthMethod{

            ssh.PublicKeys(key),

        },

        //alternatively, you could use a password

        /*

            Auth: []ssh.AuthMethod{

                ssh.Password("PASSWORD"),

            },

        */

    }

    // Connect

    client, err := ssh.Dial("tcp", net.JoinHostPort(addr, "22"), config)

    if err != nil {

        return "", err

    }

    // Create a session. It is one session per command.

    session, err := client.NewSession()

    if err != nil {

        return "", err

    }

    defer session.Close()

    var b bytes.Buffer  // import "bytes"

    session.Stdout = &b // get output

    // you can also pass what gets input to the stdin, allowing you to pipe

    // content from client to server

    //      session.Stdin = bytes.NewBufferString("My input")


    // Finally, run the command

    err = session.Run(cmd)

    return b.String(), err

}


查看完整回答
反對(duì) 回復(fù) 2022-01-17
?
鴻蒙傳說(shuō)

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

嘗試使用 os/exec https://golang.org/pkg/os/exec/來(lái)執(zhí)行 ssh


package main


import (

    "bytes"

    "log"

    "os/exec"

)


func main() {

    cmd := exec.Command("ssh", "remote-machine", "bash-command")

    var out bytes.Buffer

    cmd.Stdout = &out

    err := cmd.Run()

    if err != nil {

        log.Fatal(err)

    }

}

要跳過(guò)機(jī)器,請(qǐng)使用 ssh 配置文件中的 ProxyCommand 指令。


Host remote_machine_name

  ProxyCommand ssh -q bastion nc remote_machine_ip 22


查看完整回答
反對(duì) 回復(fù) 2022-01-17
?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

這里的其他解決方案也可以,但我會(huì)拋出另一個(gè)你可以嘗試的選項(xiàng):simplessh。我認(rèn)為它更容易使用。對(duì)于這個(gè)問(wèn)題,我將使用下面的選項(xiàng) 3,您可以在其中使用您的密鑰進(jìn)行 ssh。


選項(xiàng) 1:使用密碼 SSH 到機(jī)器,然后運(yùn)行命令


import (

    "log"


    "github.com/sfreiberg/simplessh"

)


func main() error {

    var client *simplessh.Client

    var err error


    if client, err = simplessh.ConnectWithPassword("hostname_to_ssh_to", "username", "password"); err != nil {

        return err

    }


    defer client.Close()


    // Now run the commands on the remote machine:

    if _, err := client.Exec("cat /tmp/somefile"); err != nil {

        log.Println(err)

    }


    return nil

}

選項(xiàng) 2:使用一組可能的密碼通過(guò) SSH 連接到機(jī)器,然后運(yùn)行命令


import (

    "log"


    "github.com/sfreiberg/simplessh"

)


type access struct {

    login    string

    password string

}


var loginAccess []access


func init() {

    // Initialize all password to try

    loginAccess = append(loginAccess, access{"root", "rootpassword1"})

    loginAccess = append(loginAccess, access{"someuser", "newpassword"})

}


func main() error {

    var client *simplessh.Client

    var err error


    // Try to connect with first password, then tried second else fails gracefully

    for _, credentials := range loginAccess {

        if client, err = simplessh.ConnectWithPassword("hostname_to_ssh_to", credentials.login, credentials.password); err == nil {

            break

        }

    }


    if err != nil {

        return err

    }


    defer client.Close()


    // Now run the commands on the remote machine:

    if _, err := client.Exec("cat /tmp/somefile"); err != nil {

        log.Println(err)

    }


    return nil

}

選項(xiàng) 3:使用您的密鑰通過(guò) SSH 連接到機(jī)器


import (

    "log"


    "github.com/sfreiberg/simplessh"

)


func SshAndRunCommand() error {

    var client *simplessh.Client

    var err error


    // Option A: Using a specific private key path:

    //if client, err = simplessh.ConnectWithKeyFile("hostname_to_ssh_to", "username", "/home/user/.ssh/id_rsa"); err != nil {


    // Option B: Using your default private key at $HOME/.ssh/id_rsa:

    //if client, err = simplessh.ConnectWithKeyFile("hostname_to_ssh_to", "username"); err != nil {


    // Option C: Use the current user to ssh and the default private key file:

    if client, err = simplessh.ConnectWithKeyFile("hostname_to_ssh_to"); err != nil {

        return err

    }


    defer client.Close()


    // Now run the commands on the remote machine:

    if _, err := client.Exec("cat /tmp/somefile"); err != nil {

        log.Println(err)

    }


    return nil

}


查看完整回答
反對(duì) 回復(fù) 2022-01-17
  • 3 回答
  • 0 關(guān)注
  • 237 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)