Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sometime, I need to find out specific log content(such as api call) from log files, those log files exists in several Linux servers.

I have to use SSH to login every server and use grep searching the log. Right now, I plan to write a shell to finish this job automatically. But when I tried to put the "ssh" in a shell, I execute it, it always need password input and show the message on screen.

Is there anyway to make sure there is no alert message about the password input? Or is there is anyway to support using "grep" on multiple servers?

I can't use SSH key authentication as I can do this change on production servers

Because I also plan to provide the result on web page(like JSP,PHP. ) and execute that shell by Java at backend.

share|improve this question

migrated from Jan 12 '13 at 19:06

This question came from our site for professional and enthusiast programmers.

See "ssh key authentication" – Flexo Jan 12 '13 at 12:24
providing commands which you are using will improve chances to get useful answers – udo Jan 13 '13 at 9:47

An alternative to public key authentication or kerberos authentication is to use master connections. This is typically done by adding this into the ~/.ssh/config file on the client:

Host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p

After that, if you are already connected to a server, then opening another session will not ask your password and will reuse the first connection. So if you keep open connections to all of your servers, then your script will never need any password input.

See the ControlMaster, ControlPath, ControlPersist options in ssh_config(5) or, alternatively, the -O, -S options of ssh(1).

share|improve this answer

Since you can't use public key authentication you are probably better off using the method described in the first answer to avoid having to repeat the login process. Making a script to connect to multiple servers should be relatively straightforward, though:

#!/usr/bin/env bash

hosts=(ipaddr1 ipaddr2 ipaddr3) # or host names
user=username                   # your user on host
log=/tmp/mylog                  # result of grepping

for host in ${hosts[@]}; do
    echo "== $host ==" >> $log
    ssh -l $user $host "grep some_pattern /path/to/logfile 2>&1" >> $log
less $log                       # view result

I have not tested this, but it should work. Please tell me if this is not what you need.

share|improve this answer
where does it take password here ? – Balaji Boggaram Ramanarayan Sep 16 '15 at 20:38

just use grep4j ( ) on your backend and display the results on your jsp/php

share|improve this answer
Please explain how to use the tool instead of just dropping a link. – Oliver Salzburg Mar 23 '14 at 11:25

You must log in to answer this question.