logging into server and excute commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting logging into server and excute commands
# 1  
Old 12-27-2010
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

Code:
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


Last edited by pludi; 12-27-2010 at 04:12 AM..
# 2  
Old 12-27-2010
First, don't use cat like that. A more elegant way would be to write
Code:
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:
Code:
ssh $i 'uname -a; cat /etc/group; ps -ef | grep -i rp'


Last edited by pludi; 12-27-2010 at 05:58 AM..
This User Gave Thanks to pludi For This Post:
# 3  
Old 12-27-2010
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

Code:
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.

Moderator's Comments:
Mod Comment Please use [CODE] tags

Last edited by pludi; 12-27-2010 at 05:41 AM..
# 4  
Old 12-27-2010
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

Last edited by pludi; 12-27-2010 at 05:59 AM..
This User Gave Thanks to pludi For This Post:
# 5  
Old 12-28-2010
thanks pludi, now its working fine SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Logging to server to get etc/passwd file of all 300 server

i am new to scripting ,i need bash script in jump server to pull the /etc/passwd of all servers and the ssh keys are installed (3 Replies)
Discussion started by: profiles
3 Replies

2. Linux

Syslog not logging successful logging while unlocking server's console

When unlocking a Linux server's console there's no event indicating successful logging Is there a way I can fix this ? I have the following in my rsyslog.conf auth.info /var/log/secure authpriv.info /var/log/secure (1 Reply)
Discussion started by: walterthered
1 Replies

3. UNIX Desktop Questions & Answers

ssh command doesnot excute commands in the destination server

Hi All, I have the below code where Iam connecting from xzur111pap server to xzur0211pap server thru ssh to execute some commands. ssh xzur0211pap spaceleft=`df -k /home |tail -1 | awk '{print $5}'` spaceleft=${spaceleft%\%} if ]; then echo "ALERT : HUFS(/home $spaceleft)" exit 0... (3 Replies)
Discussion started by: gaddamja
3 Replies

4. Shell Programming and Scripting

logging into another server through script

Hello everybody, I have one small issue... :( When i'm trying to connect another unix box through below script.. #!/usr/bin/bash ssh $1 <<EOF Commands . . exit EOF But getting some syntax error "-sh: syntax error at line 2: `end of file' unexpected". I used to use... (2 Replies)
Discussion started by: raghu.iv85
2 Replies

5. Linux

Bought a New linux server, need help logging in

Hi, I bought a linux server, and actually all I can do now is enter through the SSH, This is my first time buying a server. When I bought a VPS before I could enter through remote desktop control, how can I do that on the server ? I need a little explanation because I am little short on info here... (1 Reply)
Discussion started by: Thehunterman
1 Replies

6. Cybersecurity

Logging shell commands and send it out

Dear friends I'm looking for a solution to log all commands that users do in my RedHat box, and send it out to other remote server, Is there any guide for that Thanks ---------- Post updated at 04:20 AM ---------- Previous update was at 03:47 AM ---------- I can think of something else I... (7 Replies)
Discussion started by: reaky
7 Replies

7. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

8. Solaris

Logging commands and output

I'm looking for a CLI utility that will capture all the commands you type at the Solaris CLI (and their output) into a file. I'm sure it's called "scripter", but I can't find anything on a command called scripter. Does anyone know of a such a command? Your help will be greatly... (3 Replies)
Discussion started by: soliberus
3 Replies

9. Shell Programming and Scripting

logging to remote server

Hi, I want to log-in to a remote server using shell script. The server requires the following while allowing a connection: username password one - letter authorisation. How can i implement this in my script? thanks, abey (6 Replies)
Discussion started by: abey
6 Replies

10. UNIX for Advanced & Expert Users

Logging all commands after a sudo su-

Hi there, It might seem tricky, I confess. We use sudo to allow people to initiate priviledged commands (but not all commands) on our Unix systems. To by pass this, some people initiate the sudo su - command ; The main issue is to 'know' what those people do when they gain root access.... (4 Replies)
Discussion started by: linuxmtl
4 Replies
Login or Register to Ask a Question