美文网首页
golang使用ssh包登录服务器

golang使用ssh包登录服务器

作者: 中國壹石頭 | 来源:发表于2015-09-24 15:34 被阅读8322次

ssh包的代码在这里:
https://github.com/golang/crypto.git

不要使用go get,不要使用go get,不要使用go get,不要使用go get,不要使用go get.
git clone出来后,放到$GOPATH/src/golang.org/x/目录下面即可。

package main

import (
"bytes"
"golang.org/x/crypto/ssh"
"fmt"
)

func main() {
fmt.Println("helloworld")

// An SSH client is represented with a ClientConn. Currently only
// the "password" authentication method is supported.
//
// To authenticate with the remote server you must pass at least one
// implementation of AuthMethod via the Auth field in ClientConfig.
config := &ssh.ClientConfig{
    User: "jenkins",
    Auth: []ssh.AuthMethod{
        ssh.Password("654321"),
    },
}
client, err := ssh.Dial("tcp", "11.22.33.44:22", config)
if err != nil {
    panic("Failed to dial: " + err.Error())
}

// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
session, err := client.NewSession()
if err != nil {
    panic("Failed to create session: " + err.Error())
}
defer session.Close()

// Once a Session is created, you can execute a single command on
// the remote side using the Run method.
var b bytes.Buffer
session.Stdout = &b
if err := session.Run("/usr/bin/whoami"); err != nil {
    panic("Failed to run: " + err.Error())
}
fmt.Println(b.String())

}

相关文章

  • golang使用ssh包登录服务器

    ssh包的代码在这里:https://github.com/golang/crypto.git 不要使用go ge...

  • 配置Apache服务器

    1. 登录服务器 Windows使用putty,macOS使用terminal利用ssh远程登录:ssh @

  • ssh自动登录

    ssh自动登录 登录服务器的时候,经常使用ssh进行远程的登录,经常输入密码,比较麻烦,可以使用sshpass配合...

  • 说说Git的ssh key

    ssh key的作用 我们使用ssh登录服务器时,一般常见的会使用用户名/密码方式登录,也可以使用ssh key实...

  • 服务器搭建:(二)SSH登录详解

    本文主要讲解SSH登录Ubuntu服务器以及SSH登录Sftp SSH生成 使用过GIt的人对这个部分很好理解,在...

  • SSH免密码远程登录

    一般的SSH远程登录服务器 ssh登录使用ssh user@host 的方式,如: 这种方式比较低效,尤其是密码复...

  • 2018-08-17--linux连接服务器及上传下载

    一、linux登录服务器: 1、在终端中使用ssh命令连接远程服务器 2、命令格式:ssh -l username...

  • GIT如何指定使用某个SSH进行clone操作

    先说一下本地和服务器间的ssh连接 一般使用ssh命令进行远程登录,都要输入口令。如果使用了ssh,则可以免密登录...

  • ssh登录错误ECDSA host key for ip has

    当我们使用ssh root@ip登录Linux服务器时,服务器报错: 这是由于,ssh连接服务器时,如果之前连接过...

  • SSH多种远程登录方法

    使用 SSH 登录服务器有两种方法:用户账号登录 和 使用公钥登录 用户账号登录 直接使用以下命令 另外可以配置主...

网友评论

      本文标题:golang使用ssh包登录服务器

      本文链接:https://www.haomeiwen.com/subject/bqaicttx.html