Softpanorama

May the source be with you, but remember the KISS principle ;-)
Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

Slurping: copying a file from multiple hosts

News Parallel command execution Recommended Links pslurp -- parallel slurp pssh parallel File distiribution xargs
Unix Configuration Management Tools rsync Cluster SSH Mussh dsh SSH Power Tool Tentakel Multi Remote Tools
SSH Usage in Pipes Password-less SSH login scp sftp Tips History Humor Etc

Then you administer several dozens of boxes, similar configuration files are scattered on multiple hosts.  Slurping is a useful technique of getting all those files into a single location where you can edit them and then push them back. It is a simple Unix Configuration Management Tools technique that is often more productive then complex packages like puppet.

It is very simple to implement with classic Unix tools such as  and xargs and scpIt can help to work with "sysadmin hostile", idiosyncratic flavors of Unix such as AIX and HP-UX, as in this case you can use Linux or Solaris as your platform.

Generally the term "slurping" has two meanings:

Here we will use the second meaning of this term, collecting files from multiple hosts into a single directory.

There are two approaches to placing such a file in a common directory called "potlack"

One interesting possibility is to replace files that should be identical on several hosts with a master file and soft links to each of the "slave" locations. In this case you can edit a single file and then distribute all the files in a group in one operation. 

Opposite to slurping operation is called file distribution.


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[May 02, 2013] Automating ssh and scp across multiple hosts

From comments: "Please take a look at: http://sourceforge.net/projects/mpssh/ My team and I use this to administer more than 350 Linux servers. I may say that mpssh just works. Never had a problem with it."

If you're like me you'll run Debian GNU/Linux upon a number of hosts and at times you'd like to run a command or two upon all of those hosts. There are several ways you can accomplish this, ranging from manually connecting to each host in turn, to the more complex solutions such as CFEngine or Puppet. Midway between the two you can use pssh to run commands upon multiple hosts.

The pssh package is one of a number of tools which allows you to perform SSH connections in parallel across a number of machines.

Searching the Debian package repository shows several similar solutions:

me@home:~$ apt-cache search cluster ssh
clusterssh - administer multiple ssh or rsh shells simultaneously
dish - the diligence/distributed shell for parallel sysadmin
dsh - dancer's shell, or distributed shell
kanif - cluster management and administration swiss army knife
libtaktuk2 - C bindings for taktuk
libtaktuk2-dev - C bindings for taktuk (development files)
pssh - Parallel versions of SSH-based tools
taktuk - efficient, large scale, parallel remote execution of commands

Of the tools I've only used pssh, so I'm unable to compare and contrast the alternatives. Still if you're in the position where you've got SSH access secured via private keys and wish to run multiple commands remotely, or copy files, then it may be ideal for you.

Once installed the pssh package installs a number of new commands:

parallel-slurp
This command allows you to copy files from multipl remote hosts to the local system. We'll demonstrate the usage shortly.
parallel-ssh
This command allows you to run commands upon a number of systems in parallel. We'll also demonstrate this command shortly.
parallel-nuke
This command likes you kill processes on multiple remote systems.
parallel-scp
This is the opposite of parallel-slirp and allows you to copy a file, or files, to multiple remote systems.

General Usage

Each of the new commands installed by the pssh package will expect to read a list of hostnames from a text file. This makes automated usage a little bit more straightforward, and simplifies the command-line parsing.

Running Commands On Multiple Hosts

The most basic usage is to simply run a command upon each host, and not report upon the output. For example given the file hosts.txt containing a number of hostnames we can run:

me@home:~$ parallel-ssh  -h hosts.txt uptime
[1] 18:29:35 [SUCCESS] gold.my.flat 22
[2] 18:29:35 [SUCCESS] silver.my.flat 22

This command didn't show us the output of the "uptime" command as we'd expect. To do that you need to add the "-i" (inline) flag:

me@home:~$ parallel-ssh -i -h hosts.txt uptime
[1] 18:30:29 [SUCCESS] gold.my.flat 22
 18:30:29 up 36 days,  5:21,  5 users,  load average: 0.39, 0.24, 0.23
[2] 18:30:29 [SUCCESS] silver.my.flat 22
 18:30:29 up 36 days,  4:45,  1 user,  load average: 0.04, 0.02, 0.00

For most simple commands this will be fine, but for times when you wish to actually collect the output that is possible. Simply specify an output path with "-o path" - when you do this the output for each system will be written to a file:

me@home:~$ parallel-ssh -o uptime.out -h hosts.txt uptime
[1] 18:32:30 [SUCCESS] gold.my.flat 22
[2] 18:32:30 [SUCCESS] silver.my.flat 22
me@home:~$ ls uptime.output/
gold.my.flat  silver.my.flat
me@home:~$ cat uptime.out/silver.my.flat
 18:32:30 up 36 days,  4:47,  1 user,  load average: 0.00, 0.01, 0.00

There are more options you can specify, such as the username to connect as, and the port number to use. To see these options just run parallel-ssh with no arguments.

Copying Files From Multiple Hosts

Copying files works it much the same was as the execution of commands we've just demonstrated. The only caveat is that you really do need to specify a local directory - so that the file copied from the remote host isn't repeatedly overwritten.

So, to copy the file /etc/motd from each host in our hosts.txt file to the local system we'd run this:

me@home:~$ parallel-slurp -h hosts.txt  -L local.dir /etc/motd  motd
[1] 18:39:39 [SUCCESS] gold.my.flat 22
[2] 18:39:39 [SUCCESS] silver.my.flat 22

This will give us the file /etc/motd copied to the local system with the name motd inside the directory local.dir:

me@home:~$ tree local.dir/
local.dir/
|-- gold.my.flat
|   `-- motd
`-- silver.my.flat
    `-- motd

2 directories, 2 files

As you can see there has been one level of subdirectory created for each hostname we copied from.

There are also several more options that might be useful to explore with this tool, including the -r (recursive) option which should be familiar to you from the scp command itself.

Note that parallel-slurp only copies from (multiple) systems. To copy files to to (multiple) systems you'll want to use the parallel-scp tool.

In conclusion this tool is worth exploring if you manage multiple systems and already have SSH key-based authentication setup.

Re: Automating ssh and scp across multiple hosts (alternative solution)

Posted by Anonymous (213.145.xx.xx) on Thu 5 Feb 2009 at 09:35

Please take a look at: http://sourceforge.net/projects/mpssh/

My team and I use this to administer more than 350 Linux servers. I may say that mpssh just works. Never had a problem with it.

I just tested pssh with:
parallel-ssh -p 200 --print -h ./serverlist.txt -l root date

It shown several (5-6 errors for 229 hosts) random errors like:
XXX.com: XXX.com: [184] 11:19:14 [FAILURE] XXX.com [Errno 10] No child processes

The same "date" command executed with mpssh returned the current date of each server, no errors. I tested this 3 times, same results.

Just my 2 cents.

[May 02, 2013] How to copy file w- same name from multiple hosts into one

This is probably the most primitive solution that is reinvented multiple times each year on multiple forums.
Q: How to copy file w/ same name from multiple hosts into one
This is my first time posting. Please help to correct if I do something incorrectly. Here is the problem I'm facing.

I normally run a ssh script to login into hundred of hosts and grep for log files on there. All the grep output are stored on the remote host, under ~, with the same name. Now, I do not want those files to remain on there as it can cause impact if disk space runs out. I want to copy them all into my machine and I don't know a good way to do it.

Example:
After my script to grep for log, this is what I have:

Remote host 1: ~/file1
Remote host 2: ~/file1
...
Remote host n: ~/file1

How can I copy all those file1 into my machine under either a huge file1.master that have all file1 appended to it or file1.host1....file1.hostn under my machine?

Please let me know if anything is unclear or please point me to a thread that covered this already (I searched but couldn't find one). Thanks.

A:

Hi and welcome to LQ!

Having files with the same name, especially when working with multiple machines, isn't to practical. I would suggest you include the hostnames to those files when creating them. You don't mention the code you use to grep specific log content, but have a look at this:

Code:

grep "string" file(s) > log.out.$(hostname)
The $(hostname) part expands to the hostname of that specific machine. You end up with unique files (log.out.hn1, log.out.hn2, log.out.hnn).

To get the files from the hosts I would do something like this:
- create a file that holds all the hosts that need to be accessed, one host per line,
- use a loop and scp to fetch the file

Code:

$ cat hosts
hn1
hn2
.
.
hnn

$ cat fetchit.sh
#!/bin/bash

while read HOST
do
#  scp "$HOST":~/file1.$HOST .
  scp "$HOST":~/file1 file1.$HOST
done < <(cat hosts)
The commented-out line is for the situation I described above.
The second scp line is for your current situation, as you can see it locally appends the hostname to the fetched file.

[May 02, 2013] Perl Slurp Ease

Here is a Perl idiom that allows the $text variable to be declared, and there is no need for a tightly nested block. The do block will execute <FH> in a scalar context and slurp in the file named by $text:

{ 
   local( *FH ) ;
   open( FH, $file ) or die "sudden flaming death\n"
   my $text = do { local( $/ ) ; <FH> } ;
}

... ... ...

Spewing a file is a much simpler operation than slurping. You don't have context issues to worry about and there is no efficiency problem with returning a buffer. Here is a simple burp subroutine:

        sub burp {
                my( $file_name ) = shift ;
                open( my $fh, ">$file_name" ) || 
                                 die "can't create $file_name $!" ;
                print $fh @_ ;
        }

Note that it doesn't copy the input text but passes @_ directly to print. We will look at faster variations of that later on.

Recommended Links

Softpanorama hot topic of the month

Softpanorama Recommended

pslurp -- parallel slurp

Using rdist to copy files to multiple hosts Madhav Kobal's Blog

rdist - Linux Command - Unix Command

solaris - rdist (1)

Automating ssh and scp across multiple hosts

Powershell - Copy a list of files to multiple servers and backup exisiting files

JSch SCP Files From and To Multiple Hosts using Apache Ant Task - The Helper MonkeyTool

Re scp of multiple files to multiple locations on remote host - comp.unix.shell



Etc

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit exclusivly for research and educational purposes.   If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner. 

ABUSE: IPs or network segments from which we detect a stream of probes might be blocked for no less then 90 days. Multiple types of probes increase this period.  

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least


Copyright © 1996-2016 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: February 16, 2017