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.unix;
019
020import java.io.IOException;
021import org.apache.commons.net.bsd.RLoginClient;
022
023import 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 ***/
048
049// This class requires the IOUtil support class!
050public final class rlogin
051{
052
053    public static void main(String[] args)
054    {
055        String server, localuser, remoteuser, terminal;
056        RLoginClient client;
057
058        if (args.length != 4)
059        {
060            System.err.println(
061                "Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>");
062            System.exit(1);
063            return ; // so compiler can do proper flow control analysis
064        }
065
066        client = new RLoginClient();
067
068        server = args[0];
069        localuser = args[1];
070        remoteuser = args[2];
071        terminal = args[3];
072
073        try
074        {
075            client.connect(server);
076        }
077        catch (IOException e)
078        {
079            System.err.println("Could not connect to server.");
080            e.printStackTrace();
081            System.exit(1);
082        }
083
084        try
085        {
086            client.rlogin(localuser, remoteuser, terminal);
087        }
088        catch (IOException e)
089        {
090            try
091            {
092                client.disconnect();
093            }
094            catch (IOException f)
095            {/* ignored */}
096            e.printStackTrace();
097            System.err.println("rlogin authentication failed.");
098            System.exit(1);
099        }
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