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 examples.unix;
19  
20  import java.io.IOException;
21  import org.apache.commons.net.bsd.RExecClient;
22  
23  import 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(String[] args)
43      {
44          String server, username, password, command;
45          RExecClient client;
46  
47          if (args.length != 4)
48          {
49              System.err.println(
50                  "Usage: rexec <hostname> <username> <password> <command>");
51              System.exit(1);
52              return ; // so compiler can do proper flow control analysis
53          }
54  
55          client = new RExecClient();
56  
57          server = args[0];
58          username = args[1];
59          password = args[2];
60          command = args[3];
61  
62          try
63          {
64              client.connect(server);
65          }
66          catch (IOException e)
67          {
68              System.err.println("Could not connect to server.");
69              e.printStackTrace();
70              System.exit(1);
71          }
72  
73          try
74          {
75              client.rexec(username, password, command);
76          }
77          catch (IOException e)
78          {
79              try
80              {
81                  client.disconnect();
82              }
83              catch (IOException f)
84              {/* ignored */}
85              e.printStackTrace();
86              System.err.println("Could not execute command.");
87              System.exit(1);
88          }
89  
90  
91          IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
92                           System.in, System.out);
93  
94          try
95          {
96              client.disconnect();
97          }
98          catch (IOException e)
99          {
100             e.printStackTrace();
101             System.exit(1);
102         }
103 
104         System.exit(0);
105     }
106 
107 }
108