Command output lost during shell session.

Thomas Höhn <[email protected]>
Newsgroups gmane.comp.java.sshtools.user
Message-ID <[email protected]>
Hi there,

I run commands through a shell started in a ssh session channel.
There is no problem if I just run simple commands on the remote host
which produce an output or error message which I can read out.

What I want to do is a conversation with the command like
the chat or expect scripts do in order to automate a task like
adduser - or more important for me: opiepasswd - where I need
to parse the output and send a computed response.

But sometimes the output of the command gets lost even if
I flush input/output beforehand and wait for the output of the
command for a long time.

I start the command by writing it to the session output stream with
a final newline, then wait and catch the output of the command
(adduser, opiepasswd) from the input stream and sending the
password as response to the output stream (and once again).
For opiepasswd I have to parse the output of the command and
send a computed response. But it often fails because of the lost
output...

Whats wrong? Any idea?
Should I use a different ssh channel for that?

Regards,
Thomas Hoehn


Here are some code fragments I use:

==8<================================================

public InputStream in ;
public OutputStream out ;
public InputStream err;
private String errmsg;
private String inmsg;

SessionChannelClient session = sshClient.openSessionChannel();

try {
            session.startShell();
} catch (IOException e) {}

in = session.getInputStream();
out = session.getOutputStream();
try {
            err = session.getStderrInputStream();
} catch (IOException e) {}

...

public int addUser(String user, String pwd) {

if(sendCommand("adduser "+user, timeout) < 0) return -1;
else if(inmsg.indexOf("New password:") == -1) return -1;
else if(sendString(pwd, true, false, timeout) < 0) return -1;
else if(inmsg==null||csSession.getInMsg().indexOf("Re-enter new
password:") == -1) return -1;
else if(sendString(pwd, true, false, timeout) < 0) return -1;
else if(inmsg==null||inmsg.indexOf("Password changed") == -1) return -1;
return 0;
}

public int sendCommand(String command, long waitTime) throws IOException {
        return sendString(command, true, true, waitTime);
}
   
public int sendString(String command, boolean flush, long waitTime)
throws IOException {
       
        if (command==null || command.trim().equals("")) {
            return 0;
        }
       
        // flush input and err stream
        if(flush) {
            errmsg = readerr();
            inmsg = read();
        }
       
        String finalCmd = command + "\n";
               
        out.write(finalCmd.getBytes());
        out.flush();
       
        try {
            Thread.currentThread().sleep(waitTime);
        } catch(Exception e) {
        }
       
        errmsg = readerr();
        inmsg = read(); // or read(timeOutInSeconds)
       
        if (errmsg != null) return -1;
        if (inmsg != null) return inmsg.length();
        return 0;
}

    public String read(int timeoutSeconds) throws IOException{
        byte[] buffer = new byte[2048];
        int count = 0;
        String retString = null;
        int n_timer = 0;
        do {
            if (in.available() > 0) {
                count = in.read(buffer);
                if (count > 0) {
                    String readString = new String (buffer,0,count);
                    retString = (retString == null? readString :
                                                   
retString.concat(readString));
                } else {
                    break;
                }
            } else {
                if (count > 0) {
                    break;
                }               
            }
            try {
                Thread.currentThread().sleep(1000);
            } catch (Exception e) {
            }
            n_timer++;
            if (n_timer >= timeoutSeconds) {
                break;
            }
        } while(true);
        return retString;
    }


    public String read() throws IOException{
        byte[] buffer = new byte[2048];
        int count;
        String retString = null;       
        do {
            if (in.available() <= 0) {
                return retString;
            }
            count = in.read(buffer);
            if (count > 0) {
                String readString = new String (buffer,0,count);
                retString = (retString == null? readString :
                                               
retString.concat(readString));
                try {
                    Thread.currentThread().sleep(500);
                } catch (Exception e) {
                }
            } else {
                return retString;               
            }
        } while(true);       
    }

readerr() similar to read()

==8<================================================



-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games.  How far can you shotput
a projector? How fast can you ride your desk chair down the office luge track?
If you want to score the big prize, get to know the little guy.  
Play to win an NEC 61" plasma display: http://www.necitguy.com/?r=20
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.