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

Posted on 2014-04-14
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:

homeserver I'd like to execute a script called: should contain

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

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.
Question by:Viclyn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 68

Accepted Solution

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




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

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

Author Comment

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.
LVL 68

Expert Comment

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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
auto mounter on hp-ux 2 66
AWS Linux EC2 question on group 7 94
[bash] change filenames from file.log-ddmmyyyy to file.log-yyyymmdd 1 15
cmake and message 1 27
FreeBSD on EC2 FreeBSD ( is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Join Greg Farro and Ethan Banks from Packet Pushers ( and Greg Ross from Paessler ( for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

710 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