001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package examples.unix; 019 020import java.io.IOException; 021import org.apache.commons.net.bsd.RCommandClient; 022 023import examples.util.IOUtil; 024 025/*** 026 * This is an example program demonstrating how to use the RCommandClient 027 * class. This program connects to an rshell daemon and requests that the 028 * given command be executed on the server. It then reads input from stdin 029 * (this will be line buffered on most systems, so don't expect character 030 * at a time interactivity), passing it to the remote process and writes 031 * the process stdout and stderr to local stdout. 032 * <p> 033 * On Unix systems you will not be able to use the rshell capability 034 * unless the process runs as root since only root can bind port addresses 035 * lower than 1024. 036 * <p> 037 * Example: java rshell myhost localusername remoteusername "ps -aux" 038 * <p> 039 * Usage: rshell <hostname> <localuser> <remoteuser> <command> 040 ***/ 041 042// This class requires the IOUtil support class! 043public final class rshell 044{ 045 046 public static void main(String[] args) 047 { 048 String server, localuser, remoteuser, command; 049 RCommandClient client; 050 051 if (args.length != 4) 052 { 053 System.err.println( 054 "Usage: rshell <hostname> <localuser> <remoteuser> <command>"); 055 System.exit(1); 056 return ; // so compiler can do proper flow control analysis 057 } 058 059 client = new RCommandClient(); 060 061 server = args[0]; 062 localuser = args[1]; 063 remoteuser = args[2]; 064 command = args[3]; 065 066 try 067 { 068 client.connect(server); 069 } 070 catch (IOException e) 071 { 072 System.err.println("Could not connect to server."); 073 e.printStackTrace(); 074 System.exit(1); 075 } 076 077 try 078 { 079 client.rcommand(localuser, remoteuser, command); 080 } 081 catch (IOException e) 082 { 083 try 084 { 085 client.disconnect(); 086 } 087 catch (IOException f) 088 {/* ignored */} 089 e.printStackTrace(); 090 System.err.println("Could not execute command."); 091 System.exit(1); 092 } 093 094 095 IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), 096 System.in, System.out); 097 098 try 099 { 100 client.disconnect(); 101 } 102 catch (IOException e) 103 { 104 e.printStackTrace(); 105 System.exit(1); 106 } 107 108 System.exit(0); 109 } 110 111} 112