user181548
user181548

Reputation:

How does "mysql" ask for a password?

If I run the command-line program mysql like

 mysql -u someone -p < sql-file

so that a password is required (-p), it prompts for a password:

 Enter password:

However, as you can see from the above, standard input is actually a file, sql-file.

How does "mysql" (or similar programs) do this?

(Please note that this is not a question about MySQL in particular, I am interested in how a C program can access the terminal like this.)

Upvotes: 4

Views: 1059

Answers (3)

jhourback
jhourback

Reputation: 4571

When you pipe in a file, it comes on stdin. Applications that read passwords usually open up a separate input stream.

The best way to accomplish your goal is to use Expect.

#!/usr/bin/expect -f
spawn mysql
expect "*password*"
send "your_password_here"

Upvotes: 0

casablanca
casablanca

Reputation: 70721

Passwords are usually read directly from the terminal. In fact, there's a function getpass that does just this.

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272647

Open /dev/tty as a file in read-mode, and then read from it.

Upvotes: 5

Related Questions