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