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