Move Local SSH Keys to Remote User


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/bin/bash
  2. # sends your local dev machines keys to the production machine for easy deployment + ssh
  3.  
  4. if [[ ! -e ~/.ssh/id_rsa.pub ]]; then
  5. echo "Local keys do not exist, creating them..."
  6. ssh-keygen -t rsa
  7. fi
  8.  
  9. echo "username"
  10. read username
  11. echo "domain"
  12. read domain
  13.  
  14. if [[ ! -e ~/.ssh/id_rsa.pub ]]; then
  15. echo "You do not have a SSH key generated locally"
  16. exit 1
  17. fi
  18.  
  19. scp ~/.ssh/id_rsa.pub "$username"@"$domain":~/tmp_key
  20. ssh "$username"@"$domain" 'if [[ ! -e ~/.ssh/ ]]; then mkdir -m 700 ~/.ssh; fi; if [[ ! -e ~/.ssh/authorized_keys ]]; then touch ~/.ssh/authorized_keys; fi; chmod 600 ~/.ssh/authorized_keys; cat ~/tmp_key >> ~/.ssh/authorized_keys; rm ~/tmp_key'
  21.  
  22. exit 0

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.