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