Solved

Script or command to: List a directory and cat a file on multiple servers

Posted on 2014-04-14
3
606 Views
Last Modified: 2014-04-14
I'd like to write a quick script in bash to list the contents of a file located in /home/users/thisfile.txt

For example:

From
homeserver I'd like to execute a script called: what_thisfile.sh

whats_thisfile.sh should contain

ssh user@server_fus; cat /home/users/thisfile.txt
ls
read -s -n 1 -p "Press any key"
ssh user@server_cda; cat/home /users/thisfile.txt
ls
read -s -n 1 -p "Press any key"
...etc

The read command just pauses for a moment, so I can see what the file contains.

What's the best way to do what I'm describing? ...a command line for loop would be fine, it wouldn't have to be a script file.
0
Comment
Question by:Viclyn
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39999252
Create a file named e.g "servers.txt" containing your servernames, one per line.

Example:

server_fus
server_cda
server_foo
server_bar
server_baz


Then

for S in $(<servers.txt)
 do
   ssh -tt user@$S '
      cat /home/users/thisfile.txt
      ls
       read -s -n 1 -p "Press any key"  '
done

A one-liner is possible, but really "long":

for S in $(<servers.txt);  do ssh -tt user@$S 'cat /home/users/thisfile.txt;  ls;  read -s -n 1 -p "Press any key"  ';  done

Open in new window


If you can go without "read" (sleep e.g. 1 second instead) we can make it shorter:

xargs -I{} ssh user@{} "cat /home/users/thisfile.txt;  ls; sleep1" <servers.txt
0
 

Author Comment

by:Viclyn
ID: 39999383
The "Press any key" seems to be suppressed. It still works, I was just wondering if there was a way to have it displayed during each iteration...?

But, I consider this answered, and very much appreciate the help.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39999385
Did you notice that I edited my answer to add the "-tt" flag to the ssh command?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Assess most serious Linux privilege escalation bug 17 149
Linux server cannot access samba share 12 91
Removing DES and 3DES ciphers in linux RedHat 6.8 38 95
aix unix tar error 3 42
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now