    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.RExecClient;
    022
    023    import examples.util.IOUtil;
    024
    025    /***
    026     * This is an example program demonstrating how to use the RExecClient class.
    027     * This program connects to an rexec server 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     * Example: java rexec myhost myusername mypassword "ps -aux"
    034     * <p>
    035     * Usage: rexec <hostname> <username> <password> <command>
    036     * <p>
    037     ***/
    038
    039    // This class requires the IOUtil support class!
    040    public final class rexec
    041    {
    042
    043        public static final void main(String[] args)
    044        {
    045            String server, username, password, command;
    046            RExecClient client;
    047
    048            if (args.length != 4)
    049            {
    050                System.err.println(
    051                    "Usage: rexec <hostname> <username> <password> <command>");
    052                System.exit(1);
    053                return ; // so compiler can do proper flow control analysis
    054            }
    055
    056            client = new RExecClient();
    057
    058            server = args[0];
    059            username = args[1];
    060            password = args[2];
    061            command = args[3];
    062
    063            try
    064            {
    065                client.connect(server);
    066            }
    067            catch (IOException e)
    068            {
    069                System.err.println("Could not connect to server.");
    070                e.printStackTrace();
    071                System.exit(1);
    072            }
    073
    074            try
    075            {
    076                client.rexec(username, password, command);
    077            }
    078            catch (IOException e)
    079            {
    080                try
    081                {
    082                    client.disconnect();
    083                }
    084                catch (IOException f)
    085                {}
    086                e.printStackTrace();
    087                System.err.println("Could not execute command.");
    088                System.exit(1);
    089            }
    090
    091
    092            IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
    093                             System.in, System.out);
    094
    095            try
    096            {
    097                client.disconnect();
    098            }
    099            catch (IOException e)
    100            {
    101                e.printStackTrace();
    102                System.exit(1);
    103            }
    104
    105            System.exit(0);
    106        }
    107
    108    }
    109