golang ssh demo

密码登入:

package main

import (
	"fmt"

	"golang.org/x/crypto/ssh"
)

func main() {

	// 目标服务器地址
	targetAddr := "ip:22"
	// 创建 SSH 客户端连接
	sshConfig := &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{
			ssh.Password("passwd"),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Ignore host key verification (unsafe)
	}

	sshClient, err := ssh.Dial("tcp", targetAddr, sshConfig)
	if err != nil {
		fmt.Println("Error creating SSH client:", err)
		return
	}
	defer sshClient.Close()

	// 在 SSH 连接上执行一些操作,这里以发送登入请求为例
	session, err := sshClient.NewSession()
	if err != nil {
		fmt.Println("Error creating SSH session:", err)
		return
	}
	defer session.Close()

	// 这里可以执行其他 SSH 操作,例如执行命令、传输文件等
	// 以下演示发送一个简单的命令
	output, err := session.CombinedOutput("ls -l")
	if err != nil {
		fmt.Println("Error executing command:", err)
		return
	}

	fmt.Println("Command output:", string(output))
}

私钥登录:

package main

import (
	"fmt"
	"io/ioutil"

	"golang.org/x/crypto/ssh"
)

func main() {

	// 目标服务器地址
	targetAddr := "ip:22"

	// 使用公钥进行身份验证
	privateKey, err := parsePrivateKey("/root/.ssh/id_rsa")
	if err != nil {
		fmt.Println("Error parsing private key:", err)
		return
	}

	sshConfig := &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(privateKey),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Ignore host key verification (unsafe)
	}
	sshClient, err := ssh.Dial("tcp", targetAddr, sshConfig)
	if err != nil {
		fmt.Println("Error creating SSH client:", err)
		return
	}
	defer sshClient.Close()

	// 在 SSH 连接上执行一些操作,这里以发送登入请求为例
	session, err := sshClient.NewSession()
	if err != nil {
		fmt.Println("Error creating SSH session:", err)
		return
	}
	defer session.Close()

	// 这里可以执行其他 SSH 操作,例如执行命令、传输文件等
	// 以下演示发送一个简单的命令
	output, err := session.CombinedOutput("ls -l")
	if err != nil {
		fmt.Println("Error executing command:", err)
		return
	}

	fmt.Println("Command output:", string(output))
}

// parsePrivateKey parses an SSH private key file
func parsePrivateKey(keyPath string) (ssh.Signer, error) {
	keyBytes, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return nil, err
	}

	privateKey, err := ssh.ParsePrivateKey(keyBytes)
	if err != nil {
		return nil, err
	}

	return privateKey, nil
}

socket5代理方式:

package main

import (
	"fmt"

	"golang.org/x/crypto/ssh"
	"golang.org/x/net/proxy"
)

func main() {
	// ... (previous code remains unchanged)
	proxyAddr := "192.168.23.1:1081"

	// 目标服务器地址
	targetAddr := "ip:22"

	// 代理服务器认证信息
	auth := &proxy.Auth{
		User:     "aaa",
		Password: "bbb",
	}

	// 创建 SOCKS5 代理客户端
	dialer, err := proxy.SOCKS5("tcp", proxyAddr, auth, proxy.Direct)
	if err != nil {
		fmt.Println("Error creating SOCKS5 proxy:", err)
		return
	}

	// 通过代理连接目标服务器
	conn, err := dialer.Dial("tcp", targetAddr)
	if err != nil {
		fmt.Println("Error connecting to target through proxy:", err)
		return
	}
	defer conn.Close()

	// 创建 SSH 客户端连接
	sshConfig := &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{
			ssh.Password("passwd"), // or use public key authentication
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Ignore host key verification (unsafe)
	}

	// Use the proxy connection for SSH Dial
	sshClientConn, chans, reqs, err := ssh.NewClientConn(conn, targetAddr, sshConfig)
	if err != nil {
		fmt.Println("Error creating SSH client connection:", err)
		return
	}
	defer sshClientConn.Close()

	sshClient := ssh.NewClient(sshClientConn, chans, reqs)

	// Now you can use the sshClient for further operations
	session, err := sshClient.NewSession()
	if err != nil {
		fmt.Println("Error creating SSH session:", err)
		return
	}
	defer session.Close()

	// 以下演示发送一个简单的命令
	output, err := session.CombinedOutput("ls -l")
	if err != nil {
		fmt.Println("Error executing command:", err)
		return
	}
	fmt.Println("Command output:", string(output))
	wait := make(chan bool)
	<-wait
}

你可能感兴趣的:(golang,ssh,开发语言)