medmond919

Go Example #6

Mar 28th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "os"
  5.     "os/exec"
  6.   "io"
  7.     "strings"
  8.     "github.com/kr/pty"
  9. )
  10.  
  11. // This will be the equivalent of (echo id; cat) | /bin/sh but nicer
  12. // because it realocates a tty after.
  13.  
  14. func main() {
  15.     buffer := strings.NewReader("id\n")
  16.     reader := io.MultiReader(buffer, os.Stdin)
  17.  
  18.     cmd := exec.Command("/bin/sh")
  19.     tty, _ := pty.Start(cmd)
  20.     defer tty.Close()
  21.  
  22.     go func() {
  23.         io.Copy(tty, reader)
  24.     }()
  25.     go func() {
  26.         io.Copy(os.Stdout, tty)
  27.     }()
  28.  
  29.     cmd.Wait()
  30. }
Advertisement
Add Comment
Please, Sign In to add comment