Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

scp

ssh

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.

like image 431
cledoux Avatar asked Jun 07 '11 16:06

cledoux


1 Answers

ssh user@host 'cat - > /tmp/file.ext; do_something_with /tmp/file.ext;rm /tmp/file.ext' < 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.

like image 132
Wrikken Avatar answered Nov 15 '22 18:11

Wrikken