1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
package com.jcraft.util;
import java.io.*;
import java.net.*;
import java.lang.*;
public class JRexec implements Runnable {
private Thread thread=null;
private InputStream data=null;
private InputStream in=null;
private OutputStream out=null;
private boolean devnull=true;
private Socket socket=null;
String user=null;
String host=null;
String passwd=null;
String command=null;
public JRexec(String user, String host, String passwd, String command)
throws JRexecException {
this.user=user;
this.host=host;
this.passwd=passwd;
this.command=command;
try{
socket = new Socket(host, 512);
in = socket.getInputStream();
out = socket.getOutputStream();
}
catch(java.net.UnknownHostException e){
throw new JRexecException(e.toString());
}
catch(java.io.IOException e) {
throw new JRexecException(e.toString());
}
}
public JRexec(String user, String host, String passwd, String command,
InputStream data)
throws JRexecException {
this(user, host, passwd, command);
this.data=data;
}
public JRexec(String user, String host, String passwd, String command,
String data)
throws JRexecException {
this(user, host, passwd, command);
this.data=new ByteArrayInputStream(data.getBytes());
}
public synchronized InputStream getResult(){
if(thread!=null || !devnull) return null;
devnull=false;
return in;
}
public synchronized void doit(){
if(thread==null){
thread=new Thread(this);
thread.start();
}
}
public void run() {
try {
// port
out.write(0);
int chop=8;
if(user.length()<chop) chop=user.length();
out.write(user.getBytes(), 0, chop);
out.write(0);
chop=8;
if(passwd.length()<chop) chop=passwd.length();
out.write(passwd.getBytes(), 0, chop);
out.write(0);
chop=4096;
if(command.length()<chop) chop=command.length();
out.write(command.getBytes(), 0, chop);
out.write(0);
byte[] buf=new byte[1024];
if(data!=null){
while(true){
int len=data.read(buf, 0, buf.length);
if(len==-1) break;
out.write(buf, 0, len);
}
}
if(devnull){
byte[] b=new byte[1];
in.read(b, 0, 1);
if(b[0]==0){ // success
while(true){
int len=in.read(buf, 0, buf.length);
if(len==-1) break;
System.out.print(new String(buf, 0, len));
}
}
else{ // error message
while(true){
int len=in.read(buf, 0, buf.length);
if(len==-1) break;
System.out.print(new String(buf, 0, len));
}
}
}
// stop();
}
catch(IOException e) {
close();
System.out.println("IO Error : " + e );
}
return;
}
private synchronized void stop(){
if(thread!=null){
thread=null;
}
}
public void close(){
try {
in.close();
out.close();
socket.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String arg[]){
try {
JRexec jrexec=new JRexec("yamanaka", "localhost", "foo", "du /");
InputStream in=jrexec.getResult();
jrexec.doit();
byte[] b=new byte[1];
in.read(b, 0, 1); // 0
System.out.println("return: "+b[0]);
byte[] buf=new byte[1024];
while(true){
int len=in.read(buf, 0, buf.length);
if(len==-1) break;
System.out.print(new String(buf, 0, len));
}
}
catch(Exception e) {
System.out.println("IO Error : " + e );
}
}
}
|