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        final String server;
042        final String localuser;
043        final String remoteuser;
044        final String command;
045        final RCommandClient client;
046
047        if (args.length != 4) {
048            System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
049            System.exit(1);
050            return; // so compiler can do proper flow control analysis
051        }
052
053        client = new RCommandClient();
054
055        server = args[0];
056        localuser = args[1];
057        remoteuser = args[2];
058        command = args[3];
059
060        try {
061            client.connect(server);
062        } catch (final IOException e) {
063            System.err.println("Could not connect to server.");
064            e.printStackTrace();
065            System.exit(1);
066        }
067
068        try {
069            client.rcommand(localuser, remoteuser, command);
070        } catch (final IOException e) {
071            try {
072                client.disconnect();
073            } catch (final IOException f) {
074                /* ignored */}
075            e.printStackTrace();
076            System.err.println("Could not execute command.");
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}