Hello all,
I have a unix box and am trying to do an ssh to itself using java. I am ausing the following code for that:
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public class UnixExecTest {
public static void main(String args[]) {
try {
String input = "test";
while (!(input.equals("exit"))) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
System.out.print("Enter command: ");
input = stdin.readLine();
System.out.println("input = " + input);
String[] cmd = {"sh", "-c", input.trim()};
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd);
Process proc = rt.exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc
.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(proc
.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Using the code above, I am able to execute the unix commands. All the basic commands like ps, ls, id etc gives perfect output. But when i try to do an "ssh
", I see an error message "Pseudo-terminal will not be allocated because stdin is not a terminal." Then after a couple of seconds, it shows me the welcome message like its supposed to be and asks for my password. as soon as I wnter my password, it gives me an error message like :
ttytype: couldn't open /dev/tty for reading
stty: : Not a typewriter
Not a terminal
Not a terminal
stty: : Not a typewriter
stty: : Not a typewriter
resize: Can't open terminal /dev/tty
and then looks like the program is hanging and doesnt do anything. I am expecting a unix promt to come up for entering commands. Any ideas on where i am going wrong ? I'd really appreciate any help.
Thank you !!
Let me paste the contents of the output screen once again:
Code:
ERROR>Pseudo-terminal will not be allocated because stdin is not a terminal.
ERROR>
ERROR>************************************************************************
ERROR>** **
ERROR>** THIS SYSTEM IS RESTRICTED! **
ERROR>** **
ERROR>** You are authorized to use this system for approved business **
ERROR>** purposes only. Use for any other purpose is prohibited. All **
ERROR>** transactional records, reports, e-mail, software, and other **
ERROR>** data generated by or residing upon this system are the property **
ERROR>** of the Company and may be used by the Company for any purpose. **
ERROR>** Authorized and unauthorized activities may be monitored. **
ERROR>** **
ERROR>************************************************************************
ERROR>
ERROR>************************************************************************
ERROR>* *
ERROR>* CAUTION: You have reached a restricted access system. *
ERROR>* Unauthorized use of this system is prohibited and will *
ERROR>* be prosecuted to the fullest extent of the law. *
ERROR>* *
ERROR>************************************************************************
ERROR>
Enter user's Password:
ERROR>ttytype: couldn't open /dev/tty for reading
ERROR>stty: : Not a typewriter
ERROR>Not a terminal
ERROR>Not a terminal
ERROR>stty: : Not a typewriter
ERROR>stty: : Not a typewriter
ERROR>resize: Can't open terminal /dev/tty
OUTPUT>
OUTPUT>
OUTPUT>
OUTPUT>