golang使用ssh远程连接服务器并执行命令

喜欢而已 提交于 2019-12-05 01:04:32

安装golang.org/x

直接去github上面,把https://github.com/zieckey/golang.org,把整个目录拷贝下来放到你的gopath下面即可。记住在gopath的src下面,一定是golang.org/x/... 

使用

package main

import (
    "fmt"
    "golang.org/x/crypto/ssh"
    "log"
    "net"
    "time"
)

//连接的配置
type ClientConfig struct {
    Host       string       //ip
    Port       int64        // 端口
    Username   string       //用户名
    Password   string       //密码
    Client     *ssh.Client //ssh client
    LastResult string       //最近一次运行的结果
}

func (cliConf *ClientConfig) createClient(host string, port int64, username, password string) {
    var (
        client  *ssh.Client
        err     error
    )
    cliConf.Host = host
    cliConf.Port = port
    cliConf.Username = username
    cliConf.Password = password
    cliConf.Port = port

    //一般传入四个参数:user,[]ssh.AuthMethod{ssh.Password(password)}, HostKeyCallback,超时时间,
    config := ssh.ClientConfig{
        User: cliConf.Username,
        Auth: []ssh.AuthMethod{ssh.Password(password)},
        HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
            return nil
        },
        Timeout: 10 * time.Second,
    }
    addr := fmt.Sprintf("%s:%d", cliConf.Host, cliConf.Port)

    //获取client
    if client, err = ssh.Dial("tcp", addr, &config); err != nil {
        log.Fatalln("error occurred:", err)
    }

    cliConf.Client = client
}

func (cliConf *ClientConfig) RunShell(shell string) string {
    var (
        session  *ssh.Session
        err     error
    )

    //获取session,这个session是用来远程执行操作的
    if session, err = cliConf.Client.NewSession(); err != nil {
        log.Fatalln("error occurred:", err)
    }

    //执行shell
    if output, err := session.CombinedOutput(shell); err != nil {
        log.Fatalln("error occurred:", err)
    } else {
        cliConf.LastResult = string(output)
    }
    return cliConf.LastResult
}

func main() {
    cliConf := new(ClientConfig)
    cliConf.createClient("xxxx.xxx.xx.xx", 22, "root", "xxxxxxxxx")
    /*
    可以看到我们这里每次执行一条命令都会创建一条session
    这是因为一条session默认只能执行一条命令
    并且两条命令不可以分开写
    比如:
    cliConf.RunShell("cd /opt")
    cliConf.RunShell("ls")
    这两条命令是无法连续的,下面的ls查看的依旧是~目录
    因此我们可以连着写,使用;分割
     */
    fmt.Println(cliConf.RunShell("cd /opt; ls -l"))
    /*
    total 20
    drwxr-xr-x 3 root root 4096 Nov 18 14:05 hadoop
    drwxr-xr-x 3 root root 4096 Nov 18 14:20 hive
    drwxr-xr-x 3 root root 4096 Nov 18 15:07 java
    drwxr-xr-x 3 root root 4096 Nov  4 23:01 kafka
    drwxr-xr-x 3 root root 4096 Nov  4 22:54 zookeeper
     */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!