{"id":426,"date":"2011-11-11T23:31:52","date_gmt":"2011-11-12T07:31:52","guid":{"rendered":"http:\/\/www.tranzoa.net\/~alex\/blog\/?p=426"},"modified":"2011-11-11T23:31:52","modified_gmt":"2011-11-12T07:31:52","slug":"rsync-and-sshpass","status":"publish","type":"post","link":"https:\/\/www.tranzoa.net\/~alex\/blog\/?p=426","title":{"rendered":"rsync and sshpass"},"content":{"rendered":"<p>After spending yet another several hours fighting the good fight, it seems like a good idea to note how to use <code>rsync<\/code> on a troublesome <code>ssh<\/code> connection &#8211; one that both requires a password and that is slooooow to get logged on.<\/p>\n<p>First, the problem of the password is solved using <code>sshpass<\/code>. It allows you to put the password for the remote system in a file. The file, we presume, is stored in your <code>~\/.ssh\/<\/code> directory and has its permissions set so that only you, the user, can see it or do anything with it. In that way, it works like an <code>ssh<\/code> key file that you might tell <code>ssh<\/code> to log in with using the &#8220;<code>ssh -i ...<\/code>&#8221; command line option.<\/p>\n<p>Second, to allow for a slow server, use something like this as an option to <code>ssh<\/code>:<\/p>\n<pre>\r\nssh -o ServerAliveInterval=45 ...\r\n<\/pre>\n<p>That is, tell <code>ssh<\/code> to wait for 45 seconds for the slow server to get you logged in.<\/p>\n<p>So the command line to get a connection to the server is something like this:<\/p>\n<pre>\r\nsshpass -f ~\/.ssh\/bonzo_password.txt       \\\r\n             ssh                           \\\r\n            -p __BONZO_SSH_PORT_22__       \\\r\n            -o ServerAliveInterval=45      \\\r\n             __BONZO_USER__@__BONZO__\r\n<\/pre>\n<p><dir><br \/>\n  <em>Excuse the horrid backslashes. They are for the browser window.<\/em><br \/>\n<\/dir><\/p>\n<p>All well and good. But that command line won&#8217;t work under <code>rsync<\/code>. The connection will be made, then instantly dropped. And <code>rsync<\/code> hangs.<\/p>\n<p><dir><br \/>\n  I presume the drop is something security-related in <code>ssh<\/code>.<br \/>\n  Or maybe <code>sshpass<\/code>.<br \/>\n  Whatever.<br \/>\n  <code>rsync<\/code> is unhappy.<br \/>\n<\/dir><\/p>\n<p>But, here&#8217;s the deal: <code>rsync<\/code> will be happy if the connection is already made in another terminal, and you have configured <code>ssh<\/code> to allow connections to go through a &#8220;master&#8221; connection &#8211; that is, tell <code>ssh<\/code> to not log in normally, but to rather use an existing <code>ssh<\/code> connection.<\/p>\n<p><dir><br \/>\nYou tell <code>ssh<\/code> to use an existing connection by putting the following in a <code>~\/.ssh\/config<\/code> file (remember to set the permissions for everything in <code>~\/.ssh<\/code> for <strong>only<\/strong> your access):<\/p>\n<pre>\r\nControlMaster       auto\r\nControlPath         \/tmp\/ssh_mux_%h_%p_%r\r\n<\/pre>\n<p><\/dir><\/p>\n<p>For clean-up reasons, you put the <code>sshpass<\/code> command in a separate <code>.sh<\/code> file which we&#8217;ll call <code>ssh_to_bonzo.sh<\/code>.<\/p>\n<p>So now you run that <code>ssh_to_bonzo.sh<\/code> script in a &#8220;SCREEN&#8221; &#8211; sort of a virtual terminal window!<\/p>\n<pre>\r\nscreen -t ssh_to_bonzo -dmS ssh_to_bonzo ssh_to_bonzo.sh\r\nsleep 45\r\n<\/pre>\n<p><dir><br \/>\n  <em>Note the you-set-it, 45 second delay.<\/em><br \/>\n<\/dir><\/p>\n<p>Once this code is run, you can <code>rsync<\/code> away, though with the world&#8217;s worst command line.<\/p>\n<p>So your whole <code>rsync<\/code> script might look something like this:<\/p>\n<pre>\r\nscreen -t ssh_to_bonzo -dmS ssh_to_bonzo ssh_to_bonzo.sh\r\nsleep 45\r\nrsync --rsh=\"sshpass -f ~\/.ssh\/bonzo_password.txt ssh -l bonzo_user_name\"     \\\r\n        bonzo:bonzo_directory                                                 \\\r\n        to_directory\r\npkill ssh_to_bonzo.sh\r\n<\/pre>\n<p>Or something of that sort.<\/p>\n<p><em>Notice the <code>pkill ssh_to_bonzo.sh<\/code> at the end. That cleans up the <code>screen<\/code> operation.<\/p>\n<p>Note, too, that you must still give the <code>sshpass<\/code> command line to the <code>rsync --rsh<\/code> option.<\/em><\/p>\n<p>Piece of cake.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After spending yet another several hours fighting the good fight, it seems like a good idea to note how to use rsync on a troublesome ssh connection &#8211; one that both requires a password and that is slooooow to get &hellip; <a href=\"https:\/\/www.tranzoa.net\/~alex\/blog\/?p=426\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,5,12],"tags":[],"class_list":["post-426","post","type-post","status-publish","format-standard","hentry","category-bloggy-things","category-programing","category-programming"],"_links":{"self":[{"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=\/wp\/v2\/posts\/426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=426"}],"version-history":[{"count":34,"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=\/wp\/v2\/posts\/426\/revisions\/460"}],"wp:attachment":[{"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tranzoa.net\/~alex\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}