{"id":401,"date":"2011-10-09T00:02:02","date_gmt":"2011-10-09T00:02:02","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=401"},"modified":"2015-03-05T00:52:14","modified_gmt":"2015-03-05T00:52:14","slug":"using-ssh-to-run-commands-remotely","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/10\/using-ssh-to-run-commands-remotely\/","title":{"rendered":"using ssh to run commands remotely"},"content":{"rendered":"<p>Deployment systems like <a href=\"http:\/\/41j.com\/blog\/2011\/10\/simple-chef-example\/\">Chef<\/a> are great, but sometimes it&#8217;s useful to have an ad-hoc method for running commands on remote systems. ssh is great for this, in part because it&#8217;s already present on most systems.<\/p>\n<p>To follow these examples it&#8217;s best to have passwordless authentication setup, otherwise you&#8217;ll end up having to enter your password lots of times (you are read about this <a href=\"http:\/\/blogs.translucentcode.org\/mick\/archives\/000230.html\">here<\/a>).<\/p>\n<p>Running a command on a single remote system is trivial with ssh. You can just do the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nssh myusername@remotecomputer mycommand\r\n<\/pre>\n<p>For example, if you were trying to download an install a tar ball:<\/p>\n<pre>\r\nssh pkdick@valis.com wget http:\/\/exegensis.com\/package.tar.gz\r\nssh pkdick@valis.com tar xzvf package.tar.gz\r\nssh pkdick@valis.com tar xzvf .\/package\/install.sh\r\n<\/pre>\n<p>Which would run three commands on the remote host to download the package, untar it and run an install script.<\/p>\n<p>However with a bit of bash magic you can use ssh to run the same command on multiple systems (here valis tyrellcorp polsvr1). For example:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nfor i in valis tyrellcorp polsvr1; do\r\n  ssh pkdick@$i wget http:\/\/exegensis.com\/package.tar.gz;\r\n  ssh pkdick@$i tar xzvf package.tar.gz\r\n  ssh pkdick@$i tar xzvf .\/package\/install.sh\r\ndone\r\n<\/pre>\n<p>Which would run the same install process as above on servers called valis tyrellcorp and polsvr1.<\/p>\n<p>It&#8217;s a hacky solution compared to using a real deployment system, but it can be useful at times. Particularly for quick fixes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Deployment systems like Chef are great, but sometimes it&#8217;s useful to have an ad-hoc method for running commands on remote systems. ssh is great for this, in part because it&#8217;s already present on most systems. To follow these examples it&#8217;s best to have passwordless authentication setup, otherwise you&#8217;ll end up having to enter your password [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-6t","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":5,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"predecessor-version":[{"id":2175,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions\/2175"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}