Reputation:
Is there a way to have full control over the terminal via networking. For example, I have a server and client written in C, I want to send a command like rm file
or cd directory
to the server (server in this case is a linux computer) and get the terminal output back to the client. I have it currently working with ls
using popen()
but it cannot change directories or do other complex commands. Is there a better way to do this, perhaps using fork()
pipe()
and other functions in order to use any command and get full output from terminal.
Upvotes: 1
Views: 345
Reputation: 98486
If you can get the output from ls
then you are on the right path.
The next thing is to be able to send the input, not very different from the output. You can use the command cat
to check if you are doing it right, both input and output at the same time.
Then, just run your favourite shell command, for example bash
, and you can write the commands to the input stream and read the output from each command.
Note that cd
is not a program; it is a shell command, so you have to run a shell for it to mean anything at all. The same is true for redirection and the other complexities it seems you want to do.
PS: Please be aware that this may be a serious security issue and that you should seriously consider an already-made alternative, such as ssh, as others have said.
Upvotes: 0
Reputation: 50220
Not sure what your exact set-up is, but why not just fork off a shell on the remote side? You can then do anything that can be done from the commandline.
If a real, interactive connection is an option, look into pty's ("pseudo-ttys"). It's how real terminal applications work.
Upvotes: 0
Reputation: 162297
What you ask for is done by SSH.
For example, I have a server and client written in C, I want to send a command like rm file or cd directory to the server (server in this case is a linux computer) and get the terminal output back to the client.
I strongly discourage to implement such a functionality yourself. The odds are high, that your own approach will have several security issues.
SSH is the de-facto standard for remote terminals, has strong encryption, real world tested authentication and strong maintenance support. Also there are excellent open source implementations, like OpenSSH.
Upvotes: 1