logging into server and excute commands

hi all,

I am new to unix and unix scipting. i need a script to logging into servers and to excute some commands in each server.

for eg :

I tried with below script ,but cant get the desired o/p. please help with this

for i in `cat serverlist`
do
echo $i
ssh $i uname -a ;
cat /etc/group ;
ps -ef | grep -i rp 
done 
 
#"serverlist" is the file that contains servers list

First, don't use cat like that. A more elegant way would be to write

while read i
do
    # Your stuff here
done < serverlist

Second, I guess you want to run all commands on the remote box. If so, you'll have to tell ssh to execute them. It won't automagically pick up the remaining commands because it can't divine your intentions. Try it like this:

ssh $i 'uname -a; cat /etc/group; ps -ef | grep -i rp'
1 Like

hi pludi,

thanks for ur tips.

after adding the ' ' script works fine now.

But in the while loop script, it just collects the information only from 1 server.
say i have

server1
server2
server3
server4
server5

while read $i
do
echo $i
ssh $i 'uname -a;grep -i 079464 /etc/passwd'
done <serverlist

it collects the info from server1 only
o/p:
server 1
o/p of the commands.

Sorry, forgot something there. Have ssh close all input channels by adding the -n switch. And a typo I've did: it should be while read i instead of while read $i

1 Like

thanks pludi, now its working fine :):):slight_smile: