1

I know how to get the stdout into a file using dup/dup2 system calls, but how do I get the entire output that would be normally shown on my terminal(including the prompt that says my username along with the $ symbol and the current working directory) to a file?

posixKing
  • 408
  • 1
  • 8
  • 17
  • 1
    This is hard to understand. Do you want to do this after the output has been generated, like grabbing a "screenshot" of the existing content in the terminal window? The prompt is printed before your program runs, and not printed again until the program completes, so nothing you can do in a program will grab the prompt "live", as far as I understand. – unwind Sep 01 '16 at 10:04
  • If I type a command like `ls -al|sort -r`, I want the entire `$lufork@admin:~$ ls -al|sort -r` thing to be copied to a file. I can pretty much copy the command by dup system call by redirecting the stdout to some file, but how do I get the prompt as well. – posixKing Sep 01 '16 at 10:09
  • 2
    Why? This is sounds a bit like an [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – unwind Sep 01 '16 at 10:16
  • Well I built a ssh like application wherein multiple clients can connect to a running server and run bash commands on my server. The only problem is that my prompt is not being printed at the right place. I used exec call(after establishing a socket connection with server of course) to make sure that client gets into server's bash. Once client is in, I am able to run all commands perfectly except that my prompt is being printed after the output of my commands. If I say ls, I would get it's output and then the prompt(which I should have gotten before typing in ls). – posixKing Sep 02 '16 at 00:05
  • Question should be moved to super user – mpez0 Sep 03 '16 at 23:26

3 Answers3

1

Can't comment cause of low reputation.

I would say there is no way to do that inside a code in C. Instead, you could use bash for example to redirect everything to a file, and leave the code in C as it is. In this way you have all the info you want to save: prompt, current directory, call to the program (including flags), and of course the output of the program.

Well, you can do:
-For bash prompt PS1: Echo expanded PS1 (in case you want it expanded, if not there is a simple way to do it just echong PS1)
- For executed command: https://unix.stackexchange.com/questions/169259/how-to-capture-command-line-input-into-logfile-and-execute-it-at-the-same-time
- Standard output and error output: Redirect stderr and stdout in a Bash script

And that's all you want to capture, I think.

Franks
  • 50
  • 8
  • How would I do that even with bash? – posixKing Sep 02 '16 at 00:02
  • Well, you can do: (1) bash prompt PS1: https://stackoverflow.com/questions/3451993/echo-expanded-ps1 (in case you want it expanded, if not there is a simple way to do it just echong PS1) (2) executed command: https://unix.stackexchange.com/questions/169259/how-to-capture-command-line-input-into-logfile-and-execute-it-at-the-same-time (3) Standard output and error output: https://stackoverflow.com/questions/637827/redirect-stderr-and-stdout-in-a-bash-script And that's all you want to capture, I think. – Franks Sep 03 '16 at 20:06
1

Yes you can, but this may be difficult in many details (depending on your expert level). For the shell to behave normally (I would mean exactly as in a terminal), then it needs to interact with a terminal (special system object). So you need to create a program that behave like a terminal, this what pseudo-terminals devices (/dev) are intended for. Read documentation about this to implement it but roughly, your application should behave like the user so should be connected to the slave side of the pseudo-terminal, and the shell to the master side of the pseudo-terminal. Then you can easily log real inputs made by the user and catch outputs made by the shell.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Oh thanks for your info. Do you happen to know if there are any good tutorials on building a full fledged SSH like application from the bottom up? – posixKing Sep 02 '16 at 06:27
  • This may be another question (probably unsuitable for SO :-). You may easily find functional diagram of SSH (especially authentication part), but a good way is to have a look at SSH code source (learning by reading is always a right way). – Jean-Baptiste Yunès Sep 02 '16 at 07:18
0

Look up the script command in Unix systems. If you want to capture all keyboard and std in/out for a command, use the script executable. If you want to see how it's done, look up the source.

mpez0
  • 2,815
  • 17
  • 12