Linux equivalent to PowerShell's "one-to-many" remoting
Solution 1
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.
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.
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.
Solution 2
If you want to do it interactively, you can use terminator
which allows to broadcast a command to multiple terminals.
See: How do I run the same linux command in more than one tab/shell simultaneously?
Solution 3
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
Solution 4
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
Solution 5
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
Related videos on Youtube
MacMartin
Updated on September 18, 2022Comments
-
MacMartin over 1 year
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:
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).
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"'
-
Admin about 8 yearsparallel ssh may do it: serverfault.com/questions/2533/…
-
Admin about 8 yearsMobaXTerm supports "multi-execution" across many ssh sessions: 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.
-
Admin about 8 yearswith tmux:
:setw synchronize-panes on
( unix.stackexchange.com/a/124800/161003)
-
Admin about 8 yearsI 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 about 8 yearsHi, 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.
-
alkuzad about 8 years@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 :)
-
space earth about 8 yearsAnsible 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.
-
Admin about 6 yearsSame way with
tmux
and Crtl+b:set sync on