2

As an experiment, I'm trying to write a script where I pipe a ZIP file into unzip on my server via ssh, however, what if I didn't have my SSH keys set up, and was prompted for a password? I'd still like it to come from stdin, not from the ZIP file - THEN I want to pipe a ZIP file into unzip, once the connection is established (maybe using two separate input pipes, one for the SSH password, one for the unzip process that ssh runs?).

Also, I'm currently writing into /tmp on the server, then deleting, because unzip doesn't seem to like /dev/stdin on my server (I'm running Arch, the server is running Ubuntu 20.04). /dev/stdin works when I pipe inside of the ssh session, it works when I pipe on my machine, but not when I pipe FROM my machine, into ssh. Any reason why?

AdminBee
  • 22,803
Liran
  • 21
  • 1
    SSH password prompt will avoid pipes by default - that is why if you want to pipe a password into the ssh command, you will need to take special steps as described in the question linked in Archemar's comment. – telcoM Sep 03 '21 at 06:07
  • As @telcoM said, ssh password is not read from a pipe; but you can use the sshpass utility to automate entering password for ssh. So it will probably work if you use sshpass to provide password and pipe for the zip file. – raj Sep 03 '21 at 11:14
  • Or you can install PuTTY for Linux; it contains the plink command that can be used as a replacement for ssh and it accepts password as parameter. – raj Sep 03 '21 at 11:17

1 Answers1

0

You seem to have two unrelated problems.

  1. How to programatically provide password into ssh.

As already noted in the comments, ssh does not read password from stdin (pipe); it reads it directly from terminal. So you cannot provide password to ssh via a pipe, but you can do it using the sshpass tool (you have to install it), like this:

sshpass -p 'password' ssh user@host

Or you can read the password from a file, instead of providing it directly on the command line:

sshpass -f passwordfile.txt ssh user@host

You can also probably (I haven't tested this) use the plink command that is a part of PuTTY for Linux instead of ssh. plink accepts password as a parameter:

plink -pw 'password' user@host
  1. unzip not accepting input from stdin.

According to text displayed by unzip -hh, unzip simply doesn't support reading the zipfile from stdin. If you want to unzip a file that you provide via stdin, you should use the funzip utility, like this:

cat file.zip | funzip >file.out

or

funzip <file.zip >file.out

However, be aware that funzip extracts only the first file from a zipfile.

So combining these two, you will have to use the following command:

cat file.zip | sshpass -p 'password' ssh user@host "funzip >file.out"

or

sshpass -p 'password' ssh user@host "funzip >file.out" <file.zip

Enclosing the command funzip >file.out in quotes causes the file file.out to be created on the remote computer, not on your local one (if you omit the quotes, the file will be created on your local machine).

raj
  • 1,123
  • 4
  • 11