001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package examples;
017    
018    import java.io.IOException;
019    import org.apache.commons.net.bsd.RLoginClient;
020    
021    /***
022     * This is an example program demonstrating how to use the RLoginClient
023     * class. This program connects to an rlogin daemon and begins to
024     * interactively read input from stdin (this will be line buffered on most
025     * systems, so don't expect character at a time interactivity), passing it
026     * to the remote login process and writing the remote stdout and stderr
027     * to local stdout.  If you don't have .rhosts or hosts.equiv files set up,
028     * the rlogin daemon will prompt you for a password.
029     * <p>
030     * On Unix systems you will not be able to use the rshell capability
031     * unless the process runs as root since only root can bind port addresses
032     * lower than 1024.
033     * <p>
034     * JVM's using green threads will likely have problems if the rlogin daemon
035     * requests a password.  This program is merely a demonstration and is
036     * not suitable for use as an application, especially given that it relies
037     * on line buffered input from System.in.  The best way to run this example
038     * is probably from a Win95 dos box into a Unix host.
039     * <p>
040     * Example: java rlogin myhost localusername remoteusername vt100
041     * <p>
042     * Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>
043     * <p>
044     ***/
045    
046    // This class requires the IOUtil support class!
047    public final class rlogin
048    {
049    
050        public static final void main(String[] args)
051        {
052            String server, localuser, remoteuser, terminal;
053            RLoginClient client;
054    
055            if (args.length != 4)
056            {
057                System.err.println(
058                    "Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>");
059                System.exit(1);
060                return ; // so compiler can do proper flow control analysis
061            }
062    
063            client = new RLoginClient();
064    
065            server = args[0];
066            localuser = args[1];
067            remoteuser = args[2];
068            terminal = args[3];
069    
070            try
071            {
072                client.connect(server);
073            }
074            catch (IOException e)
075            {
076                System.err.println("Could not connect to server.");
077                e.printStackTrace();
078                System.exit(1);
079            }
080    
081            try
082            {
083                client.rlogin(localuser, remoteuser, terminal);
084            }
085            catch (IOException e)
086            {
087                try
088                {
089                    client.disconnect();
090                }
091                catch (IOException f)
092                {}
093                e.printStackTrace();
094                System.err.println("rlogin authentication failed.");
095                System.exit(1);
096            }
097    
098    
099            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