0

I am trying to automate setting up Private Key Authentication for SSH.

Not sure how to achieve my desired result. I need to look stdin line by line into a file.

key(){
read -p "Paste Your Private Key Here: " privatekey

while read $privatekey; do
        echo $privatekey > ~/.ssh/id_rsa;
done


chmod 600 ~/.ssh/id_rsa
chown work:work ~/.ssh/id_rsa
echo "Your SSH Key Authentication is setup!"
}

Can someone assist with this?

Thanks!

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Remove the `$` in the `read` line: use `while read -r privatekey; do echo "$privatekey" >> ~/.ssh/id_rsa; done`. Note that you do use double quotes around `"$privatekey"` as you echo it, and you don't want to zap the file each time round the loop, hence the `>>`. You could also move the redirection outside the loop: while read -r privatekey; do echo "$privatekey"; done > ~/.ssh/id_rsa`. And there are many equivalent questions. – Jonathan Leffler Oct 31 '15 at 05:16
  • Hello thanks for the help! – unixnerd Oct 31 '15 at 05:19
  • So two things. The first line of the input is not included in the output file which I would like it to be. And how would I make it so that it doesnt keep hanging waiting for more input. How would I make the loop end? – unixnerd Oct 31 '15 at 05:23
  • Don't do a `read` outside the loop; that'll get everything into the file. Do indicate EOF when you're done: on Unix, type Control-D (usually; it is configurable). Or define that the user can type a blank line, or 'exit' or 'done' or whatever and test for the condition when `read` returns and before the `echo`. Use `break` to exit the loop. – Jonathan Leffler Oct 31 '15 at 05:26

0 Answers0