001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package examples;
017    
018    import java.io.IOException;
019    import org.apache.commons.net.bsd.RExecClient;
020    
021    /***
022     * This is an example program demonstrating how to use the RExecClient class.
023     * This program connects to an rexec server and requests that the
024     * given command be executed on the server.  It then reads input from stdin
025     * (this will be line buffered on most systems, so don't expect character
026     * at a time interactivity), passing it to the remote process and writes
027     * the process stdout and stderr to local stdout.
028     * <p>
029     * Example: java rexec myhost myusername mypassword "ps -aux"
030     * <p>
031     * Usage: rexec <hostname> <username> <password> <command>
032     * <p>
033     ***/
034    
035    // This class requires the IOUtil support class!
036    public final class rexec
037    {
038    
039        public static final void main(String[] args)
040        {
041            String server, username, password, command;
042            RExecClient client;
043    
044            if (args.length != 4)
045            {
046                System.err.println(
047                    "Usage: rexec <hostname> <username> <password> <command>");
048                System.exit(1);
049                return ; // so compiler can do proper flow control analysis
050            }
051    
052            client = new RExecClient();
053    
054            server = args[0];
055            username = args[1];
056            password = args[2];
057            command = args[3];
058    
059            try
060            {
061                client.connect(server);
062            }
063            catch (IOException e)
064            {
065                System.err.println("Could not connect to server.");
066                e.printStackTrace();
067                System.exit(1);
068            }
069    
070            try
071            {
072                client.rexec(username, password, command);
073            }
074            catch (IOException e)
075            {
076                try
077                {
078                    client.disconnect();
079                }
080                catch (IOException f)
081                {}
082                e.printStackTrace();
083                System.err.println("Could not execute command.");
084                System.exit(1);
085            }
086    
087    
088            IOUtil.readWrite(client.getInputStream(), client.getOutputStream(),
089                             System.in, System.out);
090    
091            try
092            {
093                client.disconnect();
094            }
095            catch (IOException e)
096            {
097                e.printStackTrace();
098                System.exit(1);
099            }
100    
101            System.exit(0);
102        }
103    
104    }
105