39

Coming from Windows administration, I want to dig deeper in Linux (Debian). One of my burning questions I could not answer searching the web (didn't find it) is: how can I achieve the so called "one-to-many" remoting like in PowerShell for Windows?

To break it down to the basics I would say:

My view on Linux:

  • I can ssh into a server and type my command
  • I get the result. For an environment of 10 servers I would have to write a (perl/python?) script sending the command for each of them?

My experience from Windows:

  1. I type my command and with "invoke-command" I can "send" this to a bunch of servers (maybe from a textfile) to execute simultaneously and get the result back (as an object for further work).

  2. I can even establish multiple sessions, the connection is held in the background, and selectively send commands to these sessions, and remote in and out like I need.

(I heard of chef, puppet, etc. Is this something like that?)


Update 2019:
After trying a lot - I suggest Rex (see this comment below) - easy setup (effectively it just needs ssh, nothing else) and use (if you know just a little bit perl it's even better, but it's optional)
With Rex(ify) you can do adhoc command and advance it to a real configuration management (...meaning: it is a CM in first place, but nice for adhoc tasks, too) The website seams outdated, but currently (as of 01/2019) it's in active development and the IRC-Channel is also active.

With Windows' new openssh there are even more possibilities

you can try: rex -u user -p password -H 192.168.1.3 -e 'say run "hostname"'

MacMartin
  • 2,924
  • parallel ssh may do it: http://serverfault.com/questions/2533/linux-running-the-same-command-on-many-machines-at-once – rogerdpack Mar 14 '16 at 15:06
  • MobaXTerm supports "multi-execution" across many ssh sessions: http://mobaxterm.mobatek.net/features.html (scroll down). Also if your primary workstation environment is Windows it's also a pretty sweet tool for connecting to Linux boxes and much more. – Kev Mar 16 '16 at 20:52
  • with tmux: :setw synchronize-panes on ( http://unix.stackexchange.com/a/124800/161003) – MacMartin Mar 22 '16 at 23:05

7 Answers7

50

Summary

  • Ansible is a DevOps tool that is a powerful replacement for PowerShell
  • RunDeck as a graphical interface is handy
  • Some people run RunDeck+Ansible together

clusterssh

For sending remote commands to several servers, for a beginner, I would recommend clusterssh

To install clusterssh in Debian:

apt-get install clusterssh

Another clusterssh tutorial:

ClusterSSH is a Tk/Perl wrapper around standard Linux tools like XTerm and SSH. As such, it'll run on just about any POSIX-compliant OS where the libraries exist — I've run it on Linux, Solaris, and Mac OS X. It requires the Perl libraries Tk (perl-tk on Debian or Ubuntu) and X11::Protocol (libx11-protocol-perl on Debian or Ubuntu), in addition to xterm and OpenSSH.

Ansible

As for a remote framework for multiple systems administration, Ansible is a very interesting alternative to Puppet. It is more lean, and it does not need dedicated remote agents as it works over SSH (it also has been bought by RedHat)

The Playbooks are more elaborate than the command line options.

However, to start using Ansible you need a simple installation and to setup the clients list text file.

Afterwards, to run a command in all servers, it is as simple as doing:

ansible all -m command -a "uptime"

The output also is very nicely formatted and separated per rule/server, and while running it in the background can be redirected to a file and consulted later.

You can start with simple rules, and Ansible usage will get more interesting as you grow in Linux, and your infra-structure becomes larger. As such it will do so much more than PowerShell.

As an example, a very simple Playbook to upgrade Linux servers that I wrote:

---
- hosts: all
  become: yes
  gather_facts: False
  tasks:
   - name: updates a server
     apt: update_cache=yes
   - name: upgrade a server
     apt: upgrade=full

It also has many modules defined that let you easily write comprehensive policies.

Module Index - Ansible Documentation

It also has got an interesting official hub/"social" network of repositories to search for already made ansible policies by the community. Ansible Galaxy

Ansible is also widely used, and you will find lots of projects in github, like this one from myself for FreeRadius setup.

While Ansible is a free open source framework, it also has a paid web panel interface, Ansible Tower although the licensing is rather expensive.

Nowadays, after RedHat bought it, tower has also the open source version known as AWX.

As a bonus, Ansible also is capable of administering Windows servers, though I have never used it for that.

It is also capable of administering networking equipment (routers, switches, and firewall), which make it a very interesting solution as an automation turn key solution.

How to install Ansible

Rundeck

Yet again, for a remote framework easier to use, but not so potent as Ansible, I do recommend Rundeck.

It is a very powerful multi-user/login graphical interface where you can automate much of your common day-to-day tasks, and even give watered down views to sysops or helpdesk people.

When running the commands, it also gives you windows with the output broken down by server/task.

It can run multiple jobs in the background seamlessly, and allows you to see the report and output later on.

rundeck image

How to install RunDeck

Please note there are people running Ansible+RunDeck as a web interface; not all cases are appropriated for that.

It also goes without saying that using Ansible and/or RunDeck can be construed as a form or part of the infra-structure documentation, and over time allows to replicate and improve the actions/recipes/Playbooks.

Lastly, talking about a central command server, I would create one just up for the task. Actually the technical term is a jump box. 'Jump boxes' improve security, if you set them up right.

slm
  • 369,824
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 1
    Ansible is a great solution vs. some script or utility which merely broadcasts a command to multiple SSH servers. Ansible does that (and is a better fit for that purpose than Puppet or Chef), but also gives you the option of achieving your objective in a better way e.g. using a built in module to install something idempotently instead of just sending out the install command and hoping for the best. – JBentley Mar 16 '16 at 07:28
24

If you want to do it interactively, you can use terminator which allows to broadcast a command to multiple terminals.

enter image description here

See: How do I run the same linux command in more than one tab/shell simultaneously?

jlliagre
  • 61,204
12

You can also use pssh (or parallel-ssh), which is an SSH client that connects to a list of hosts and executes the command on all hosts in parallel:

$ parallel-ssh -i -H "host1 host2" uname -a
[1] 11:37:12 [SUCCESS] host2
Linux host2 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[2] 11:37:12 [SUCCESS] host1
Linux host1 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
morxa
  • 240
11

Perhaps you can achieve your goal with a simple zsh (bash) function. Let's say you have servers l61 l62 l63 and you often need to run a command on them:

function run_on_cluster()
{
    (for srv in l61 l62 l63;
    do
        echo "ssh $srv $1";
    done) | parallel --gnu
}

So, in the command line:

$ run_on_cluster "uname -r"
2.6.32-279.el6.x86_64
2.6.32-279.el6.x86_64
2.6.32-504.23.4.el6.x86_64
  • 5
    I would recommend printing the name of the servers before their output. Running the script on parallel also brings other quirks, like fumbling the output of the script - extra measures have to be taken to preserve the activity of the output. Running each task in the foreground also offers the possibility of minimal input interaction. – Rui F Ribeiro Mar 14 '16 at 09:15
3

If you're using KDE, here's another method.
- Open a Konsole window.
For each server you want to communicate with:
- open a tab
- establish ssh connection with the server
In the "Edit" menu, select "copy input" to "all tabs"

Every command you enter in that tab will be copied to all servers.
You can switch from tab to tab to look at the different results.

Note: my menu labels (in quotes) may be wrong since I do not use an english version of KDE

1

GNU parallel

It allows simple one-liners with saved settings, so you can create a profile, "prod', which will contain a server list and common switches. Then just add the command you want, and you are ready. It also can copy files to the server and automatically delete them at the end, and it's pretty convenient for even bigger scripts. Also it is very fast.

The only problem is that it has so many options ;) But the documentation also includes a lot of examples, so you can just look at them in the beginning.

alkuzad
  • 121
  • 2
    Hi, Welcome to Linux and Linux. Someone already gave a script using gnu-parallel has an example. I advise you to give a look around, more elaborate answers are appreciated, and will give you more points. – Rui F Ribeiro Mar 15 '16 at 08:27
  • 1
    @RuiFRibeiro sorry I did not notice, SO android app bugs with codeboxes and I could not see most of content (it generates blank white space). You mentioned gnu-parallel is fumbling output - it can write it in order but you have to tell it to do so :) – alkuzad Mar 15 '16 at 09:04
  • Yes, I am not familiar with it, but already suspected so, hence mentioning the answer needs improvement. Thanks. Nothing prevents you writing a better answer around it, however for commenting, I do recommend commenting in that answer. – Rui F Ribeiro Mar 15 '16 at 09:07
1

I want to mention "Rex" here, too: https://www.rexify.org/

Its an awesome tool that uses perl + ssh (user/password or key authentication) and because perl is installed on every (?!) linux system, I think it seems more natural to use (it has some similarities with Ansible & Co.)

you can run adhoc commands on multiple server at once (server1, server2, server... - after setting up authentication, or alternatively use the -p/-u parameters and -s/-S for sudo execution of the command):

$ rex -H "server[1..3]" -e 'say run "uptime"'

or create a file where you specify different "tasks" and call them from there

$ rex -f myConfigFile.rex uptime

It can be installed via repositories or even with perls cpan (or cpanm Rex) and it's initial setup is easy, see:
- https://www.rexify.org/ or
- https://www.rexify.org/docs/guides/start_using__r__ex.html and
- http://www.admin-magazine.com/Archive/2014/21/First-steps-in-IT-automation-by-Rex

MacMartin
  • 2,924