ssh/scp commands with Sparrowdo

Sometimes you need to execute remote commands or copy files on/to remote hosts using ssh/scp commands. Here is how you can do it using Sparrowdo ssh/scp core-dsl functions.


Issuing ssh commands

The shortest form  to do this – call `ssh‘ function with minimum of required parameters – command to execute and remote host address.

ssh 'uptime', %( host => '' )

Usually people use ssh public-key authentication, so it is possible to set a path to ssh private key and provide user id:

ssh 'uptime', %(
  host    => '',
  user    => 'old-dog',
  ssh-key => 'keys/id_rsa'

Note, that ssh private key should be only stored at master host where sparrowdo runs, no other actions need to be taken, sparrowdo will care about coping(*) of ssh private key to the target host. It’s handy!

(*) By the way –  sparrowdo will remove a private ssh key from target host when ssh command is done.

There are many options of `ssh’ function you may read about at sparrowdo docs, here are just more examples.

You may run multi-line bash commands btw:

ssh q:to/CMD/, %( host => '', user => 'old_dog');
  set -e
  apt-get update
  DEBIAN_FRONTEND=noninteractive apt-get install -y -qq curl

Or don’t execute the same command twice relying on existence of file located at target server:

ssh "rm file", %(  host => '' , create => '/do/not/run/twice' );

And finally you may set alternative descriptions for your commands which will be shown at sparrowdo report to help you understand what your command does:

ssh "cat patch.sql | mysql", %(
  description => 'patching my database',
  host => ''

Issuing scp commands

`Scp’ command akin `ssh’ one.  Except it deals with remote files coping. Nothing to say here but showing some examples.

Copy a number of files to remote hosts

scp %( 
  data    => "/var/file1 /var/file2 /var/file3",
  host    => "", 
  user    => "Me", 
  ssh-key => "keys/id_rsa", 

Note, that files to copy should exists on the target hosts. If they don’t you may copy them from master host first using `file‘ function:

file '/var/file1', %( content =>  ( slurp 'files/file1' ) );
file '/var/file2', %( content =>  ( slurp 'files/file2' ) );
file '/var/file3', %( content =>  ( slurp 'files/file3' ) );

The same way as you do for `ssh’ command you may prevent from coping the same file twice if some file exists at target host:

scp %( 
  data    => "/var/biiiiiiig-file",
  host    => "", 
  create  => "/tmp/do/not/copy/me/twice"

And finally one my copy files FROM master to target host, using `pull’ flag:

scp %( 
  data    => "/var/data/dir",
  host    => "master-host:/var/file1", 
  pull    => 1, 
  ssh-key => "keys/id_rsa", 

That is it. Stay tuned with Sparrowdo Automation.  🙂


2 thoughts on “ssh/scp commands with Sparrowdo

  1. gnu says:

    I researched a bunch of parallel ssh/scp tools.
    But I finally realized that the best tool is GNU parallel + ssh/scp command or pure shell.
    GNU parallel also simply can manipulate several worker servers like the following..
    $ parallel -S worker1,worker2,worker3,… ssh {} command ::: server_llist…

    I don’t find any parallel ssh tool which support simple and seamless multi-server scaling mechanism like GNU parallel.

    But I think sparrowdo is also good for adequate-sized hosts. 🙂


    1. yeah, sure. the idea behind the sparrowdo way is not parallel sshing, I guess there’s a lot of dedicated tools for it. But to provide programmatic API to create remote ssh/scp scenarios using Perl6 and some useful/simple DSL.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s