當前位置: 首頁>>代碼示例>>Golang>>正文


Golang ssh.Session類代碼示例

本文整理匯總了Golang中code/google/com/p/go/crypto/ssh.Session的典型用法代碼示例。如果您正苦於以下問題:Golang Session類的具體用法?Golang Session怎麽用?Golang Session使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Session類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ExecPath

func (c *SSHConn) ExecPath(cmd, path string) (out []byte, err error) {
	var session *ssh.Session

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

	envVars := ""
	if c.env != nil {
		for name, value := range c.env {
			envVars = envVars + name + "=\"" + value + "\" "
			/*
				      TODO: This should be the proper way to set the environment, but fails for some reason
							       * Investigate why and possibly send pull-request to maintainer
										err = session.Setenv(name, value)

										if err != nil {
											log.Fatal("Failed to set environment: " + err.Error())
										}
			*/
		}
	}

	cmd = envVars + cmd

	if path != "" {
		cmd = "cd " + path + " && " + cmd
	}

	return session.CombinedOutput(cmd)
}
開發者ID:carriercomm,項目名稱:sky-1,代碼行數:33,代碼來源:ssh.go

示例2: send_script

/*
	Expected to be invoked as a goroutine which runs in parallel to sending the ssh command to the
	far side. This function reads from the input buffer reader br and writes to the target stripping
	blank and comment lines as it goes.
*/
func send_script(sess *ssh.Session, argv0 string, env_file string, br *bufio.Reader) {

	target, err := sess.StdinPipe() // we create the pipe here so that we can close here
	if err != nil {
		fmt.Fprintf(os.Stderr, "unable to create stdin for session: %s\n", err)
		return
	}
	defer target.Close()

	if argv0 != "" {
		target.Write([]byte("ARGV0=\"" + argv0 + "\"\n")) // $0 isn't valid using this, so simulate $0 with argv0
	}

	if env_file != "" { // must push out the environment first
		env_file, err = find_file(env_file) // find it in the path if not a qualified name
		if err == nil {
			ef, err := os.Open(env_file)

			if err != nil {
				fmt.Fprintf(os.Stderr, "ssh_broker: could not open environment file: %s: %s\n", env_file, err)
			} else {
				ebr := bufio.NewReader(ef) // get a buffered reader for the file
				send_file(ebr, target)
				ef.Close()
			}
		} else {
			fmt.Fprintf(os.Stderr, "ssh_broker: could not find  environment file: %s: %s\n", env_file, err)
		}
	}

	send_file(br, target)
}
開發者ID:dhanunjaya,項目名稱:gopkgs,代碼行數:37,代碼來源:ssh_broker.go

示例3: RequestAgentForwarding

// RequestAgentForwarding sets up agent forwarding for the session.
// ForwardToAgent or ForwardToRemote should be called to route
// the authentication requests.
func RequestAgentForwarding(session *ssh.Session) error {
	ok, err := session.SendRequest("[email protected]", true, nil)
	if err != nil {
		return err
	}
	if !ok {
		return errors.New("forwarding request denied")
	}
	return nil
}
開發者ID:Blystad,項目名稱:deis,代碼行數:13,代碼來源:forward.go

示例4: execCmd

func (sshTransport *SSHTransport) execCmd(session *ssh.Session, cmd string) (*bytes.Buffer, error) {
	var b bytes.Buffer
	modes := ssh.TerminalModes{
		ECHO:          0,
		TTY_OP_ISPEED: 14400,
		TTY_OP_OSPEED: 14400,
	}
	if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
		log.Fatalf("request for pseudo terminal failed: " + err.Error())
	}
	session.Stdout = &b
	if err := session.Run(cmd); err != nil {
		return nil, fmt.Errorf(b.String())
	}
	return &b, nil
}
開發者ID:rmenn,項目名稱:henchman,代碼行數:16,代碼來源:ssh.go

示例5: executeCommand

func (config *Config) executeCommand(s *ssh.Session, cmd string, sudo bool) ([]byte, error) {
	if s.Stdout != nil {
		return nil, errors.New("ssh: Stdout already set")
	}
	if s.Stderr != nil {
		return nil, errors.New("ssh: Stderr already set")
	}

	b := newSingleWriterReader()
	s.Stdout = &b
	s.Stderr = &b
	done := make(chan bool)

	if sudo {
		stdInWriter, err := s.StdinPipe()
		if err != nil {
			if config.AbortOnError == true {
				log.Fatalf("%s", err)
			}
			return nil, err
		}

		go config.injectSudoPasswordIfNecessary(done, &b, stdInWriter)
	}

	err := s.Run(cmd)
	close(done)
	return b.Bytes(), err
}
開發者ID:euforia,項目名稱:loom,代碼行數:29,代碼來源:loom.go

示例6: remoteStandardio

func remoteStandardio(
	s *ssh.Session) (io.WriteCloser, io.Reader, io.Reader, string) {

	var stdin io.WriteCloser
	var stdout io.Reader
	var stderr io.Reader
	var err error

	// plumb into standard input
	if stdin, err = s.StdinPipe(); err != nil {
		return nil, nil, nil, fmt.Sprintf("Error: %v\n", err)
	}
	// plumb into standard output
	if stdout, err = s.StdoutPipe(); err != nil {
		return nil, nil, nil, fmt.Sprintf("Error: %v\n", err)
	}
	// plumb into standard error
	if stderr, err = s.StderrPipe(); err != nil {
		return nil, nil, nil, fmt.Sprintf("Error: %v\n", err)
	}
	return stdin, stdout, stderr, ""
}
開發者ID:prataprc,項目名稱:gocbsh,代碼行數:22,代碼來源:client.go


注:本文中的code/google/com/p/go/crypto/ssh.Session類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。