Quik Tester
Quik Tester

Reputation: 357

Trying to pass sudo password directly through STDIN

This is my program, and it doesn't seem to be working. Sometimes I thought it worked with \0 or \r instead of \n, but I guess that is because I had already executed a sudo command on the terminal where I ran this program, so it didn't prompt me for password.

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

int main()
{
   FILE* pipe = popen("sudo -S ls", "w");   
   pwrite(pipe,"mypass\n");
   pclose(pipe);
   return 0;
}

Where am I going wrong? How can I fix it? Thanks.

Upvotes: 2

Views: 641

Answers (1)

Quik Tester
Quik Tester

Reputation: 357

I just found out a workaround that seems to be working. Thanks everyone, who looked into the question.

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

int main()
{
   FILE* pipe = popen("echo mypass | sudo -S ls", "w");   
   pclose(pipe);
   return 0;
}

Upvotes: 2

Related Questions