Git Product home page Git Product logo

Comments (4)

progrium avatar progrium commented on August 26, 2024

from ssh.

lifb avatar lifb commented on August 26, 2024

The code follows:

package main

import (
	"net"
	"time"

	"github.com/log"
	"golang.org/x/crypto/ssh"
)

func main() {
	UserName, Password := "root", "******"
	ip_port := "**.**.**.**:22"
	PassWd := []ssh.AuthMethod{ssh.Password(Password)}
	Conf := ssh.ClientConfig{
		User:    UserName,
		Auth:    PassWd,
		Timeout: time.Duration(20) * time.Second,
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
	}
	sshClient, err := ssh.Dial("tcp", ip_port, &Conf)
	if nil != err {
		log.Error("ssh Dial failed")
		return
	}
	session, err := sshClient.NewSession()
	if nil != err {
		log.Error("sshClient NewSession failed")
		return
	}
	defer session.Close()
	err = session.Run("/ushell")
	//	err = session.Run("/ushell;passwd")
	//	output, _ := session.Output("/ushell") //the output is:
	//-> Please input password!
	//->

	//-> Error password!!
	//-> Please input password!
	//->

	//-> Error password!!
	//-> Please input password!
	//->

	//-> Error password!!
	//-> Input password exceed 3 times ushell exit
}

from ssh.

progrium avatar progrium commented on August 26, 2024

from ssh.

belak avatar belak commented on August 26, 2024

It may not be very clear if you’re not used to working with SSH, but the Session object provided by this library is meant to be an extension of the gossh.Channel type. Because we expect things to be built on top of this, you need to tell the library what to do with IO. In this instance it means, handling the Channel.Read, Channel.Write and possibly even Channel.StderrPipe properly.

When you call session.Run(“./shell”) it’s running the command, but because Stdin isn’t set up, it’s reading from the channel and instantly seeing EOF.

Take a look at the example under https://godoc.org/golang.org/x/crypto/ssh#Channel to see how they set up Stdout. Something similar can be used for Stdin.

Good luck!

from ssh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.