Pipe output from an interactive command to a smaller one

I would like to do something like

openssl enc -d -aes256 -in somefile | less 

openssl requires a password from stdin . and it gets spoiled when less involved.

Is there a way to draw output from an interactive command (e.g. openssl with a password request) and pass the output to less ?

Or is there a better method using a bash script?

+7
bash pipe openssl less-unix
source share
2 answers

Perhaps the shell script will ask for the key, then save the key in a temporary file and use the openssl -kfile option to find it. I hope your openssl version supports -kfile.

I would take care of security with this, but with a little caution, the security hole is probably smaller than you think. (But do you trust your system administrator 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 
+3
source share

You can try the following:

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

But it does not look safe.

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

 expect -c ' spawn yourscript expect "Please enter your password:" send "$PASSWORD" ' 
+1
source share

All Articles