zoukankan      html  css  js  c++  java
  • golang ssh

    ...

    这版正常点

    package main
    
    import (
    	"bytes"
    	"fmt"
    	"golang.org/x/crypto/ssh"
    	"io"
    	"log"
    	"net"
    	"time"
    )
    
    type Connection struct {
    	*ssh.Client
    	password string
    }
    
    func Connect(addr, user, password string) (*Connection, error) {
    	sshConfig := &ssh.ClientConfig{
    		User: user,
    		Auth: []ssh.AuthMethod{
    			ssh.Password(password),
    		},
    		HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
    	}
    
    	conn, err := ssh.Dial("tcp", addr, sshConfig)
    	if err != nil {
    		return nil, err
    	}
    
    	return &Connection{conn, password}, nil
    
    }
    
    func (conn *Connection) SendCommands(cmds string) ([]byte, error) {
    	session, err := conn.NewSession()
    	if err != nil {
    		log.Fatal(err)
    	}
    	defer session.Close()
    
    	modes := ssh.TerminalModes{
    		ssh.ECHO:          0,     // disable echoing
    		ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
    		ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
    	}
    
    	err = session.RequestPty("xterm", 80, 40, modes)
    	if err != nil {
    		return []byte{}, err
    	}
    
    	stdoutB := new(bytes.Buffer)
    	session.Stdout = stdoutB
    	in, _ := session.StdinPipe()
    
    	go func(in io.Writer, output *bytes.Buffer) {
    		log.Println("go run func run1...")
    		in.Write([]byte("cat > 1.txt <<EOF
    "))
    		time.Sleep(time.Second)
    		in.Write([]byte("uosuos
    "))
    		time.Sleep(time.Second)
    		in.Write([]byte("uosuos
    "))
    		time.Sleep(time.Second)
    		in.Write([]byte("EOF
    "))
    		time.Sleep(time.Second)
    
    		log.Println(output.ReadByte())
    	}(in, stdoutB)
    
    
    	//go func(in io.Writer, output *bytes.Buffer) {
    	//	time.Sleep(3*time.Second)
    	//	log.Println("go run func run2...")
    	//	in.Write([]byte("pwd
    "))
    	//	time.Sleep(1*time.Second)
    	//}(in, stdoutB)
    
    	//err = session.Run(cmds)
    	session.Shell()
    	time.Sleep(4* time.Second)
    	if err != nil {
    		return []byte{}, err
    	}
    	return stdoutB.Bytes(), nil
    }
    
    func main() {
    	// ssh refers to the custom package above
    	conn, err := Connect("1.1.13.3:22", "huzh", "uosuos")
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	output, err := conn.SendCommands("sudo ls /root")
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Println("output===>",string(output),"xxx")
    
    }
    

      

    from https://stackoverflow.com/questions/44471749/golang-enter-ssh-sudo-password-on-prompt-or-exit

    golang ssh sudo 

    package main
    import (
    	"bytes"
    	"fmt"
    	"io"
    	"log"
    	"net"
    	"strings"
    
    	"golang.org/x/crypto/ssh"
    )
    
    type Connection struct {
    	*ssh.Client
    	password string
    }
    
    func Connect(addr, user, password string) (*Connection, error) {
    	sshConfig := &ssh.ClientConfig{
    		User: user,
    		Auth: []ssh.AuthMethod{
    			ssh.Password(password),
    		},
    		HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
    	}
    
    	conn, err := ssh.Dial("tcp", addr, sshConfig)
    	if err != nil {
    		return nil, err
    	}
    
    	return &Connection{conn, password}, nil
    
    }
    
    func (conn *Connection) SendCommands(cmds string) ([]byte, error) {
    	session, err := conn.NewSession()
    	if err != nil {
    		log.Fatal(err)
    	}
    	defer session.Close()
    
    	modes := ssh.TerminalModes{
    		ssh.ECHO:          0,     // disable echoing
    		ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
    		ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
    	}
    
    	err = session.RequestPty("xterm", 80, 40, modes)
    	if err != nil {
    		return []byte{}, err
    	}
    
    	stdoutB := new(bytes.Buffer)
    	session.Stdout = stdoutB
    	in, _ := session.StdinPipe()
    
    	go func(in io.Writer, output *bytes.Buffer) {
    		for {
    			if strings.Contains(string(output.Bytes()), "[sudo] password for ") {
    				_, err = in.Write([]byte(conn.password + "
    "))
    				if err != nil {
    					break
    				}
    				fmt.Println("put the password ---  end .")
    				break
    			}
    		}
    	}(in, stdoutB)
    
    	err = session.Run(cmds)
    	if err != nil {
    		return []byte{}, err
    	}
    	return stdoutB.Bytes(), nil
    }
    
    func main() {
    	// ssh refers to the custom package above
    	conn, err := Connect("1.1.13.20:22", "uos", "uosuos")
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	output, err := conn.SendCommands("sleep 3; ls /tmp")
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Println(len(string(output)))
    
    }
    

      

  • 相关阅读:
    C#基础知识系列十(集合)
    C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)
    C#基础知识系列八(const和readonly关键字)
    C#基础知识系列七(base、this、new、override、abstract、virtual、static)
    C#基础知识系列六(静态类和静态类成员)
    C#基础知识系列五(构造函数)
    C#基础知识系列四(运算符汇总)
    C#基础知识系列三(类和结构体、String和StringBuilder、equals和==)
    C#基础知识系列二(值类型和引用类型、可空类型、堆和栈、装箱和拆箱)
    C#基础知识系列一(goto、i++、三元运算符、ref和out、String和string、重载运算符)
  • 原文地址:https://www.cnblogs.com/eiguleo/p/14869471.html
Copyright © 2011-2022 走看看