Current location - Recipe Complete Network - Complete cookbook - How to use SSH in C language program
How to use SSH in C language program
1, this function is best realized by script.

2. It is best to make dsa public key authentication, that is, ssh password-free login.

3. The password input of 3.ssh is bound to TTY instead of stdin, so it cannot be redirected (to the pipeline).

3. 1. If you want to enter the ssh password through the program, you should simulate TTY or let ssh think that your input comes directly from TTY.

3.2. Alternatively, you can implement the simplest ssh protocol yourself.

3.3, program simulation TTY, generally directly use the expact library, which is very convenient in the script, but because C is too clumsy to handle strings. ...

4. After logging in, the interaction with ssh needs to be carried out through the pipeline. Write the characters typed in the simulation console and read the command output data; As for the command return value ... read echo $? Output it.

5. If the command itself has an input/output prompt, such as rm xxx will let you press Y or something, then you must capture this prompt yourself and write a "Y\n" in the pipeline. So ... maybe it's convenient to expect.

6. You can make a script, use expect to complete the login action in the script, and then issue the interact command to enter the interactive mode; Then use popen () to execute this script in C, so as to avoid the login problem, but still deal with the command line interaction problem (that is, to answer questions such as are you sure? Of course, this can be bypassed by passing in parameters such as -y for the command, but some commands may not support it).