开发者

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.)


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


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


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜