go实现ssh远程执行命令


本文摘自网络,作者,侵删。

package main

import (

    "fmt"

    "golang.org/x/crypto/ssh"

    "log"

    "time"

)

func main(){

    sshHost := "192.168.6.126"

    sshUser := "root"

    sshPassword := "123456"

    sshType := "password"

    sshPort := 22

    //创建sshp登陆配置

    config := &ssh.ClientConfig{

        Timeout:         5*time.Second,//ssh 连接time out 时间一秒钟, 如果ssh验证错误 会在一秒内返回

        User:            sshUser,

        HostKeyCallback: ssh.InsecureIgnoreHostKey(), //这个可以, 但是不够安全

        //HostKeyCallback: hostKeyCallBackFunc(h.Host),

    }

    if sshType == "password" {

        config.Auth = []ssh.AuthMethod{ssh.Password(sshPassword)}

    }

    //dial 获取ssh client

阅读剩余部分

相关阅读 >>

手撸Golang Go与微服务 saga模式之5

手撸Golang 结构型设计模式 享元模式

手撸Golang etcd raft协议之5

优雅的实现 Golang rest api 架构

Golang中“=”与“:=”的区别

Gocn酷Go推荐】ip2location 解析 ip 地址库

身份证号验证库

值得推荐的五种自动化代码审查工具

最清晰易懂的 Go waitgroup 源码剖析

手撸Golang 基本数据结构与算法 链表

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...