    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
    018    package examples.unix;
    019
    020    import java.io.IOException;
    021    import org.apache.commons.net.bsd.RCommandClient;
    022
    023    import examples.util.IOUtil;
    024
    025    /***
    026     * This is an example program demonstrating how to use the RCommandClient
    027     * class. This program connects to an rshell daemon and requests that the
    028     * given command be executed on the server.  It then reads input from stdin
    029     * (this will be line buffered on most systems, so don't expect character
    030     * at a time interactivity), passing it to the remote process and writes
    031     * the process stdout and stderr to local stdout.
    032     * <p>
    033     * On Unix systems you will not be able to use the rshell capability
    034     * unless the process runs as root since only root can bind port addresses
    035     * lower than 1024.
    036     * <p>
    037     * Example: java rshell myhost localusername remoteusername "ps -aux"
    038     * <p>
    039     * Usage: rshell <hostname> <localuser> <remoteuser> <command>
    040     * <p>
    041     ***/
    042
    043    // This class requires the IOUtil support class!
    044    public final class rshell
    045    {
    046
    047        public static final void main(String[] args)
    048        {
    049            String server, localuser, remoteuser, command;
    050            RCommandClient client;
    051
    052            if (args.length != 4)
    053            {
    054                System.err.println(
    055                    "Usage: rshell <hostname> <localuser> <remoteuser> <command>");
    056                System.exit(1);
    057                return ; // so compiler can do proper flow control analysis
    058            }
    059
    060            client = new RCommandClient();
    061
    062            server = args[0];
    063            localuser = args[1];
    064            remoteuser = args[2];
    065            command = args[3];
    066
    067            try
    068            {
    069                client.connect(server);
    070            }
    071            catch (IOException e)
    072            {
    073                System.err.println("Could not connect to server.");
    074                e.printStackTrace();
    075                System.exit(1);
    076            }
    077
    078            try
    079            {
    080                client.rcommand(localuser, remoteuser, command);
    081            }
    082            catch (IOException e)
    083            {
    084                try
    085                {
    086                    client.disconnect();
    087                }
    088                catch (IOException f)
    089                {}
    090                e.printStackTrace();
    091                System.err.println("Could not execute command.");
    092                System.exit(1);
    093            }
    094
    095
    096            IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
    097                             System.in, System.out);
    098
    099            try
    100            {
    101                client.disconnect();
    102            }
    103            catch (IOException e)
    104            {
    105                e.printStackTrace();
    106                System.exit(1);
    107            }
    108
    109            System.exit(0);
    110        }
    111
    112    }
    113