Skip to content

Instantly share code, notes, and snippets.

@tehmoon
Created March 20, 2018 01:29
Show Gist options
  • Save tehmoon/fc14b95258a82b4d9e6d9d096c1f05d0 to your computer and use it in GitHub Desktop.
Save tehmoon/fc14b95258a82b4d9e6d9d096c1f05d0 to your computer and use it in GitHub Desktop.
Reallocate stdin after injecting file/buffer to cmd
package main
import (
"os"
"os/exec"
"io"
"strings"
"github.com/kr/pty"
)
// This will be the equivalent of (echo id; cat) | /bin/sh but nicer
// because it realocates a tty after.
func main() {
buffer := strings.NewReader("id\n")
reader := io.MultiReader(buffer, os.Stdin)
cmd := exec.Command("/bin/sh")
tty, _ := pty.Start(cmd)
defer tty.Close()
go func() {
io.Copy(tty, reader)
}()
go func() {
io.Copy(os.Stdout, tty)
}()
cmd.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment