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.RLoginClient;
21
22 /***
23 * This is an example program demonstrating how to use the RLoginClient
24 * class. This program connects to an rlogin daemon and begins to
25 * interactively read input from stdin (this will be line buffered on most
26 * systems, so don't expect character at a time interactivity), passing it
27 * to the remote login process and writing the remote stdout and stderr
28 * to local stdout. If you don't have .rhosts or hosts.equiv files set up,
29 * the rlogin daemon will prompt you for a password.
30 * <p>
31 * On Unix systems you will not be able to use the rshell capability
32 * unless the process runs as root since only root can bind port addresses
33 * lower than 1024.
34 * <p>
35 * JVM's using green threads will likely have problems if the rlogin daemon
36 * requests a password. This program is merely a demonstration and is
37 * not suitable for use as an application, especially given that it relies
38 * on line buffered input from System.in. The best way to run this example
39 * is probably from a Win95 dos box into a Unix host.
40 * <p>
41 * Example: java rlogin myhost localusername remoteusername vt100
42 * <p>
43 * Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>
44 * <p>
45 ***/
46
47 // This class requires the IOUtil support class!
48 public final class rlogin
49 {
50
51 public static final void main(String[] args)
52 {
53 String server, localuser, remoteuser, terminal;
54 RLoginClient client;
55
56 if (args.length != 4)
57 {
58 System.err.println(
59 "Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>");
60 System.exit(1);
61 return ; // so compiler can do proper flow control analysis
62 }
63
64 client = new RLoginClient();
65
66 server = args[0];
67 localuser = args[1];
68 remoteuser = args[2];
69 terminal = args[3];
70
71 try
72 {
73 client.connect(server);
74 }
75 catch (IOException e)
76 {
77 System.err.println("Could not connect to server.");
78 e.printStackTrace();
79 System.exit(1);
80 }
81
82 try
83 {
84 client.rlogin(localuser, remoteuser, terminal);
85 }
86 catch (IOException e)
87 {
88 try
89 {
90 client.disconnect();
91 }
92 catch (IOException f)
93 {}
94 e.printStackTrace();
95 System.err.println("rlogin authentication failed.");
96 System.exit(1);
97 }
98
99
100 IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
101 System.in, System.out);
102
103 try
104 {
105 client.disconnect();
106 }
107 catch (IOException e)
108 {
109 e.printStackTrace();
110 System.exit(1);
111 }
112
113 System.exit(0);
114 }
115
116 }
117