ExpectIt
ExpectIt copied to clipboard
Ability to flush buffer before expect
I have a use case where I believe the remaining buffer after a matched expect is tripping up my processing for next.
I'm using ExpectIt with SSHJ to handle password expiry. In this use case, against OpenSSH, when the initial connect is made "passwd" is activated server-side.
The issue (for me) is that passwd issues error and instructions lines like:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
New password:
Currently I am matching on "BAD PASSWORD" so that I can feed back info to the user - however if I feed in next entered password in then password responds with
Retype new password:
...which is reasonable, but at this point the buffer is:
The password fails the dictionary check - it is based on a dictionary word
New password:
Retype new password:
...(i.e. with the "BAD PASSWORD" removed and the left over "New password" indicates first password - whereas "Retype new password" indicates second.
So is it possible to add a "flush()" method to allow controlling the buffer before sending the password? I have done this in a cloned version using the following - which works well, but I may not have considered all threading implications and attributes.
In SingleInputExpect
void flush() {
try {
ByteBuffer byteBuffer = ByteBuffer.allocate(BUFFER_SIZE);
int len = source.read(byteBuffer);
buffer.delete(0, buffer.length());
} catch (IOException ex) {
LOG.warning("Error reading source");
}
}
and in ExpectImpl
@Override
public void flush() {
SingleInputExpect[] inputs = getInputs();
for (SingleInputExpect input : inputs) {
input.flush();
}
}
Can you wait for "Retype new password:" after sending the new password on a response to "New password:"?