golang 去SSH服务器完整的例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 去SSH服务器完整的例子相关的知识,希望对你有一定的参考价值。

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"

	"code.google.com/p/go.crypto/ssh"
	"code.google.com/p/go.crypto/ssh/terminal"
)

func main() {
	// An SSH server is represented by a ServerConfig, which holds
	// certificate details and handles authentication of ServerConns.
	config := &ssh.ServerConfig{
		PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool {
			return user == "testuser" && pass == "tiger"
		},
	}

	pemBytes, err := ioutil.ReadFile("id_rsa")
	if err != nil {
		log.Fatal("Failed to load private key:", err)
	}
	if err = config.SetRSAPrivateKey(pemBytes); err != nil {
		log.Fatal("Failed to parse private key:", err)
	}

	// Once a ServerConfig has been configured, connections can be
	// accepted.
	conn, err := ssh.Listen("tcp", "0.0.0.0:2022", config)
	if err != nil {
		log.Fatal("failed to listen for connection")
	}
	for {
		// A ServerConn multiplexes several channels, which must 
		// themselves be Accepted.
		log.Println("accept")
		sConn, err := conn.Accept()
		if err != nil {
			log.Println("failed to accept incoming connection")
			continue
		}
		if err := sConn.Handshake(); err != nil {
			log.Println("failed to handshake")
			continue
		}
		go handleServerConn(sConn)
	}
}

func handleServerConn(sConn *ssh.ServerConn) {
	defer sConn.Close()
	for {
		// Accept reads from the connection, demultiplexes packets
		// to their corresponding channels and returns when a new
		// channel request is seen. Some goroutine must always be
		// calling Accept; otherwise no messages will be forwarded
		// to the channels.
		ch, err := sConn.Accept()
		if err == io.EOF {
			return
		}
		if err != nil {
			log.Println("handleServerConn Accept:", err)
			break
		}
		// Channels have a type, depending on the application level
		// protocol intended. In the case of a shell, the type is
		// "session" and ServerShell may be used to present a simple
		// terminal interface.
		if ch.ChannelType() != "session" {
			ch.Reject(ssh.UnknownChannelType, "unknown channel type")
			break
		}
		go handleChannel(ch)
	}
}

func handleChannel(ch ssh.Channel) {
	term := terminal.NewTerminal(ch, "> ")
	serverTerm := &ssh.ServerTerminal{
		Term:    term,
		Channel: ch,
	}
	ch.Accept()
	defer ch.Close()
	for {
		line, err := serverTerm.ReadLine()
		if err == io.EOF {
			return
		}
		if err != nil {
			log.Println("handleChannel readLine err:", err)
			continue
		}
		fmt.Println(line)
	}
}

Gravitational Teleport 是一个先进的 SSH 服务器,基于 Golang SSH 构建,完全兼容 OpenSSH

Gravitational Teleport 是一个先进的 SSH 服务器,可通过 SSH 或者 HTTPS 远程访问 Linux 服务器。其目的是为了替代 sshd。Teleport 可以轻松让团队以最佳实践来使用 SSH,例如:

  • 无需分发密钥,Teleport 使用基于证书的访问并实现自动过期

  • 增强了两阶段身份验证

  • 集群支持,每个 Teleport 节点是集群的一部分,而且可通过 Web UI 浏览

  • 可以记录和重放 SSH 会话,方便分享和审计

  • 通过会话的共享来实现协作诊断问题

  • 可通过 HTTPS 访问置于防火墙之后的服务

  • 可通过 OAuth 实现对其他认证系统的支持

Teleport 基于 Golang SSH 构建,完全兼容 OpenSSH。

http://www.oschina.net/p/gravitational-teleport

以上是关于golang 去SSH服务器完整的例子的主要内容,如果未能解决你的问题,请参考以下文章

golang 去lang SSH连接

golang 展示恐慌,推迟和恢复结合使用的完整例子

golang 去HTTP接口简单例子

golang 实现ssh公钥登录

golang 学习关于去符文和黑客攻击一些例子。

golang通过ssh实现远程文件传输