How can I scp a file and run an ssh command asking for password only once?

Another option would be to just leave an ssh tunnel open.

Up vote 4 down vote favorite share g+ share fb share tw.

Here's the context of the question: 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 scp the file over to the print computer and then ssh in and print from command line. Here's what I would like to do: 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? Before somebody suggests it, key-based ssh-logins are not an option.

It has been specifically disabled by the Administrators for security reasons. Solution: 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 :) 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: 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 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. If anyone wants the code, just let me know and I'll either paste-bin it or put it on my git repo.

Linux bash ssh scp link|improve this question edited Jun 8 '11 at 3:19 asked Jun 7 '11 at 16:47karategeek683216.

3 Use ssh-agent – Daenyth Jun 7 '11 at 16:59 Ack, totally forgot. Indeed, basically, ssh-agent does mainly the same as my second answer, with less hassle. – Wrikken Jun 7 '11 at 17:03 @Daenyth From what I can tell ssh-agent will not work.

The way I understand it, ssh-agent uses public key authentication, which has been disable on the server side (print machine). – karategeek6 Jun 7 '11 at 18:40 Your admins are incompetent if they turned off key login "for security reasons". Keys are infinitely more secure than password-based login.

– Daenyth Jun 7 '11 at 18:42 @Daenyth You're preaching to the choir. I think their reasoning, however, is that they can't trust the users to keep their key safe. They have no choice but to allow passwords, and they'd rather only worry about one entry point rather than two.

I'm in a University setting, which means our workstations are only slightly better than publicly accessible. – karategeek6 Jun 7 '11 at 18:54.

Cat file. Ext | ssh user@host 'cat - > /tmp/file. Ext; do_something_with /tmp/file.

Ext;rm /tmp/file. Ext' Another option would be to just leave an ssh tunnel open: In ~/. Ssh/config: Host * ControlMaster auto ControlPath ~/.

Ssh/sockets/ssh-socket-%r-%h-%p . $ ssh -f -N -l user host (socket is now open) Subsequent ssh/scp requests will reuse the already existing tunnel.

Second solution worked like a charm. I'd like to point out to future readers, however that the third switch is lower case letter L, not the number 1. It took me a few minutes to realize you weren't telling me to use protocol 1.

I also have a two part question. First, does this just leave an open connection to the server? If it does, how can I manually close this connection?

– karategeek6 Jun 7 '11 at 18:57 Yes, it does, you can close it with ssh -S -O exit something. Yes, something, you can use the name of the server if you like, your cat's name, anything as long as there is an argument in the place of something.... – Wrikken Jun 7 '11 at 19:14 Alternatively, if you know you are only busy for a limited time, you could use ssh -f user@host sleep 10; to open the socket instead of ssh -f -N -l user host where 10 is the number of seconds it will stay open. – Wrikken Jun 7 '11 at 19:17.

Even Better: you can do it completely without password entry if you add your public key to the receiving computer's account's ~/. Ssh/authorized_keys file ...

– Wrikken Jun 7 '11 at 17:32 maybe the person asking the question didn't know about key-based ssh login... that's why I added my answer.. – Tilo Jun 9 '11 at 5:51.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions