0

I want to pass some commands from my C program, Here is the sample program :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){

    FILE *ssh = popen("ssh [email protected]", "w");

    pid_t pid;
    pid = fork();


    if(!ssh)
    {
        fprintf(stderr, "Could not open pipe for output.\n");
    } 

    if (pid==0){

        fprintf(stdout, "Child process properly created.\n");
        fputs("user", ssh); 
        fputc('\n', ssh);   // enter key 
         _exit(0);
    }


    fprintf(stdout, "Exit from child process.\n");
    pclose(ssh); 

    return 0 ;

}

Now when I run this program, it asks for Password at command prompt something like this :

[email protected]'s password:

I want to pass password from my sample program, not from Command prompt. Could anyone please tell me how to do this in C, programmatically.

SSH is used only for example.It could be for other scenarios also.

Thanks, Yuvi

3
  • 1
    If your question is about ssh interface I suggest you ask in superuser.com. Commented Mar 16, 2012 at 10:08
  • No that it not related to sssh Commented Mar 16, 2012 at 10:13
  • No that is it not merely related to ssh, I have taken ssh as an example only. There are many cases when you fire any command using system function, then it asks for some argument. Commented Mar 16, 2012 at 10:43

3 Answers 3

2

I think the ssh command reads the password from its stdin, in which case you'll need to pipe the password into the program, and for this you can use popen. C99/POSIX example:

#include <stdio.h>

int main()
{
    FILE *ssh = popen("ssh user@host", "w");

    fputs("p4ssw0rd", ssh);
    fputc('\n', ssh); // enter key

    pclose(ssh);
}

Untested. You may have better luck using a combination of fork/exec/dup etc.

Sign up to request clarification or add additional context in comments.

7 Comments

I have updated my code, but still no luck. Cud you plz explain tad more how to achieve it, I am noob in C programming.
I have also tried all the permutation with the combination of fork/exec/dup here is the refernece link which I have followed beej.us/guide/bgipc/output/html/multipage/index.html, but still not able to achieve this.
@Yuvi: You shouldn't need to fork. What happens if you run the code above in my answer without forking?
It's asking for password just like simple system() call.
@Yuvi: OK, I found out that SSH detects whether its stdin is connected to a terminal or from a pipe, and if it is connected to a pipe then it doesn't read any data from it. If you replace ssh user@host with just cat, you will see that you pipe text into the cat command and cat will echo it to the terminal (or rather, to its stdout). SSH can't be scripted like this unfortunately, and this can be proven by using shell built-ins, e.g. echo "password" | ssh user@host doesn't work either, it still asks for the password.
|
0

This is possible but takes a lot more code. First of all, you need to use fork() instead of system() plus one of the exec*() calls to replace the child process with the command you want to run.

For details, ask here or see Unix Programming Frequently Asked Questions, specifically 1. Process Control and 1.5 How can a parent and child process communicate?

1 Comment

He also wants to "enter the password" though (I think) so this will require some file descriptor fiddling as well.
0

When it's ssh a lot simpliar to setup ssh keys so you don't have to add an password. However if you really want to do that it's very easy to do with an api named expect. I have been using that myself with good results.

1 Comment

Would you please edit your answer to add information about the api you have mentioned?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.