View Javadoc

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