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