LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A scripting question (https://www.linuxquestions.org/questions/programming-9/a-scripting-question-912701/)

geek12 11-09-2011 03:37 PM

A scripting question
 
Hi,

I am still very new to scripting and learning. I have a very basic question. How do I run commands remotely. For example, if I am on server x and want to run a set of commands on server y and z, how can I do that without having to log on to each of them. Basic commands like looking for a user in /etc/passwd file in each server etc.

Thanks.

macemoneta 11-09-2011 03:44 PM

Most Linux systems use ssh. You can pass a command for execution on a remote system, without starting a terminal session. For example:
Code:

ssh someuser@somesystem grep somebody /etc/passwd

Doc CPU 11-09-2011 03:45 PM

Hi there,

Quote:

Originally Posted by geek12 (Post 4520056)
How do I run commands remotely. For example, if I am on server x and want to run a set of commands on server y and z, how can I do that without having to log on to each of them.

you can't. It's that simple.
If you want to run commands (i.e. programs) on a unix-like machine, you have to log on to that machine - preferrably via ssh. That means, you need to have a user account on that host (which can be a generic one, of course).

[X] Doc CPU

Sergei Steshenko 11-09-2011 04:44 PM

Quote:

Originally Posted by Doc CPU (Post 4520061)
Hi there,



you can't. It's that simple.
If you want to run commands (i.e. programs) on a unix-like machine, you have to log on to that machine - preferrably via ssh. That means, you need to have a user account on that host (which can be a generic one, of course).

[X] Doc CPU

Well, maybe you can. If the remote hosts have processes monitoring requests and if there is a way to send a request to the remote hosts.

A way to send request is to, say, create or modify a file, and for that there should exist NFS mounts.

Or remote hosts can listen on a port using sockets.

geek12 11-09-2011 06:24 PM

Quote:

Originally Posted by macemoneta (Post 4520059)
Most Linux systems use ssh. You can pass a command for execution on a remote system, without starting a terminal session. For example:
Code:

ssh someuser@somesystem grep somebody /etc/passwd



Thanks. The above command worked. Now my question is how can I do routine to multiple servers, possibly with one command. I know I can probably do ssh someuser@somesystem grep somebody /etc/passwd;ssh someuser@somesystem1 grep somebody /etc/passwd ; and so on .. but if there is a quicker way to do this that would be great. For example, I can have a file with a list of systems in it and then have a for loop read thru that file, one system at a time. I dont know how to put this into code, but I thought that might work?

Thanks.

Sergei Steshenko 11-09-2011 06:46 PM

http://www.linuxquestions.org/questi...lancer-800668/

macemoneta 11-09-2011 07:16 PM

The pssh (parallel ssh) command can perform the function you're looking for. It can send the same commands to multiple systems and report or collect to files the responses. It's available in most distribution's repositories.


All times are GMT -5. The time now is 11:15 AM.