// NewClient creates a new SFTP client on top of an already created // ssh.Session. func NewClient(s *ssh.Session) (*Client, error) { stdin, err := s.StdinPipe() if err != nil { return nil, err } stdout, err := s.StdoutPipe() if err != nil { return nil, err } stderr, err := s.StderrPipe() if err != nil { return nil, err } if err := s.RequestSubsystem("sftp"); err != nil { return nil, err } sftp := &Client{ stdin: stdin, stdout: stdout, stderr: stderr, chans: &fxpChanList{}, session: s, } if err := sftp.init(); err != nil { return nil, err } return sftp, nil }
func logSession(session *ssh.Session) { { r, err := session.StdoutPipe() if err != nil { exitf("failed to get stdoutPipe: %s", err) } go io.Copy(os.Stdout, r) } { r, err := session.StderrPipe() if err != nil { exitf("failed to get StderrPipe: %s", err) } go io.Copy(os.Stderr, r) } }
func tailFile(session *ssh.Session, file string, sudo bool, linec chan<- string) error { var command string if sudo { command = fmt.Sprintf("sudo /usr/bin/env tail -F %s", file) } else { command = fmt.Sprintf("/usr/bin/env tail -F %s", file) } var wg sync.WaitGroup errc := make(chan error, 3) consumeStream := func(r io.Reader) { defer wg.Done() scan := bufio.NewScanner(r) scan.Split(bufio.ScanLines) for scan.Scan() { linec <- scan.Text() } if err := scan.Err(); err != nil { errc <- err } } stderr, err := session.StderrPipe() if err != nil { return fmt.Errorf("opening stderr: %v", err) } stdout, err := session.StdoutPipe() if err != nil { return fmt.Errorf("opening stdout: %v", err) } wg.Add(1) go consumeStream(stderr) go consumeStream(stdout) if err := session.Start(command); err != nil { return err } wg.Add(1) go func() { if err := session.Wait(); err != nil { errc <- err } }() go func() { wg.Wait() close(errc) }() return <-errc }
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 }
func (s *sshConn) runSudoCommand(sess *ssh.Session, stdin io.Writer, cmd string) error { // Wrap the output if s.DebugEnabled() { debugOutWriter := util.NewDebugLogWriter("SSH OUT:", s.Context) if sess.Stdout != nil { sess.Stdout = io.MultiWriter(sess.Stdout, debugOutWriter) } else { sess.Stdout = debugOutWriter } debugErrWriter := util.NewDebugLogWriter("SSH ERR:", s.Context) if sess.Stderr != nil { sess.Stderr = io.MultiWriter(sess.Stderr, debugErrWriter) } else { sess.Stderr = debugErrWriter } } // We need a checker to enter the password passwordTyper := util.NewExpectListener(stdin, shell.SudoPasswordPromptMatch, s.server.SSH.Pass+"\n") if sess.Stdout == nil { sess.Stdout = passwordTyper } else { sess.Stdout = io.MultiWriter(sess.Stdout, passwordTyper) } if sess.Stderr == nil { sess.Stderr = passwordTyper } else { sess.Stderr = io.MultiWriter(sess.Stderr, passwordTyper) } if err := sess.Run("sudo -S " + cmd); err != nil { return fmt.Errorf("Error running command %v: %v", cmd, err) } return nil }
func (cmd *command) StartCommand() (error, *ssh.Session) { var ( session *ssh.Session err error ) z := cmd.zh // open ssh link if z.client == nil { if err = z.dialSSH(); err != nil { return err, nil } } // establish ssh session if session, err = z.client.NewSession(); err != nil { return err, nil } // setup env, stdin, stdout, stderr if err = prepareCommand(session, cmd); err != nil { return err, nil } // start remote command err = session.Start(cmd.Path) if err == nil { return err, session } return err, nil }
/* 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) }
func (n *SSHNode) getClientAndSession() (*ssh.Client, *ssh.Session, error) { var client *ssh.Client var s *ssh.Session var err error // Retry few times if ssh connection fails for i := 0; i < MaxSSHRetries; i++ { client, err = n.dial() if err != nil { time.Sleep(SSHRetryDelay) continue } s, err = client.NewSession() if err != nil { client.Close() time.Sleep(SSHRetryDelay) continue } modes := ssh.TerminalModes{ ssh.ECHO: 0, ssh.TTY_OP_ISPEED: 14400, ssh.TTY_OP_OSPEED: 14400, } // Request pseudo terminal if err := s.RequestPty("xterm", 40, 80, modes); err != nil { return nil, nil, fmt.Errorf("failed to get pseudo-terminal: %v", err) } return client, s, nil } return nil, nil, err }
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 }
//NewSSHClientSession creates a new ssh session instance func NewSSHClientSession(s *ssh.Session, in io.Reader) *SSHClientSession { out := new(bytes.Buffer) err := new(bytes.Buffer) s.Stdin = in s.Stdout = out s.Stderr = err return &SSHClientSession{s, in, out, err} }
func execLine(session *ssh.Session, cmd string) { out, err := session.CombinedOutput(cmd) if err != nil { // panic(err) log.Fatal(err.Error()) } fmt.Println(string(out)) }
// 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("*****@*****.**", true, nil) if err != nil { return err } if !ok { return errors.New("forwarding request denied") } return nil }
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 }
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 }
// 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)) }
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 }
// Executes a command on an SSH session struct, return an error if there is one func executeCommand(cmd string, session *ssh.Session) (string, error) { //Runs CombinedOutput, which takes cmd and returns stderr and stdout of the command out, err := session.CombinedOutput(cmd) if err != nil { return "", err } // Convert our output to a string tmpOut := string(out) tmpOut = strings.Replace(tmpOut, "\n", "<br>", -1) // Return a string version of our result return tmpOut, nil }
// 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 }
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 }
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) }
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 }
// 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)) }
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 }
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 }
func (client *SSHClient) RunCommand(cmd *SSHCommand) error { var ( session *ssh.Session err error ) if session, err = client.newSession(); err != nil { return err } defer session.Close() if err = client.prepareCommand(session, cmd); err != nil { return err } err = session.Run(cmd.Path) return err }
func (sshTransport *SSHTransport) execCmd(session *ssh.Session, cmd string) (*bytes.Buffer, error) { modes := ssh.TerminalModes{ ECHO: 0, TTY_OP_ISPEED: 14400, TTY_OP_OSPEED: 14400, } if err := session.RequestPty("xterm", 100, 80, modes); err != nil { return nil, HenchErr(err, nil, "request for psuedo terminal failed") } var stdoutbuf bytes.Buffer session.Stdout = &stdoutbuf err := session.Run(cmd) if err != nil { return nil, HenchErr(err, map[string]interface{}{ "output": string(stdoutbuf.String()), }, "While retrieving output from ssh") } return &stdoutbuf, nil }
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 }
func pipe(ch ssh.Channel, client *ssh.Client, session *ssh.Session, command string) (int, error) { targetStderr, err := session.StderrPipe() if err != nil { return -1, errors.New("fail to pipe stderr: " + err.Error()) } targetStdout, err := session.StdoutPipe() if err != nil { return -1, errors.New("fail to pipe stdout: " + err.Error()) } targetStdin, err := session.StdinPipe() if err != nil { return -1, errors.New("fail to pipe stdin: " + err.Error()) } go io.Copy(targetStdin, ch) go io.Copy(ch.Stderr(), targetStderr) go io.Copy(ch, targetStdout) err = session.Start(command) if err != nil { ch.Write([]byte("Error when starting '" + command + "': " + err.Error())) ch.Close() } err = session.Wait() if err != nil { if err, ok := err.(*ssh.ExitError); ok { return err.ExitStatus(), nil } else { return -1, errors.New("failed to wait ssh command: " + err.Error()) } } return 0, nil }
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()) }
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()) }