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