View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.examples.unix;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.net.bsd.RExecClient;
23  import org.apache.commons.net.examples.util.IOUtil;
24  
25  /**
26   * This is an example program demonstrating how to use the RExecClient class. This program connects to an rexec server and requests that the given command be
27   * executed on the server. It then reads input from stdin (this will be line buffered on most systems, so don't expect character at a time interactivity),
28   * passing it to the remote process and writes the process stdout and stderr to local stdout.
29   * <p>
30   * Example: java rexec myhost myuser mypassword "ps -aux"
31   * <p>
32   * Usage: rexec <hostname> <username> <password> <command>
33   */
34  
35  // This class requires the IOUtil support class!
36  public final class rexec {
37  
38      public static void main(final String[] args) {
39  
40          if (args.length != 4) {
41              System.err.println("Usage: rexec <hostname> <user> <password> <command>");
42              System.exit(1);
43              return; // so compiler can do proper flow control analysis
44          }
45  
46          final RExecClient client = new RExecClient();
47          final String server = args[0];
48          final String user = args[1];
49          final String password = args[2];
50          final String command = args[3];
51  
52          try {
53              client.connect(server);
54          } catch (final IOException e) {
55              System.err.println("Could not connect to server.");
56              e.printStackTrace();
57              System.exit(1);
58          }
59  
60          try {
61              client.rexec(user, password, command);
62          } catch (final IOException e) {
63              try {
64                  client.disconnect();
65              } catch (final IOException f) {
66                  /* ignored */}
67              e.printStackTrace();
68              System.err.println("Could not execute command.");
69              System.exit(1);
70          }
71  
72          IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out);
73  
74          try {
75              client.disconnect();
76          } catch (final IOException e) {
77              e.printStackTrace();
78              System.exit(1);
79          }
80  
81          System.exit(0);
82      }
83  
84  }