3

Both at work and at home I tend to do the following very often:

$ scp "SomeScriptIJustCreatedOrModified.sh" some-server:
$ ssh some-server ./SomeScriptIJustCreatedOrModified.sh arguments

And sometimes this variation too:

$ scp "SomeScriptIJustCreatedOrModified.sh" some-server:/tmp
$ ssh some-server -t sudo -u other_user /tmp/SomeScriptIJustCreatedOrModified.sh arguments

The thing is, the server is often in a different country and the SSH handshake usually takes at least a couple seconds --- and this apparent short wait can get old quickly, as well as get in the way for long lists of servers. Not only that, I have been berated before for making suspiciously quick sucessive SSH connections.

How to accomplish this?

7
  • Make an alias in your .bashrc?
    – Adam
    Commented Aug 21, 2014 at 20:37
  • @Adam one connection, not one command.
    – Liz
    Commented Aug 21, 2014 at 20:38
  • 2
    Look into using controlmaster, it opens one connection to the server and while that connection is open any future ssh or scp connections use that same connection and do not require you to enter your credentials. puppetlabs.com/blog/speed-up-ssh-by-reusing-connections
    – Adam
    Commented Aug 21, 2014 at 20:41
  • My .ssh/config looks like this: pastebin.com/8SHV8g3k
    – Adam
    Commented Aug 21, 2014 at 20:42
  • Why don't you just stay logged in on the remote server instead of connecting every time?
    – jjlin
    Commented Aug 22, 2014 at 0:06

2 Answers 2

5

ControlMaster opens a single ssh connection, and allows future ssh and scp connections to go through the same connection, so there is no need to provide the password again, or wait for the connection to open.

Here is an example of how to setup controlmaster: Speed Up SSH by Reusing Connections.

Edit your .ssh/config file to include the lines:

Host hostname                                                                                                          
    User username                                                                                                  
    ControlMaster auto
    ControlPath ~/.ssh/%r@%h:%p
    ControlPersist 600

Where hostname and username are replaced with the appropriate information.

ControlPersist uses seconds by default, but you can use 10m for 10 minutes for example. It can also be yes, in which case there is no timeout.

0

If you have ssh access on the server, you could always use an sftp client like ExpanDrive to mount your remote home directory on your local machine. Then you can just write your script locally in a text editor and save it to the locally-mounted remote machine.

At that point, it'll be on the server, and all you have to do is ssh into it and run it. As long as you keep your sftp and ssh connections open, you can save and run as many scripts as you want, all day long, and you'd only have to connect once for sftp and once for ssh.

Just keep in mind that with this workflow, every time you save you'll be writing to remote the server, and it'll only write as fast as OS and network limitations allow (i.e., it's going to be slower than writing locally).

2
  • Thanks, but it's a good solution for a single server scenario only.
    – Liz
    Commented Aug 22, 2014 at 14:24
  • You can connect to multiple servers simultaneously in Expandrive. For ssh, you can use screen or tabs in your terminal to open multiple connections at once.
    – wbr
    Commented Aug 22, 2014 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .