1

I am trying to catch the password prompt of sftp, but somehow it is not from STDIN or STDERR. It possibly comes from a sub-process.

The following is my test. I tried to redirect the password prompt to a file. I tried file handle number 1 through 4. None worked.

mypc:/home/myname$ sftp localhost >/tmp/junk
myname@localhost's password:

mypc:/home/myname$ sftp localhost 2>/tmp/junk
myname@localhost's password:

mypc:/home/myname$ sftp localhost 3>/tmp/junk
myname@localhost's password:

mypc:/home/myname$ sftp localhost 4>/tmp/junk
myname@localhost's password:

This is my OS

mypc:/home/myname$ uname -a
Linux mypc 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

mypc:/home/myname$ which sftp
/usr/bin/sftp

The reason I need to catch it is because I am writing a Perl script to test sftp connection. I could have used Perl's Expect module if I were allowed to install it inside my work place.

Thank you

12
  • 2
    Related: How/why does ssh output to tty when both stdout and stderr are redirected? sftp uses ssh as transport. Commented Feb 29 at 22:51
  • that must be it. I read it. it makes sense
    – oldpride
    Commented Feb 29 at 22:59
  • 1
    You should be able to install Perl modules locally under your home directory Commented Feb 29 at 23:44
  • 1
    @KamilMaciorowski could you reproduce the relevant points here so we can mark this as answered?
    – terdon
    Commented Mar 1 at 14:23
  • 1
    all, no worries. If none of you post the answer in a week, I will summarize our discussion into an answer.
    – oldpride
    Commented Mar 1 at 21:59

1 Answer 1

0

1. Why the password prompt cannot be caught by a simple direction?

  • The prompt likely comes from a child process.
  • This may be a special design so that the prompt will not be mixed with stdout and stderr which users are likely to catch.

Reference: How/why does ssh output to tty when both stdout and stderr are redirected?

2. How to catch it?

we can use Perl's Expect module. Test is shown below

myname@mypc:/mnt/c/users/myname$ perl -MExpect -e 'print ${Expect->spawn("sftp localhost")->expect(3, [qr/.+/])}[2], "\n";'
umyname@localhost's password:

myname@mypc:/mnt/c/users/myname$ perl -MExpect -e 'print ${Expect->spawn("sftp localhost")->expect(3, [qr/.+/])}[2], "\n";'  >/tmp/junk

This basically groups everything from tty into stdout.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .