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