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.RCommandClient;
23  import org.apache.commons.net.examples.util.IOUtil;
24  
25  /**
26   * This is an example program demonstrating how to use the RCommandClient
27   * class. This program connects to an rshell daemon 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   * On Unix systems you will not be able to use the rshell capability
34   * unless the process runs as root since only root can bind port addresses
35   * lower than 1024.
36   * <p>
37   * Example: java rshell myhost localusername remoteusername "ps -aux"
38   * <p>
39   * Usage: rshell <hostname> <localuser> <remoteuser> <command>
40   */
41  
42  // This class requires the IOUtil support class!
43  public final class rshell
44  {
45  
46      public static void main(final String[] args)
47      {
48          final String server;
49          final String localuser;
50          final String remoteuser;
51          final String command;
52          final RCommandClient client;
53  
54          if (args.length != 4)
55          {
56              System.err.println(
57                  "Usage: rshell <hostname> <localuser> <remoteuser> <command>");
58              System.exit(1);
59              return ; // so compiler can do proper flow control analysis
60          }
61  
62          client = new RCommandClient();
63  
64          server = args[0];
65          localuser = args[1];
66          remoteuser = args[2];
67          command = args[3];
68  
69          try
70          {
71              client.connect(server);
72          }
73          catch (final IOException e)
74          {
75              System.err.println("Could not connect to server.");
76              e.printStackTrace();
77              System.exit(1);
78          }
79  
80          try
81          {
82              client.rcommand(localuser, remoteuser, command);
83          }
84          catch (final IOException e)
85          {
86              try
87              {
88                  client.disconnect();
89              }
90              catch (final IOException f)
91              {/* ignored */}
92              e.printStackTrace();
93              System.err.println("Could not execute command.");
94              System.exit(1);
95          }
96  
97  
98          IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
99                           System.in, System.out);
100 
101         try
102         {
103             client.disconnect();
104         }
105         catch (final IOException e)
106         {
107             e.printStackTrace();
108             System.exit(1);
109         }
110 
111         System.exit(0);
112     }
113 
114 }
115