0

I use Eclipse and I try to execute shell commands written in a cpp-file.

As far as I know, I can use the system() function to execute a shell command in cpp, but how can I respond to a prompt in the terminal?

I know that after a certain command the shell will ask for a password and I have to put it in there. At first I tried to put my password into a second system() function but it didn't work. Although I put a sleep call between both system() function calls, it doesn't work:

system(DO)// after this command the shell asks for a password
sleep(10)
system(Password)`

How can I enter the password?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Pluxyy
  • 95
  • 2
  • 11

1 Answers1

0

You can pass the password in your first system instruction. If your command is something like getPassword then it will be:

system("echo yourPassword | getPassword");

Although including the plain password in the program is dangerous.

UPDATE:

For SSH it will not work, but for common commands that get arguments from console like apt-get it will work. Using sshpass turns it back to getting password argument from stdin as normal. Another alternative is expect in Tcl that can enter password for SSH automatically and conditional.

Again, using plain text for password is not recommended at all. consider using public key authentication instead.

Community
  • 1
  • 1
Hamid Rouhani
  • 2,309
  • 2
  • 31
  • 45
  • Will not work for ssh - it takes passwords from console rather than stdin. "ps -ef" will show your password. –  Jan 04 '16 at 22:17