示例#1
2
func (client *SSHClient) prepareCommand(session *ssh.Session, cmd *SSHCommand) error {
	for _, env := range cmd.Env {
		variable := strings.Split(env, "=")
		if len(variable) != 2 {
			continue

		}

		if err := session.Setenv(variable[0], variable[1]); err != nil {
			return err

		}

	}

	if cmd.Stdin != nil {
		stdin, err := session.StdinPipe()
		if err != nil {
			return fmt.Errorf("Unable to setup stdin for session: %v", err)

		}
		go io.Copy(stdin, cmd.Stdin)

	}

	if cmd.Stdout != nil {
		stdout, err := session.StdoutPipe()
		if err != nil {
			return fmt.Errorf("Unable to setup stdout for session: %v", err)

		}
		go io.Copy(cmd.Stdout, stdout)

	}

	if cmd.Stderr != nil {
		stderr, err := session.StderrPipe()
		if err != nil {
			return fmt.Errorf("Unable to setup stderr for session: %v", err)

		}
		go io.Copy(cmd.Stderr, stderr)

	}

	return nil

}
示例#2
0
func prepareCommand(session *ssh.Session, cmd *command) error {
	for _, env := range cmd.Env {
		variable := strings.Split(env, "=")
		if len(variable) != 2 {
			continue
		}

		if err := session.Setenv(variable[0], variable[1]); err != nil {
			return err
		}
	}

	if cmd.Stdout == nil {
		session.Stdout = &cmd.stdout
	} else {
		session.Stdout = cmd.Stdout
	}

	if cmd.Stdin != nil {
		session.Stdin = cmd.Stdin

	}
	if cmd.Stderr == nil {
		session.Stderr = &cmd.stderr
	} else {
		session.Stderr = cmd.Stderr
	}
	return nil
}
示例#3
0
func configureSessionEnv(session *ssh.Session, env map[string]string) error {
	fmt.Println("Setting environment variables")

	for key, value := range env {
		if err := session.Setenv(key, value); err != nil {
			fmt.Printf("Unable to set environment %s = %s\n", key, value)
			return err
		}
	}

	fmt.Println("Done setting environment variables")
	return nil
}
示例#4
0
文件: ssh.go 项目: ccirello/gochatbot
func (client *clientSSH) prepareCommand(session *ssh.Session, cmd *sshCommand) error {
	for _, env := range cmd.env {
		variable := strings.Split(env, "=")
		if len(variable) != 2 {
			continue
		}

		if err := session.Setenv(variable[0], variable[1]); err != nil {
			return err
		}
	}

	return nil
}
			It("includes a default environment excluding PATH", func() {
				result, err := session.Output("/usr/bin/env")
				Expect(err).NotTo(HaveOccurred())

				Expect(result).To(ContainSubstring(fmt.Sprintf("PATH=/bin:/usr/bin")))
				Expect(result).To(ContainSubstring(fmt.Sprintf("LANG=en_US.UTF8")))
				Expect(result).To(ContainSubstring(fmt.Sprintf("TEST=FOO")))
				Expect(result).To(ContainSubstring(fmt.Sprintf("HOME=%s", os.Getenv("HOME"))))
				Expect(result).To(ContainSubstring(fmt.Sprintf("USER=%s", os.Getenv("USER"))))
			})
		})

		Context("when environment variables are requested", func() {
			Context("before starting the command", func() {
				It("runs the command with the specified environment", func() {
					err := session.Setenv("ENV1", "value1")
					Expect(err).NotTo(HaveOccurred())

					err = session.Setenv("ENV2", "value2")
					Expect(err).NotTo(HaveOccurred())

					result, err := session.Output("/usr/bin/env")
					Expect(err).NotTo(HaveOccurred())

					Expect(result).To(ContainSubstring("ENV1=value1"))
					Expect(result).To(ContainSubstring("ENV2=value2"))
				})

				It("uses the value last specified", func() {
					err := session.Setenv("ENV1", "original")
					Expect(err).NotTo(HaveOccurred())