title: How to script multiple ssh and scp commands to various systems
body: <p>I would like to script a sequence of commands involving multiple ssh and scp calls. On a daily basis, I find myself manually performing this task:</p>
  
  <p>From LOCAL system, ssh to SYSTEM1<br>
  mkdir /tmp/data on SYSTEM1<br>
  from SYSTEM1, ssh to SYSTEM2<br>
  mkdir /tmp/data on SYSTEM2<br>
  from SYSTEM2, SSH to SYSTEM3  </p>
  
  <p>scp files from SYSTEM3:/data to SYSTEM2:/tmp/data<br>
  exit to SYSTEM2<br>
  scp files from SYSTEM2:/data and SYSTEM2:/tmp/data to SYSTEM1:/tmp/data<br>
  rm -fr SYSTEM2:/tmp/data<br>
  exit to SYSTEM1<br>
  scp files from SYSTEM1:/data and SYSTEM1:/tmp/data to LOCAL:/data<br>
  rm -fr SYSTEM1:/tmp/data  </p>
  
  <p>I do this process at LEAST once a day and it takes approximately 5-10 minutes going between the different systems and then cleaning up afterwards. I would really like to automate this in a bash script but my amateur attempts so far have been unsuccessful. As you might suspect, the systems communication is constrained, meaning LOCAL can only see System1, System2 can only see System1 and System3, system3 can only see system2, etc. You get the idea. What is the best way to do this? Additionally, System1 is a hub for many other systems so SYSTEM2 must be indicated by the user (System3 will always have the same relative IP/hostname compared to any SYSTEM2).</p>
  
  <p>I tried just putting the commands in the proper order in a shell script and then manually typing in the passwords when prompted (which would already be a huge gain in efficiency) but either the method doesn't work or my execution of the script is wrong. Additionally, I would want to have a command line argument for the script that would take a pattern for which 'system2' to connect to, a pattern for the data to copy, and a target location for the data on the local system. </p>
  
  <p>Such as  </p>
  
  <pre><code>./grab_data system2 *05-14* ~/grabbed-data
  </code></pre>
  
  <p>I did some searching and I think my next step would be to have scripts on each system that perform the local tasks, and then execute the scripts via ssh commands from the respective remote system. Is there a better way? What commands should I look at using and what would be the general approach to this automating this sort of nested ssh and scp problem?</p>
  
  <p>I realize my description may be a bit convoluted so please ask for clarification on any area  that I did not properly describe.</p>
  
  <p>Thanks.</p>