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