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.RCommandClient;
020    
021    /***
022     * This is an example program demonstrating how to use the RCommandClient
023     * class. This program connects to an rshell daemon 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     * On Unix systems you will not be able to use the rshell capability
030     * unless the process runs as root since only root can bind port addresses
031     * lower than 1024.
032     * <p>
033     * Example: java rshell myhost localusername remoteusername "ps -aux"
034     * <p>
035     * Usage: rshell <hostname> <localuser> <remoteuser> <command>
036     * <p>
037     ***/
038    
039    // This class requires the IOUtil support class!
040    public final class rshell
041    {
042    
043        public static final void main(String[] args)
044        {
045            String server, localuser, remoteuser, command;
046            RCommandClient client;
047    
048            if (args.length != 4)
049            {
050                System.err.println(
051                    "Usage: rshell <hostname> <localuser> <remoteuser> <command>");
052                System.exit(1);
053                return ; // so compiler can do proper flow control analysis
054            }
055    
056            client = new RCommandClient();
057    
058            server = args[0];
059            localuser = args[1];
060            remoteuser = 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.rcommand(localuser, remoteuser, 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