rshell.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.unix;

  18. import java.io.IOException;

  19. import org.apache.commons.net.bsd.RCommandClient;
  20. import org.apache.commons.net.examples.util.IOUtil;

  21. /**
  22.  * This is an example program demonstrating how to use the RCommandClient class. This program connects to an rshell daemon and requests that the given command
  23.  * be executed on the server. It then reads input from stdin (this will be line buffered on most systems, so don't expect character at a time interactivity),
  24.  * passing it to the remote process and writes the process stdout and stderr to local stdout.
  25.  * <p>
  26.  * On UNIX systems you will not be able to use the rshell capability unless the process runs as root since only root can bind port addresses lower than 1024.
  27.  * <p>
  28.  * Example: java rshell myhost localusername remoteusername "ps -aux"
  29.  * <p>
  30.  * Usage: rshell <hostname> <localuser> <remoteuser> <command>
  31.  */

  32. // This class requires the IOUtil support class!
  33. public final class rshell {

  34.     public static void main(final String[] args) {

  35.         if (args.length != 4) {
  36.             System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>");
  37.             System.exit(1);
  38.             return; // so compiler can do proper flow control analysis
  39.         }

  40.         final RCommandClient client = new RCommandClient();
  41.         final String server = args[0];
  42.         final String localuser = args[1];
  43.         final String remoteuser = args[2];
  44.         final String command = args[3];

  45.         try {
  46.             client.connect(server);
  47.         } catch (final IOException e) {
  48.             System.err.println("Could not connect to server.");
  49.             e.printStackTrace();
  50.             System.exit(1);
  51.         }

  52.         try {
  53.             client.rcommand(localuser, remoteuser, command);
  54.         } catch (final IOException e) {
  55.             try {
  56.                 client.disconnect();
  57.             } catch (final IOException f) {
  58.                 /* ignored */
  59.             }
  60.             e.printStackTrace();
  61.             System.err.println("Could not execute command.");
  62.             System.exit(1);
  63.         }

  64.         IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out);

  65.         try {
  66.             client.disconnect();
  67.         } catch (final IOException e) {
  68.             e.printStackTrace();
  69.             System.exit(1);
  70.         }

  71.         System.exit(0);
  72.     }

  73. }