rexec.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.RExecClient;
  20. import org.apache.commons.net.examples.util.IOUtil;

  21. /**
  22.  * This is an example program demonstrating how to use the RExecClient class. This program connects to an rexec server and requests that the given command be
  23.  * 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.  * Example: java rexec myhost myuser mypassword "ps -aux"
  27.  * <p>
  28.  * Usage: rexec <hostname> <username> <password> <command>
  29.  */

  30. // This class requires the IOUtil support class!
  31. public final class rexec {

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

  33.         if (args.length != 4) {
  34.             System.err.println("Usage: rexec <hostname> <user> <password> <command>");
  35.             System.exit(1);
  36.             return; // so compiler can do proper flow control analysis
  37.         }

  38.         final RExecClient client = new RExecClient();
  39.         final String server = args[0];
  40.         final String user = args[1];
  41.         final String password = args[2];
  42.         final String command = args[3];

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

  50.         try {
  51.             client.rexec(user, password, command);
  52.         } catch (final IOException e) {
  53.             try {
  54.                 client.disconnect();
  55.             } catch (final IOException f) {
  56.                 /* ignored */
  57.             }
  58.             e.printStackTrace();
  59.             System.err.println("Could not execute command.");
  60.             System.exit(1);
  61.         }

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

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

  69.         System.exit(0);
  70.     }

  71. }