script needed

nicky s
nicky s used Ask the Experts™
on
HI i need a script,
to execute ls command to a list of servers, i can directly ssh to the destination servers without password from one of the server...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
The sample script looks like

while read line
do

ssh ddd@server1 "echo "$line:`ls -l /app/$line/xyz.txt | awk '{print $5}'`""
done < apps.txt

do the following for X number of servers
Top Expert 2007
Commented:
If you have the list of servers in a file, you can do
#!/bin/sh
for server in $(cat /path/servers.txt)
do
   ssh $server ls
done

Open in new window

Top Expert 2011
Commented:

#!/bin/sh

for SERVER in  $(cat /path/servers.txt)
do
  while read line
  do
    ssh ddd@${SERVER} "echo "$line:`ls -l /app/$line/xyz.txt | awk '{print $5}'`""
  done < apps.txt
done

Open in new window

Kamaraj SubramanianApplication Support Analyst
Commented:
@Tintin and @wesly

i red about the useless use of cat from the google search.

Is that good to use the $(cat /path/servers.txt) in the code ?

please advise

we can use it like this right ?

while read server
do
     ssh $server "ls"
done < serverlist.txt

Open in new window

Top Expert 2011

Commented:
for your code (while loop), the server list need to be one hostname per line.

In "for SERVER in  $(cat /path/servers.txt)"
It is ok to use cat since it run only one time. The server list file can be multiple hostname separated by blank space or "newline"
Kamaraj SubramanianApplication Support Analyst

Commented:
ok, thanks wesly :)
Top Expert 2007

Commented:
The 'for' loop is not strictly a UUOC.

A better example is

cat file | grep word

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial