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 .rhosts or hosts.equiv files set up, the rlogin daemon will prompt you for a
029 * 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 Win95 dos
035 * box into a Unix host.
036 * <p>
037 * Example: java rlogin myhost localusername remoteusername 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        final String server;
047        final String localuser;
048        final String remoteuser;
049        final String terminal;
050        final RLoginClient client;
051
052        if (args.length != 4) {
053            System.err.println("Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>");
054            System.exit(1);
055            return; // so compiler can do proper flow control analysis
056        }
057
058        client = new RLoginClient();
059
060        server = args[0];
061        localuser = args[1];
062        remoteuser = args[2];
063        terminal = args[3];
064
065        try {
066            client.connect(server);
067        } catch (final IOException e) {
068            System.err.println("Could not connect to server.");
069            e.printStackTrace();
070            System.exit(1);
071        }
072
073        try {
074            client.rlogin(localuser, remoteuser, terminal);
075        } catch (final IOException e) {
076            try {
077                client.disconnect();
078            } catch (final IOException f) {
079                /* ignored */}
080            e.printStackTrace();
081            System.err.println("rlogin authentication failed.");
082            System.exit(1);
083        }
084
085        IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out);
086
087        try {
088            client.disconnect();
089        } catch (final IOException e) {
090            e.printStackTrace();
091            System.exit(1);
092        }
093
094        System.exit(0);
095    }
096
097}