%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
examples.rshell |
|
|
1 | /* |
|
2 | * Copyright 2001-2005 The Apache Software Foundation |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package examples; |
|
17 | ||
18 | import java.io.IOException; |
|
19 | import org.apache.commons.net.bsd.RCommandClient; |
|
20 | ||
21 | /*** |
|
22 | * This is an example program demonstrating how to use the RCommandClient |
|
23 | * class. This program connects to an rshell daemon and requests that the |
|
24 | * given command be executed on the server. It then reads input from stdin |
|
25 | * (this will be line buffered on most systems, so don't expect character |
|
26 | * at a time interactivity), passing it to the remote process and writes |
|
27 | * the process stdout and stderr to local stdout. |
|
28 | * <p> |
|
29 | * On Unix systems you will not be able to use the rshell capability |
|
30 | * unless the process runs as root since only root can bind port addresses |
|
31 | * lower than 1024. |
|
32 | * <p> |
|
33 | * Example: java rshell myhost localusername remoteusername "ps -aux" |
|
34 | * <p> |
|
35 | * Usage: rshell <hostname> <localuser> <remoteuser> <command> |
|
36 | * <p> |
|
37 | ***/ |
|
38 | ||
39 | // This class requires the IOUtil support class! |
|
40 | 0 | public final class rshell |
41 | { |
|
42 | ||
43 | public static final void main(String[] args) |
|
44 | { |
|
45 | String server, localuser, remoteuser, command; |
|
46 | RCommandClient client; |
|
47 | ||
48 | 0 | if (args.length != 4) |
49 | { |
|
50 | 0 | System.err.println( |
51 | "Usage: rshell <hostname> <localuser> <remoteuser> <command>"); |
|
52 | 0 | System.exit(1); |
53 | 0 | return ; // so compiler can do proper flow control analysis |
54 | } |
|
55 | ||
56 | 0 | client = new RCommandClient(); |
57 | ||
58 | 0 | server = args[0]; |
59 | 0 | localuser = args[1]; |
60 | 0 | remoteuser = args[2]; |
61 | 0 | command = args[3]; |
62 | ||
63 | try |
|
64 | { |
|
65 | 0 | client.connect(server); |
66 | } |
|
67 | 0 | catch (IOException e) |
68 | { |
|
69 | 0 | System.err.println("Could not connect to server."); |
70 | 0 | e.printStackTrace(); |
71 | 0 | System.exit(1); |
72 | 0 | } |
73 | ||
74 | try |
|
75 | { |
|
76 | 0 | client.rcommand(localuser, remoteuser, command); |
77 | } |
|
78 | 0 | catch (IOException e) |
79 | { |
|
80 | try |
|
81 | { |
|
82 | 0 | client.disconnect(); |
83 | } |
|
84 | 0 | catch (IOException f) |
85 | 0 | {} |
86 | 0 | e.printStackTrace(); |
87 | 0 | System.err.println("Could not execute command."); |
88 | 0 | System.exit(1); |
89 | 0 | } |
90 | ||
91 | ||
92 | 0 | IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), |
93 | System.in, System.out); |
|
94 | ||
95 | try |
|
96 | { |
|
97 | 0 | client.disconnect(); |
98 | } |
|
99 | 0 | catch (IOException e) |
100 | { |
|
101 | 0 | e.printStackTrace(); |
102 | 0 | System.exit(1); |
103 | 0 | } |
104 | ||
105 | 0 | System.exit(0); |
106 | 0 | } |
107 | ||
108 | } |
|
109 |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |