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