Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe output from interactive command to less

I'd like to do something like

openssl enc -d -aes256 -in somefile | less

openssl requires a password from stdin. and this gets all messed up when less is involved.

Is there a way to take the output from an interactive command (like openssl asking for a password) and pipe the output into less?

Or is there a better technique using a bash script?

like image 583
patrick h Avatar asked Aug 23 '13 02:08

patrick h


People also ask

How do you pipe the output of a command to another command?

The | command is called a pipe. It is used to pipe, or transfer, the standard output from the command on its left into the standard input of the command on its right. # First, echo "Hello World" will send Hello World to the standard output.

Which character is used to pipe output from one command to another?

The pipe character | is used to connect the output from one command to the input of another. > is used to redirect standard output to a file.

What does using the pipe to the less command do Linux?

Pressing F or G makes less try to reach input EOF. If the input is a pipe, less hangs until the pipe is closed on the other side (and not "does nothing").


2 Answers

Perhaps have the shell script ask for the key, then store the key in a temp file and use openssl's -kfile option to find it. Hope your version of openssl supports -kfile.

I'd worry about security with this, but with a little care the security hole is perhaps smaller than you might think. (But do you trust your sysadmin and sudoers...?)

#!/bin/bash

INFILE=somefile

read -s -p "Enter key for $INFILE: " key
echo

# write the key to a temp file; use mktemp(1)
# for safer creation of a privately-owned file.
#
TMPKEY=$(mktemp -t) || exit 1
echo "$key" > "$TMPKEY"

# will remove the temp file on script exit
trap 'rm -f "$TMPKEY"' EXIT

# remove the key file a couple seconds after openssl runs
(sleep 2; rm -f "$TMPKEY") &

openssl enc -d -aes256 -in "$INFILE" -kfile "$TMPKEY" | less

[ -f "$TMPKEY" ] && rm -f "$TMPKEY"
trap - EXIT

# rest of script...

exit 0
like image 198
sjnarv Avatar answered Sep 29 '22 10:09

sjnarv


You can try this:

echo 'mypassword' | openssl enc -d -aes256 -in somefile | less

But this doesn't look secure.

I have not tried running openssl this way, but in case it is too verbose and if previous code is not going to work then you can always try using expect. Here's an example:

expect -c '
    spawn yourscript
    expect "Please enter your password:"
    send "$PASSWORD"
'
like image 45
Aleks-Daniel Jakimenko-A. Avatar answered Sep 29 '22 09:09

Aleks-Daniel Jakimenko-A.