Created
March 20, 2018 01:29
-
-
Save tehmoon/fc14b95258a82b4d9e6d9d096c1f05d0 to your computer and use it in GitHub Desktop.
Reallocate stdin after injecting file/buffer to cmd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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