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 package examples;
18
19 import java.io.IOException;
20 import org.apache.commons.net.bsd.RCommandClient;
21
22 /***
23 * This is an example program demonstrating how to use the RCommandClient
24 * class. This program connects to an rshell daemon and requests that the
25 * given command be executed on the server. It then reads input from stdin
26 * (this will be line buffered on most systems, so don't expect character
27 * at a time interactivity), passing it to the remote process and writes
28 * the process stdout and stderr to local stdout.
29 * <p>
30 * On Unix systems you will not be able to use the rshell capability
31 * unless the process runs as root since only root can bind port addresses
32 * lower than 1024.
33 * <p>
34 * Example: java rshell myhost localusername remoteusername "ps -aux"
35 * <p>
36 * Usage: rshell <hostname> <localuser> <remoteuser> <command>
37 * <p>
38 ***/
39
40 // This class requires the IOUtil support class!
41 public final class rshell
42 {
43
44 public static final void main(String[] args)
45 {
46 String server, localuser, remoteuser, command;
47 RCommandClient client;
48
49 if (args.length != 4)
50 {
51 System.err.println(
52 "Usage: rshell <hostname> <localuser> <remoteuser> <command>");
53 System.exit(1);
54 return ; // so compiler can do proper flow control analysis
55 }
56
57 client = new RCommandClient();
58
59 server = args[0];
60 localuser = args[1];
61 remoteuser = args[2];
62 command = args[3];
63
64 try
65 {
66 client.connect(server);
67 }
68 catch (IOException e)
69 {
70 System.err.println("Could not connect to server.");
71 e.printStackTrace();
72 System.exit(1);
73 }
74
75 try
76 {
77 client.rcommand(localuser, remoteuser, command);
78 }
79 catch (IOException e)
80 {
81 try
82 {
83 client.disconnect();
84 }
85 catch (IOException f)
86 {}
87 e.printStackTrace();
88 System.err.println("Could not execute command.");
89 System.exit(1);
90 }
91
92
93 IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
94 System.in, System.out);
95
96 try
97 {
98 client.disconnect();
99 }
100 catch (IOException e)
101 {
102 e.printStackTrace();
103 System.exit(1);
104 }
105
106 System.exit(0);
107 }
108
109 }
110