{"id":962,"date":"2015-07-09T17:45:48","date_gmt":"2015-07-09T17:45:48","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=962"},"modified":"2015-07-14T14:20:28","modified_gmt":"2015-07-14T14:20:28","slug":"upload-ssh-key-to-multiple-servers-automatically","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/upload-ssh-key-to-multiple-servers-automatically\/","title":{"rendered":"Upload ssh key to multiple servers automatically"},"content":{"rendered":"<p>Here is a quick script I created to automate copying a ssh key to multiple remote servers.<\/p>\n<p><strong>Basic command<\/strong> &#8211; the command uses sshpass to upload the ssh key to a remote server, this allows you to execute the command and not have to enter in a password to authenticate.<\/p>\n<pre>sshpass -p password ssh-copy-id -o StrictHostKeyChecking=no admin@remotehost<\/pre>\n<p><strong>Script<\/strong><\/p>\n<pre>#!\/bin\/bash\r\n\r\nremotehosts=\"$1\"\r\nusername=\"admin\"\r\npassword=\"MyCoolPassword123\"\r\n\r\nfor host in `cat ${remotehosts}`\r\ndo\r\nsshpass -p${password} ssh-copy-id -o StrictHostKeyChecking=no ${username}@${host}\r\necho \"Uploaded key to \" ${host}\r\ndone\r\n\r\necho \"Finished!\"<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Using the script<\/strong><\/p>\n<ol>\n<li>Download <a href=\"http:\/\/www.incredigeek.com\/home\/downloads\/SSHCopy\/sshcopy.sh\">here<\/a>.<\/li>\n<li>Make it executable\n<pre>chmod +x sshcopy.sh\r\n<\/pre>\n<\/li>\n<li>Edit the script and change the username and password.<\/li>\n<li>Create a file that contains each host&#8217;s IP address or hostname.<\/li>\n<li>Run script (change hostlist.txt to your host list you created in step 3.)\n<pre>.\/sshcopy.sh hostlist.txt<\/pre>\n<\/li>\n<li>Wait for the script to finish.<\/li>\n<\/ol>\n<p>Example:<\/p>\n<pre>wget www.incredigeek.com\/home\/downloads\/SSHCopy\/sshcopy.sh\r\nchmod +x sshcopy.sh\r\nsed -i s\/admin\/bob\/g sshcopy.sh                      &lt;-- Change username - you can just manually edit the file,\r\nsed -i s\/MyCoolPassword123\/password\/g sshcopy.sh     &lt;-- Change password - it might be easier than using sed\r\necho \"192.168.1.100\" &gt;&gt; host.txt                     &lt;-- Add 192.168.1.100 to the host list\r\necho \"Bob\" &gt;&gt; host.txt                               &lt;-- Add hostname bob to host list\r\n.\/sshcopy.sh host.txt                                &lt;-- Upload ssh key to all host's in the host file i.e. \"bob\" and \"192.168.1.100\"<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a quick script I created to automate copying a ssh key to multiple remote servers. Basic command &#8211; the command uses sshpass to upload the ssh key to a remote server, this allows you to execute the command &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/upload-ssh-key-to-multiple-servers-automatically\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[244,211,245,8,173,243,221,242],"class_list":["post-962","post","type-post","status-publish","format-standard","hentry","category-command-line","category-linux","tag-automation","tag-bash","tag-bash-scripts","tag-command-line-2","tag-script","tag-scripts","tag-ssh","tag-ssh-keys"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=962"}],"version-history":[{"count":14,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/962\/revisions"}],"predecessor-version":[{"id":993,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/962\/revisions\/993"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}