Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

linux - How does ssh receive password from tty?

I was wondering how openssh gets the password when login, cause I got stuck in automating entering passwords to the similar tools in linux which requires getting password from tty like ssh.

Tried to understand sshpass and found that sshpass forks a child process with the same pid then enters the password under the child process.

Don't know if my guess was right that ssh needs to check the right pid since I cannot stdin to the current tty using another process to enter the ssh password.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For security reasons, many programs requires a password interactively from users. Quite many programs uses the following kind of check before reading a password from stdin:

if (isatty(STDIN_FILENO) == 0)
{
    exit(EXIT_FAILURE); 
}

So the program allows password only from a terminal. That way it try to prevent non-interactive password entering.

sshpass is just a tool for:

fooling ssh into thinking it is getting the password from an interactive user. [from man page of sshpass]

For fooling ssh, sshpass creates and open a pseudo terminal, and gives that for stdin of ssh. fork() is needed because sshpass must write a password to ssh via the pseudo terminal.

This way stdin of ssh process is a terminal, and isatty test will be passed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...