Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/crypto/ssh: stuck in Wait on more than one command #44060

Closed
Dhvani248 opened this issue Feb 2, 2021 · 4 comments
Closed

x/crypto/ssh: stuck in Wait on more than one command #44060

Dhvani248 opened this issue Feb 2, 2021 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@Dhvani248
Copy link

Dhvani248 commented Feb 2, 2021

What version of Go are you using (go version)?

$ go version
go version go1.15.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/dhvani/motadata_8/PluginEngineGolang/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/dhvani/motadata_8/PluginEngineGolang"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/root/sdk/go1.15.5"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/root/sdk/go1.15.5/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build614479280=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"bytes"
	"fmt"
	"github.com/ScriptRock/crypto/ssh"
	"time"
	"log"
)

func main() {

	credential := map[string]interface{}{
		"username": "xyz",
		"password": "xyz",
		"hostname": "xyz",
		"port": "22",
	}

	config := &ssh.ClientConfig{
		User: credential["username"].(string),
		Auth: []ssh.AuthMethod{
			ssh.Password(credential["password"].(string)),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Config: ssh.Config{
			Ciphers: ssh.AllSupportedCiphers(),
		},
	}

	client, err := ssh.Dial("tcp", credential["hostname"].(string)+":"+credential["port"].(string), config)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	sess, err := client.NewSession()
	if err != nil {
		log.Fatal("Failed to create session: ", err)
	}
	defer sess.Close()

	stdin, err := sess.StdinPipe()
	if err != nil {
		log.Fatal(err)
	}

	var b bytes.Buffer
	sess.Stdout = &b
	sess.Stderr = &b

	err = sess.Shell()
	if err != nil {
		log.Fatal(err)
	}

	commands := []string{
		"cli",
		"show cli",
		"exit",
		"exit",
	}

	d := time.Now()

	for _, cmd := range commands {

		_, err = fmt.Fprintf(stdin, "%s\n", cmd)

		if err != nil {
			log.Fatal(err)
		}
	}

	err = sess.Wait()

	d = time.Now()

	fmt.Println(d)

	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(b.String())

}

What did you expect to see?

I am trying to connect to juniper device and need to enter into cli mode so It should return output of following commands.

What did you see instead?

This program get stucked while executing.

@davecheney
Copy link
Contributor

Please send SIGQUIT to your program and paste the entire output. Thank you.

@seankhliao seankhliao changed the title Program get stucked In session.wait() while executing more than one command x/crypto/ssh: stuck in Wait on more than one command Feb 2, 2021
@gopherbot gopherbot added this to the Unreleased milestone Feb 2, 2021
@seankhliao
Copy link
Member

have you tried looking at the output? eg. sess.Stdout = os.Stdout and sess.Stderr = os.Stderr

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 2, 2021
@Dhvani248
Copy link
Author

@seankhliao Thanks for your quick response. And yes i have tried this one.There is one solution which is putting sleep between execution of two different commands but i guess that is not good for the performance. I am attaching relative code for your reference.Please let me know if there is any other solution.

`package main

import (
"bytes"
"fmt"
"github.com/ScriptRock/crypto/ssh"
"time"
"log"
)

func main() {

credential := map[string]interface{}{
	"username": "xyz",
	"password": "xyz",
	"hostname": "xyz",
	"port": "22",
}

config := &ssh.ClientConfig{
	User: credential["username"].(string),
	Auth: []ssh.AuthMethod{
		ssh.Password(credential["password"].(string)),
	},
	HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	Config: ssh.Config{
		Ciphers: ssh.AllSupportedCiphers(),
	},
}

client, err := ssh.Dial("tcp", credential["hostname"].(string)+":"+credential["port"].(string), config)
if err != nil {
	log.Fatal(err)
}
defer client.Close()

sess, err := client.NewSession()
if err != nil {
	log.Fatal("Failed to create session: ", err)
}
defer sess.Close()

stdin, err := sess.StdinPipe()
if err != nil {
	log.Fatal(err)
}

var b bytes.Buffer
sess.Stdout = &b
sess.Stderr = &b

err = sess.Shell()
if err != nil {
	log.Fatal(err)
}

commands := []string{
	"cli",
	"show cli",
	"exit",
	"exit",
}

d := time.Now()

for _, cmd := range commands {

	_, err = fmt.Fprintf(stdin, "%s\n", cmd)

	time.Sleep(10000000000)

	if err != nil {
		log.Fatal(err)
	}
}

err = sess.Wait()

d = time.Now()

fmt.Println(d)

if err != nil {
	log.Fatal(err)
}
fmt.Println(b.String())

}`

@seankhliao
Copy link
Member

so you just need to wait for the previous command to complete before sending more stuff to stdin (since the previous command may choose to read stdin)

closing as it's not a bug in x/crypto/ssh

@golang golang locked and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants