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.RCommandClient;
023import org.apache.commons.net.examples.util.IOUtil;
024
025/**
026 * This is an example program demonstrating how to use the RCommandClient class. This program connects to an rshell daemon and requests that the given command
027 * be executed on the server. It then reads input from stdin (this will be line buffered on most systems, so don't expect character at a time interactivity),
028 * passing it to the remote process and writes the process stdout and stderr to local stdout.
029 * <p>
030 * 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.
031 * <p>
032 * Example: java rshell myhost localusername remoteusername "ps -aux"
033 * <p>
034 * Usage: rshell <hostname> <localuser> <remoteuser> <command>
035 */
036
037// This class requires the IOUtil support class!
038public final class rshell {
039
040    public static void main(final String[] args) {
041
042        if (args.length != 4) {
043            System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
044            System.exit(1);
045            return; // so compiler can do proper flow control analysis
046        }
047
048        final RCommandClient client = new RCommandClient();
049        final String server = args[0];
050        final String localuser = args[1];
051        final String remoteuser = args[2];
052        final String command = args[3];
053
054        try {
055            client.connect(server);
056        } catch (final IOException e) {
057            System.err.println("Could not connect to server.");
058            e.printStackTrace();
059            System.exit(1);
060        }
061
062        try {
063            client.rcommand(localuser, remoteuser, command);
064        } catch (final IOException e) {
065            try {
066                client.disconnect();
067            } catch (final IOException f) {
068                /* ignored */
069            }
070            e.printStackTrace();
071            System.err.println("Could not execute command.");
072            System.exit(1);
073        }
074
075        IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out);
076
077        try {
078            client.disconnect();
079        } catch (final IOException e) {
080            e.printStackTrace();
081            System.exit(1);
082        }
083
084        System.exit(0);
085    }
086
087}