title: How can I scp a file and run an ssh command asking for password only once?
body: <p>Here's the context of the question:</p>
  
  <p>In order for me to be able to print documents at work, I have to copy the file over to a different computer and then print from that computer. (Don't ask.  It's complicated and there is not another viable solution.)  Both of the computers are Linux and I work in bash.  The way I currently do this is I <code>scp</code> the file over to the print computer and then <code>ssh</code> in and print from command line.</p>
  
  <p>Here's what I would like to do:</p>
  
  <p>In order to make my life a bit easier, I'd like to combine these two step into one.  I could easily write a function that did both these steps, but I would have to provide my password twice.  Is there any way to combine the steps so that I only provide my password once?</p>
  
  <p><strong>Before somebody suggests it, key-based ssh-logins are not an option.  It has been specifically disabled by the Administrators for security reasons.</strong></p>
  
  <p>Solution:</p>
  
  <p>What I ended up doing was a modification of the second solution Wrikken provided.  Simply wrapping up his first suggestion in a function would have gotten the job done, but I liked the idea of being able to print multiple documents without having to type my password once per document.  I have a rather long password and I'm a lazy typist :)</p>
  
  <p>So, what I did was take a sequence of commands and wrap them up in a python script.  I used python because I wanted to parameterize the script, and I find it easiest to do in python.  I cheated and just ran bash commands from python through os.system.  Python just handled parameterization and flow control.  The logic was as follows:</p>
  
  <pre><code>if socket does not exist:
      run bash command to create socket with timeout
  copy file using the created socket
  ssh command to print using socket
  </code></pre>
  
  <p>In addition to using a timeout, I also put have an option in my python script to manually close the socket should I wish to do so.  </p>
  
  <p>If anyone wants the code, just let me know and I'll either paste-bin it or put it on my git repo.</p>