0

I have the below shell commands to generate the ssh key and to add the public key to authorized keys. But, I want to redirect all the out put of this command to a file while I have to get the prompt generated by these commands to enter output.

ssh-keygen -f /home/$cu/.ssh/${ssh_key}_rsa -t rsa -N ''
# Add Public Key to authorized keys
ssh-copy-id -i /home/$cu/.ssh/${ssh_key}_rsa.pub $Linux_User@$Linux_Machine

Could you please provide your suggestions.

6
  • 1
    Eh? The prompt isn't on stdout at all. A well-written program (and OpenSSH certainly follows these conventions) will prompt either to stderr or direct to the TTY, not to stdout. A prompt for a password, in particular, will go direct to the TTY. Commented Aug 14, 2017 at 17:02
  • Can you reformulate the question to let someone reproduce the problem you're facing, as opposed to being written in a way that depends on an incorrect assumption? Commented Aug 14, 2017 at 17:04
  • 1
    (By the way, what's the vi tag doing here? I don't see anything in title or text that describes how an editor is involved). Commented Aug 14, 2017 at 17:04
  • @CharlesDuffy About 5 seconds of checking shows ssh-keygen prompts to stderr. Commented Aug 14, 2017 at 17:13
  • sure, thanks for inputs. reformulating the question. Commented Aug 14, 2017 at 17:14

1 Answer 1

1

The answer you are looking for is probably a command called tee which will split a stream into 2. Although you can get very complicated with it, the most common use is to write something to a file while also letting the streams still print out like normal just like what you are looking for.

ssh-keygen -f /home/$cu/.ssh/${ssh_key}_rsa -t rsa -N '' | tee -a $log_file

ssh-copy-id -i /home/$cu/.ssh/${ssh_key}_rsa.pub $Linux_User@$Linux_Machine | tee -a $log_file
Sign up to request clarification or add additional context in comments.

1 Comment

Aside: Lots of missing quotes here. That's true in the question, too, but ideally an answer should try to showcase good practices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.