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