java -issue with runtime.exec

Discussion in 'General Computer Support' started by pressley, Dec 11, 2007.

  1. pressley

    pressley

    Joined:
    Dec 11, 2007
    Messages:
    1
    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 myuserid@myserver.com", 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>
    
    
    pressley, Dec 11, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay
    Replies:
    2
    Views:
    434
  2. Kenneth Marson

    Java 2 Runtime Environment Standard Edition

    Kenneth Marson, Oct 22, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    418
  3. Christian Falch
    Replies:
    1
    Views:
    5,805
    Toolman Tim
    Jun 23, 2004
  4. huck

    JAVA Runtime

    huck, Sep 15, 2005, in forum: Computer Support
    Replies:
    1
    Views:
    474
    =?ISO-8859-1?Q?Brian_H=B9=A9?=
    Sep 15, 2005
  5. huck

    Java Runtime

    huck, Sep 16, 2005, in forum: Computer Support
    Replies:
    4
    Views:
    1,074
    old jon
    Sep 16, 2005
Loading...

Share This Page