{"id":74,"date":"2010-01-26T14:00:53","date_gmt":"2010-01-26T21:00:53","guid":{"rendered":"http:\/\/www.imaginarybillboards.com\/?p=74"},"modified":"2010-01-27T19:09:27","modified_gmt":"2010-01-28T02:09:27","slug":"lazy-automatic-ssh-key-distribution","status":"publish","type":"post","link":"http:\/\/www.imaginarybillboards.com\/?p=74","title":{"rendered":"Lazy automatic ssh + key distribution"},"content":{"rendered":"

I want to ssh to hosts, sometimes as a user, sometimes as root. \u00c2\u00a0I also want to distribute my public ssh key so I don’t have to login anymore. \u00c2\u00a0I want to do it without stacking tons of my keys onto the ends of files, and I want to be lazy about it. \u00c2\u00a0This is the script I use, I put it somewhere in my path as “go” with chmod +x so it’s executable. \u00c2\u00a0I can then use it like “go hostname” or “go chris@somehost”.<\/p>\n

#!\/usr\/bin\/env  bash\r\n#this will copy our public key to a remote host and ssh to it.\r\n\r\nuserhost=\r\nkeyfile=~\/.ssh\/id_rsa.pub\r\nauthkeyfile='~\/.ssh\/authorized_keys'\r\n\r\n#if no username is passed (like someuser@somehost), use root by default\r\nif [[ ! \"$userhost\" =~ '@' ]]\r\n  then\r\n    userhost=root@\r\nfi\r\n\r\n#if no ssh public key exists, create one in the default spot\r\nif [ ! -e $keyfile ]\r\n  then\r\n    echo \"Creating SSH key in $keyfile\"\r\n    ssh-keygen -t rsa  -f $keyfile -q -N ''\r\nfi\r\n#now get the key itself into a variable\r\nmypubkey=`cat $keyfile`\r\n\r\n#this keeps it to one time needed to enter the password,\r\n#it'll create the .ssh directory with right perms, touch the key file,\r\n#create a backup without our key (no dupes),\r\n#and copy it back\r\nssh $userhost \"mkdir -p .ssh;\r\n  chmod 700 .ssh;\r\n  touch $authkeyfile;\r\n  cp $authkeyfile ${authkeyfile}.bak;\r\n  grep -v '$mypubkey' ${authkeyfile}.bak > $authkeyfile;\r\n  echo '$mypubkey' >> $authkeyfile\"\r\n\r\n#and finally, ssh to the host.\r\nssh $userhost<\/pre>\n","protected":false},"excerpt":{"rendered":"

I want to ssh to hosts, sometimes as a user, sometimes as root. \u00c2\u00a0I also want to distribute my public ssh key so I don’t have to login anymore. \u00c2\u00a0I want to do it without stacking tons of my keys onto the ends of files, and I want to be lazy about it. \u00c2\u00a0This is […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[21,20,33],"_links":{"self":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/74"}],"collection":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":8,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":84,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions\/84"}],"wp:attachment":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}