I am surprised about way of working getpass()
. I am using it in python
, however I am aware of the fact that it is about https://linux.die.net/man/3/getpass call in reality. Moreover, I am aware of the fact that ssh-add
uses it as well.
It is not easy to think about from many causes.
-
1.
The getpass() function opens /dev/tty (the controlling terminal of the process), outputs the string prompt, turns off echoing, reads one line (the "password"), restores the terminal state and closes /dev/tty again.
What does it mean? It opens new terminal? /dev/tty/
is opened before getpass
call?
What does it mean that it restore terminal state?
- Why does expect can work with
ssh-add
orgetpass
while asecho
does not?
Let's consider:
#getpass.py
import getpass
password = getpass.getpass()
print password
[user@host ~]$ python2.7 ~/getpass.py
Password:
somepassword
It works. Why? I was able to type password from keybord so I guess that it has read from stdin
. After all, the promise was about reading password /dev/tty
, not stdin
.
[user@host ~]$ python2.7 ~/getpass.py # we allow it to wait for password and let check to new bash session
[user@host ~]$ # new bash session
[user@host ~]$ python2.7 ~/getpass.py
[user@host ~]$ pgrep python
21502
[user@host ~]$ ls -al /proc/21502/fd
razem 0
dr-x------ 2 user wheel 0 04-14 17:55 .
dr-xr-xr-x 9 user wheel 0 04-14 17:53 ..
lrwx------ 1 user wheel 64 04-14 17:55 0 -> /dev/pts/6
lrwx------ 1 user wheel 64 04-14 17:55 1 -> /dev/pts/6
lrwx------ 1 user wheel 64 04-14 17:55 2 -> /dev/pts/6
lrwx------ 1 user wheel 64 04-14 17:55 3 -> /dev/tty
[user@host ~]$ echo 'some_password' > /proc/21502/fd/0 #stdin of python process
[user@host ~]$ echo 'some_password' > /proc/21502/fd/3
some_password # it prins some_password here, I know why (driver of `/dev/tty` check which process call it)
It does not work.
I have no idea how expect
tool can work (I checked it and it works). Can anyone explain it, please?