How do I execute a command on a remote machine in a golang CLI?

前端 未结 4 1189
臣服心动
臣服心动 2021-02-13 07:02

How do I execute a command on a remote machine in a golang CLI? I need to write a golang CLI that can SSH into a remote machine via a key and execute a shell command. Furthermor

4条回答
  •  梦如初夏
    2021-02-13 07:44

    The other solutions here will work, but I'll throw out another option you could try: simplessh. I think it is easier to use. For this question, I would use option 3 below where you can ssh using your key.

    Option 1: SSH to a machine with a password, then run a command

    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
    }
    

    Option 2: SSH to a machine using a set of possible passwords, then run a command

    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
    }
    

    Option 3: SSH to a machine using your key

    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
    }
    

提交回复
热议问题