当前位置: 首页>>代码示例>>Golang>>正文


Golang Session.Close方法代码示例

本文整理汇总了Golang中golang.org/x/crypto/ssh.Session.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Session.Close方法的具体用法?Golang Session.Close怎么用?Golang Session.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在golang.org/x/crypto/ssh.Session的用法示例。


在下文中一共展示了Session.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: sshExecNative

func sshExecNative(c conf.ServerInfo, cmd string, sudo bool, log ...*logrus.Entry) (result sshResult) {
	logger := getSSHLogger(log...)

	cmd = decolateCmd(c, cmd, sudo)
	logger.Debugf("Command: %s",
		strings.Replace(maskPassword(cmd, c.Password), "\n", "", -1))

	var client *ssh.Client
	var err error
	client, err = sshConnect(c)
	defer client.Close()

	var session *ssh.Session
	if session, err = client.NewSession(); err != nil {
		logger.Errorf("Failed to new session. err: %s, c: %s",
			err,
			pp.Sprintf("%v", c))
		result.ExitStatus = 999
		return
	}
	defer session.Close()

	// http://blog.ralch.com/tutorial/golang-ssh-connection/
	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
	}
	if err = session.RequestPty("xterm", 400, 256, modes); err != nil {
		logger.Errorf("Failed to request for pseudo terminal. err: %s, c: %s",
			err,
			pp.Sprintf("%v", c))

		result.ExitStatus = 999
		return
	}

	var stdoutBuf, stderrBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	session.Stderr = &stderrBuf

	if err := session.Run(cmd); err != nil {
		if exitErr, ok := err.(*ssh.ExitError); ok {
			result.ExitStatus = exitErr.ExitStatus()
		} else {
			result.ExitStatus = 999
		}
	} else {
		result.ExitStatus = 0
	}

	result.Stdout = stdoutBuf.String()
	result.Stderr = stderrBuf.String()
	result.Host = c.Host
	result.Port = c.Port

	logger.Debugf(
		"SSH executed. cmd: %s, err: %#v, status: %d\nstdout: \n%s\nstderr: \n%s",
		maskPassword(cmd, c.Password), err, result.ExitStatus, result.Stdout, result.Stderr)

	return
}
开发者ID:Rompei,项目名称:vuls,代码行数:62,代码来源:sshutil.go

示例2: copy

func (this *Scp) copy(size int64, mode os.FileMode, fileName string, contents io.Reader, destination string) error {
	var (
		session *ssh.Session
		err     error
	)

	ParallelController[this.Server].Session <- token
	session, err = this.client.NewSession()
	if nil != err {
		return errors.New("Failed to create new session" + err.Error())
	}

	go func() {
		w, _ := session.StdinPipe()
		defer w.Close()

		fmt.Fprintf(w, "C%#o %d %s\n", mode, size, fileName)
		io.Copy(w, contents)
		fmt.Fprint(w, "\x00")
		<-ParallelController[this.Server].Session
	}()
	cmd := fmt.Sprintf("scp -t %s", destination)
	if err := session.Run(cmd); nil != err {
		return err
	}
	session.Close()
	return nil
}
开发者ID:krishna2nd,项目名称:goll,代码行数:28,代码来源:Scp.go

示例3: runToHost

func runToHost(user, pass, host, cmd string) {
	sshConfig := &ssh.ClientConfig{
		User: user,
		Auth: []ssh.AuthMethod{ssh.Password(pass)},
	}
	sshConfig.SetDefaults()

	client, err := ssh.Dial("tcp", host, sshConfig)
	if err != nil {
		// errors.Wrap(err, err.Error())
		fmt.Println(err.Error())
	}

	var session *ssh.Session

	session, err = client.NewSession()
	if err != nil {
		// errors.Wrap(err, err.Error())
		fmt.Println(err.Error())
	}
	defer session.Close()

	var stdoutBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	session.Run(cmd)

	fmt.Println(stdoutBuf.String())
}
开发者ID:grengojbo,项目名称:sw-cli,代码行数:28,代码来源:commands.go

示例4: Exec

func (ssc *SSHConfig) Exec(cmd string, stdout, stderr io.Writer, input chan string) (err tree_lib.TreeError) {
	var (
		session       *ssh.Session
		stdin         io.WriteCloser
		command_ended bool
	)
	err.From = tree_lib.FROM_SSH_EXEC
	session, err.Err = ssc.conn.NewSession()
	if !err.IsNull() {
		return
	}
	defer session.Close()

	session.Stdout = stdout
	session.Stderr = stderr

	stdin, err.Err = session.StdinPipe()
	if !err.IsNull() {
		return
	}

	err.Err = session.Start(cmd)
	if !err.IsNull() {
		return
	}
	command_ended = false
	go func() {
		for !command_ended {
			io.Copy(stdin, bytes.NewBufferString(<-input))
		}
	}()
	err.Err = session.Wait()
	command_ended = true
	return
}
开发者ID:treescale,项目名称:treescale,代码行数:35,代码来源:ssh.go

示例5: Run

func (c *command) Run(arg ...string) ([][]string, error) {

	var err error
	var cmd waitable
	var session *ssh.Session

	joinedArgs := strings.Join(arg, " ")
	c.Path = c.Command + " " + joinedArgs
	c.Env = []string{"LC_CTYPE=C", "LANG=en_US.UTF-8"}
	id := uuid.New()
	logger.Log([]string{"ID:" + id, "START", c.Path})
	if c.zh.Local {
		lcmd := c.LocalPrepare(arg...)
		err = lcmd.Start()
		cmd = lcmd
	} else {
		err, session = c.StartCommand()
		if session != nil {
			defer func() {
				session.Close()
			}()
		}
		cmd = session
	}

	logger.Log([]string{"ID:" + id, "DONE"})

	if err != nil {
		return nil, &Error{
			Err:    err,
			Debug:  strings.Join([]string{c.Command, joinedArgs}, " "),
			Stderr: c.stderr.String(),
		}
	}

	if err = cmd.Wait(); err != nil {
		return nil, &Error{
			Err:    err,
			Stderr: c.stderr.String(),
			Debug:  strings.Join([]string{c.Command, joinedArgs}, " "),
		}
	}

	// assume if you passed in something for stdout, that you know what to do with it
	if c.Stdout != nil {
		return nil, nil
	}

	lines := strings.Split(c.stdout.String(), "\n")
	//last line is always blank
	lines = lines[0 : len(lines)-1]
	output := make([][]string, len(lines))

	for i, l := range lines {
		output[i] = strings.Fields(l)
	}
	return output, err

}
开发者ID:edillmann,项目名称:go-zfs,代码行数:59,代码来源:utils.go

示例6: sshExecNative

func sshExecNative(c conf.ServerInfo, cmd string, sudo bool) (result execResult) {
	result.Servername = c.ServerName
	result.Host = c.Host
	result.Port = c.Port

	var client *ssh.Client
	var err error
	if client, err = sshConnect(c); err != nil {
		result.Error = err
		result.ExitStatus = 999
		return
	}
	defer client.Close()

	var session *ssh.Session
	if session, err = client.NewSession(); err != nil {
		result.Error = fmt.Errorf(
			"Failed to create a new session. servername: %s, err: %s",
			c.ServerName, err)
		result.ExitStatus = 999
		return
	}
	defer session.Close()

	// http://blog.ralch.com/tutorial/golang-ssh-connection/
	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
	}
	if err = session.RequestPty("xterm", 400, 256, modes); err != nil {
		result.Error = fmt.Errorf(
			"Failed to request for pseudo terminal. servername: %s, err: %s",
			c.ServerName, err)
		result.ExitStatus = 999
		return
	}

	var stdoutBuf, stderrBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	session.Stderr = &stderrBuf

	cmd = decolateCmd(c, cmd, sudo)
	if err := session.Run(cmd); err != nil {
		if exitErr, ok := err.(*ssh.ExitError); ok {
			result.ExitStatus = exitErr.ExitStatus()
		} else {
			result.ExitStatus = 999
		}
	} else {
		result.ExitStatus = 0
	}

	result.Stdout = stdoutBuf.String()
	result.Stderr = stderrBuf.String()
	result.Cmd = strings.Replace(cmd, "\n", "", -1)
	return
}
开发者ID:ymomoi,项目名称:vuls,代码行数:58,代码来源:sshutil.go

示例7: runPipe

func runPipe(user, pass, host, cmd string) {
	fmt.Println("runPipe...")
	sshConfig := &ssh.ClientConfig{
		User: user,
		Auth: []ssh.AuthMethod{ssh.Password(pass)},
	}
	sshConfig.SetDefaults()

	client, err := ssh.Dial("tcp", host, sshConfig)
	if err != nil {
		// errors.Wrap(err, err.Error())
		fmt.Println(err.Error())
	}

	var session *ssh.Session

	session, err = client.NewSession()
	if err != nil {
		// errors.Wrap(err, err.Error())
		fmt.Println(err.Error())
	}
	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
	// }

	// if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
	// 	log.Fatal(err)
	// }

	w, err := session.StdinPipe()
	if err != nil {
		panic(err)
	}
	//    r, err := session.StdoutPipe()
	//    if err != nil {
	//        panic(err)
	//    }

	var stdoutBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	// session.Run(cmd)
	err = session.Shell()
	w.Write([]byte(fmt.Sprintf("%s\n", "configure")))
	w.Write([]byte(fmt.Sprintf("%s %s\n", "set interfaces ethernet eth4 description", cmd)))
	w.Write([]byte(fmt.Sprintf("%s\n", "commit")))
	w.Write([]byte(fmt.Sprintf("%s\n", "save")))
	w.Write([]byte(fmt.Sprintf("%s\n", "exit")))
	w.Write([]byte(fmt.Sprintf("%s\n", "exit")))

	fmt.Println(stdoutBuf.String())
}
开发者ID:grengojbo,项目名称:sw-cli,代码行数:55,代码来源:commands.go

示例8: RunCommand

// RunCommand runs a shell command in a vagrant node and returns it's exit status
func (n *VagrantNode) RunCommand(cmd string) error {
	var (
		s   *ssh.Session
		err error
	)

	if s, err = n.client.NewSession(); err != nil {
		return err
	}
	defer s.Close()

	return s.Run(newCmdStrWithSource(cmd))
}
开发者ID:balajisiva,项目名称:netplugin,代码行数:14,代码来源:vagrantnode.go

示例9: RunCommandWithOutput

// RunCommandWithOutput runs a shell command in a vagrant node and returns it's
// exit status and output
func (n *VagrantNode) RunCommandWithOutput(cmd string) (string, error) {
	var (
		s   *ssh.Session
		err error
	)

	if s, err = n.client.NewSession(); err != nil {
		return "", err
	}
	defer s.Close()

	output, err := s.CombinedOutput(newCmdStrWithSource(cmd))
	return string(output), err
}
开发者ID:balajisiva,项目名称:netplugin,代码行数:16,代码来源:vagrantnode.go

示例10: RunCommand

func (client *SSHClient) RunCommand(cmd *SSHCommand) (error, interface{}) {
	rescueStdout := os.Stdout
	r, w, _ := os.Pipe()
	cmd.Stdout = w

	var (
		session *ssh.Session
		err     error
	)
	fmt.Println("Session creation")
	if session, err = client.newSession(); err != nil {
		fmt.Println("Session creation  err", err)
		return err, nil
	}
	defer session.Close()

	if err = client.prepareCommand(session, cmd); err != nil {
		return err, nil
	}

	fmt.Println("Session run", cmd.Path)
	if err = session.Run(cmd.Path); err != nil {
		fmt.Println("Session run err", err)
		return err, nil
	}
	print(cmd.Stdout)

	w.Close()

	out, _ := ioutil.ReadAll(r)
	os.Stdout = rescueStdout

	if strings.Contains(cmd.Path, "providerLib") {

		s1 := string([]byte(out))

		return err, s1
	} else if strings.Contains(cmd.Path, "build") {
		fmt.Printf("-->%s<--", string([]byte(out)))

		s1 := string([]byte(out))

		return err, s1
	}
	defer w.Close()
	defer r.Close()

	return err, nil
}
开发者ID:nerfmiester,项目名称:repofinder,代码行数:49,代码来源:repofinder.go

示例11: runCommand

func (client *clientSSH) runCommand(cmd *sshCommand) ([]byte, error) {
	var session *ssh.Session
	var err error

	if session, err = client.newSession(); err != nil {
		return []byte{}, err
	}
	defer session.Close()

	if err = client.prepareCommand(session, cmd); err != nil {
		return []byte{}, err
	}

	return session.CombinedOutput(cmd.command)
}
开发者ID:ccirello,项目名称:gochatbot,代码行数:15,代码来源:ssh.go

示例12: RunCommandAndWait

func (client *SSHClient) RunCommandAndWait(cmd *SSHCommand) ([]byte, error) {
	var (
		session *ssh.Session
		err     error
		baData  []byte
	)

	if session, err = client.newSession(); err != nil {
		return nil, err
	}
	defer session.Close()

	baData, err = session.Output(cmd.Path)
	return baData, err
}
开发者ID:jpeters71,项目名称:gtailer,代码行数:15,代码来源:sshclient.go

示例13: copy

func copy(size int64, mode os.FileMode, fileName string, contents io.Reader, destination string, session *ssh.Session) error {
	defer session.Close()
	go func() {
		w, _ := session.StdinPipe()
		defer w.Close()
		fmt.Fprintf(w, "C%#o %d %s\n", mode, size, fileName)
		io.Copy(w, contents)
		fmt.Fprint(w, "\x00")
	}()
	cmd := fmt.Sprintf("scp -t %s", destination)
	if err := session.Run(cmd); err != nil {
		return err
	}
	return nil
}
开发者ID:rosenhouse,项目名称:proctor,代码行数:15,代码来源:shell.go

示例14: RunCommandBackground

// RunCommandBackground runs a background command in a vagrant node
func (n *VagrantNode) RunCommandBackground(cmd string) (string, error) {
	var (
		s   *ssh.Session
		err error
	)

	if s, err = n.client.NewSession(); err != nil {
		return "", err
	}
	defer s.Close()

	// start and forget about the command as user asked to run in background.
	// The limitation is we/ won't know if it fails though. Not a worry right
	// now as the test will fail anyways, but might be good to find a better way.
	return "", s.Start(newCmdStrWithSource(cmd))
}
开发者ID:balajisiva,项目名称:netplugin,代码行数:17,代码来源:vagrantnode.go

示例15: RunCommand

// RunCommand runs a shell command in a vagrant node and returns it's exit status
func (n *VagrantNode) RunCommand(cmd string) error {
	var (
		s   *ssh.Session
		err error
	)

	if s, err = n.client.NewSession(); err != nil {
		return err
	}
	defer s.Close()

	if err := s.RequestPty("vt100", 80, 25, ssh.TerminalModes{}); err != nil {
		fmt.Println(err)
		return err
	}

	return s.Run(newCmdStrWithSource(cmd))
}
开发者ID:vvb,项目名称:vagrantssh,代码行数:19,代码来源:vagrantnode.go


注:本文中的golang.org/x/crypto/ssh.Session.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。