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 18 package org.apache.commons.net.examples.unix; 19 20 import java.io.IOException; 21 22 import org.apache.commons.net.bsd.RCommandClient; 23 import org.apache.commons.net.examples.util.IOUtil; 24 25 /** 26 * 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 27 * 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), 28 * passing it to the remote process and writes the process stdout and stderr to local stdout. 29 * <p> 30 * 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. 31 * <p> 32 * Example: java rshell myhost localusername remoteusername "ps -aux" 33 * <p> 34 * Usage: rshell <hostname> <localuser> <remoteuser> <command> 35 */ 36 37 // This class requires the IOUtil support class! 38 public final class rshell { 39 40 public static void main(final String[] args) { 41 42 if (args.length != 4) { 43 System.err.println("Usage: rshell <hostname> <localuser> <remoteuser> <command>"); 44 System.exit(1); 45 return; // so compiler can do proper flow control analysis 46 } 47 48 final RCommandClient client = new RCommandClient(); 49 final String server = args[0]; 50 final String localuser = args[1]; 51 final String remoteuser = args[2]; 52 final String command = args[3]; 53 54 try { 55 client.connect(server); 56 } catch (final IOException e) { 57 System.err.println("Could not connect to server."); 58 e.printStackTrace(); 59 System.exit(1); 60 } 61 62 try { 63 client.rcommand(localuser, remoteuser, command); 64 } catch (final IOException e) { 65 try { 66 client.disconnect(); 67 } catch (final IOException f) { 68 /* ignored */ 69 } 70 e.printStackTrace(); 71 System.err.println("Could not execute command."); 72 System.exit(1); 73 } 74 75 IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out); 76 77 try { 78 client.disconnect(); 79 } catch (final IOException e) { 80 e.printStackTrace(); 81 System.exit(1); 82 } 83 84 System.exit(0); 85 } 86 87 }