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


    安装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
    	 */
    }
    
    
  • 相关阅读:
    狼文化的一点思考
    数据可视化之风向图
    谈谈JavaScript代码混淆
    比尔盖茨2016好书推荐
    Cesium原理篇:glTF
    个人 产品 团队(下):个人与团队
    技术 产品 团队(上):如何成为超级个体
    惊艳的HTML5动画特效及源码
    精心挑选的HTML5/CSS3应用及源码
    炫酷霸气的HTML5/jQuery应用及源码
  • 原文地址:https://www.cnblogs.com/traditional/p/11893916.html
Copyright © 2020-2023  润新知