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
018package examples;
019
020import java.io.IOException;
021import org.apache.commons.net.bsd.RLoginClient;
022
023/***
024 * This is an example program demonstrating how to use the RLoginClient
025 * class. This program connects to an rlogin daemon and begins to
026 * interactively read input from stdin (this will be line buffered on most
027 * systems, so don't expect character at a time interactivity), passing it
028 * to the remote login process and writing the remote stdout and stderr
029 * to local stdout.  If you don't have .rhosts or hosts.equiv files set up,
030 * the rlogin daemon will prompt you for a password.
031 * <p>
032 * On Unix systems you will not be able to use the rshell capability
033 * unless the process runs as root since only root can bind port addresses
034 * lower than 1024.
035 * <p>
036 * JVM's using green threads will likely have problems if the rlogin daemon
037 * requests a password.  This program is merely a demonstration and is
038 * not suitable for use as an application, especially given that it relies
039 * on line buffered input from System.in.  The best way to run this example
040 * is probably from a Win95 dos box into a Unix host.
041 * <p>
042 * Example: java rlogin myhost localusername remoteusername vt100
043 * <p>
044 * Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>
045 * <p>
046 ***/
047
048// This class requires the IOUtil support class!
049public final class rlogin
050{
051
052    public static final void main(String[] args)
053    {
054        String server, localuser, remoteuser, terminal;
055        RLoginClient client;
056
057        if (args.length != 4)
058        {
059            System.err.println(
060                "Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>");
061            System.exit(1);
062            return ; // so compiler can do proper flow control analysis
063        }
064
065        client = new RLoginClient();
066
067        server = args[0];
068        localuser = args[1];
069        remoteuser = args[2];
070        terminal = args[3];
071
072        try
073        {
074            client.connect(server);
075        }
076        catch (IOException e)
077        {
078            System.err.println("Could not connect to server.");
079            e.printStackTrace();
080            System.exit(1);
081        }
082
083        try
084        {
085            client.rlogin(localuser, remoteuser, terminal);
086        }
087        catch (IOException e)
088        {
089            try
090            {
091                client.disconnect();
092            }
093            catch (IOException f)
094            {}
095            e.printStackTrace();
096            System.err.println("rlogin authentication failed.");
097            System.exit(1);
098        }
099
100
101        IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
102                         System.in, System.out);
103
104        try
105        {
106            client.disconnect();
107        }
108        catch (IOException e)
109        {
110            e.printStackTrace();
111            System.exit(1);
112        }
113
114        System.exit(0);
115    }
116
117}
118