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 examples; 19 20 import java.io.IOException; 21 import org.apache.commons.net.bsd.RCommandClient; 22 23 /*** 24 * This is an example program demonstrating how to use the RCommandClient 25 * class. This program connects to an rshell daemon and requests that the 26 * given command be executed on the server. It then reads input from stdin 27 * (this will be line buffered on most systems, so don't expect character 28 * at a time interactivity), passing it to the remote process and writes 29 * the process stdout and stderr to local stdout. 30 * <p> 31 * On Unix systems you will not be able to use the rshell capability 32 * unless the process runs as root since only root can bind port addresses 33 * lower than 1024. 34 * <p> 35 * Example: java rshell myhost localusername remoteusername "ps -aux" 36 * <p> 37 * Usage: rshell <hostname> <localuser> <remoteuser> <command> 38 * <p> 39 ***/ 40 41 // This class requires the IOUtil support class! 42 public final class rshell 43 { 44 45 public static final void main(String[] args) 46 { 47 String server, localuser, remoteuser, command; 48 RCommandClient client; 49 50 if (args.length != 4) 51 { 52 System.err.println( 53 "Usage: rshell <hostname> <localuser> <remoteuser> <command>"); 54 System.exit(1); 55 return ; // so compiler can do proper flow control analysis 56 } 57 58 client = new RCommandClient(); 59 60 server = args[0]; 61 localuser = args[1]; 62 remoteuser = args[2]; 63 command = args[3]; 64 65 try 66 { 67 client.connect(server); 68 } 69 catch (IOException e) 70 { 71 System.err.println("Could not connect to server."); 72 e.printStackTrace(); 73 System.exit(1); 74 } 75 76 try 77 { 78 client.rcommand(localuser, remoteuser, command); 79 } 80 catch (IOException e) 81 { 82 try 83 { 84 client.disconnect(); 85 } 86 catch (IOException f) 87 {} 88 e.printStackTrace(); 89 System.err.println("Could not execute command."); 90 System.exit(1); 91 } 92 93 94 IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), 95 System.in, System.out); 96 97 try 98 { 99 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