View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.runnables;
20  
21  import java.util.Iterator;
22  
23  import jgroup.core.ConfigurationException;
24  import jgroup.experiment.PropertyDefinition;
25  import jgroup.experiment.Runnable;
26  import jgroup.experiment.ShellCommand;
27  import jgroup.relacs.config.ExperimentConfig;
28  import jgroup.relacs.config.Host;
29  import jgroup.relacs.config.HostSet;
30  
31  /**
32   * @author Hein Meling
33   */
34  public class CheckSshAccess
35    implements Runnable 
36  {
37  
38    /* (non-Javadoc)
39     * @see jgroup.experiment.Runnable#run(jgroup.relacs.config.ExperimentConfig)
40     */
41    public void run(ExperimentConfig ec) throws ConfigurationException 
42    {
43      // get the server hosts
44      HostSet hosts = (HostSet) ec.getServerConfig().getAllHosts().clone();
45      // get the client hosts
46      HostSet clients = ec.getClientConfig().getAllHosts();
47      hosts.addHosts(clients);
48      String userName  = ec.getProperty("user.name");
49  
50      ThreadGroup threadGroup = new ThreadGroup("CheckSshAccess");
51      boolean toExit = false;
52      for (Iterator iter = hosts.iterator(); iter.hasNext();) {
53        Host host = (Host) iter.next();
54        String hostName = host.getCanonicalHostName();
55        StringBuilder b = new StringBuilder(ec.ssh(hostName));
56        b.append("echo ");
57        b.append(userName);
58        ShellCommand sc = new ShellCommand(b.toString(), threadGroup);
59        if (sc.waitForProcess() && sc.stderrModified()) {
60          /*
61           * The stderr has been modified; this may mean that we have
62           * problems connecting to the target system machines, and
63           * hence we should stop the experiment.
64           */
65          toExit = true;
66          System.out.println("Failed to connect to " + hostName);
67        }
68      }
69      ShellCommand.waitFor(threadGroup);
70      if (toExit)
71        System.exit(1);
72    }
73  
74    /* (non-Javadoc)
75     * @see jgroup.experiment.Runnable#getProperties()
76     */
77    public PropertyDefinition[] getProperties()
78    {
79      return null;
80    }
81  
82  } // END CheckSshAccess