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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.