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.
27   * This program connects to an rexec server and requests that the
28   * given command be executed on the server.  It then reads input from stdin
29   * (this will be line buffered on most systems, so don't expect character
30   * at a time interactivity), passing it to the remote process and writes
31   * the process stdout and stderr to local stdout.
32   * <p>
33   * Example: java rexec myhost myusername mypassword "ps -aux"
34   * <p>
35   * Usage: rexec <hostname> <username> <password> <command>
36   */
37  
38  // This class requires the IOUtil support class!
39  public final class rexec
40  {
41  
42      public static void main(final String[] args)
43      {
44          final String server;
45          final String username;
46          final String password;
47          final String command;
48          final RExecClient client;
49  
50          if (args.length != 4)
51          {
52              System.err.println(
53                  "Usage: rexec <hostname> <username> <password> <command>");
54              System.exit(1);
55              return ; // so compiler can do proper flow control analysis
56          }
57  
58          client = new RExecClient();
59  
60          server = args[0];
61          username = args[1];
62          password = args[2];
63          command = args[3];
64  
65          try
66          {
67              client.connect(server);
68          }
69          catch (final IOException e)
70          {
71              System.err.println("Could not connect to server.");
72              e.printStackTrace();
73              System.exit(1);
74          }
75  
76          try
77          {
78              client.rexec(username, password, command);
79          }
80          catch (final IOException e)
81          {
82              try
83              {
84                  client.disconnect();
85              }
86              catch (final IOException f)
87              {/* ignored */}
88              e.printStackTrace();
89              System.err.println("Could not execute command.");
90              System.exit(1);
91          }
92  
93  
94          IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
95                           System.in, System.out);
96  
97          try
98          {
99              client.disconnect();
100         }
101         catch (final IOException e)
102         {
103             e.printStackTrace();
104             System.exit(1);
105         }
106 
107         System.exit(0);
108     }
109 
110 }
111