I have revered to using the match all regex but that seems to not work all the time as well. I guess I just don't understand how it works.
Below I can get the wall announcement to work after I enter enable mode(cpmmands in the if(Hostname.contains(">")){). However I can not get the output from the for loop after. I know the for loop is working because I see the red test in the Eclipse terminal.
public static void SSHConnect(List<String> commands, String IPAddress, String Username, String Password,
String EnablePassword) throws IOException, InterruptedException, JSchException {
StringBuilder wholeBuffer = new StringBuilder();
List<String> Responses = new ArrayList<String>();
JSch jSch = new JSch();
Session session = jSch.getSession(Username, IPAddress, 22);
session.setPassword(Password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
//Attempt to connect. if unable catch exception and return response
try {
session.connect();
} catch (JSchException e) {
e.printStackTrace();
String error = e.toString();
if(error.contains("UnknownHostException")){
CPE.infoBox("Unable to connect to: " + IPAddress + "\n", "Connection Error" );
}
if(error.contains("Auth fail")){
e.printStackTrace();
CPE.infoBox("Authentication failed for: " + IPAddress + "\n", "Connection Error" );
}
return;
}
Channel channel = session.openChannel("shell");
channel.connect();
Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoInput(System.out)
.withEchoInput(wholeBuffer)
.withEchoOutput(System.err)
.withInputFilters(removeColors(), removeNonPrintable())
.withExceptionOnFailure()
.build();
try {
Thread.sleep(500);
//Wait a little bit for the buffer to fill. then convert to init
String init = wholeBuffer.toString();
//Grab the Hostname so we can tell if we are in enabled mode, pos +1 because we dont actually want the new line char
String Hostname = init.substring(init.lastIndexOf("\n") + 1, init.length());
//If Hostname contains > we need to enable. if not we can go straight to entering commands
if(Hostname.contains(">")){
expect.expect(contains(">"));
expect.sendLine("enable");
expect.expect(contains("Password:"));
expect.sendLine(EnablePassword);
expect.expect(regexp((".*")));
expect.sendLine("Wall this works");
expect.sendLine();
}
Thread.sleep(500);
init = wholeBuffer.toString();
if(init.contains("% Incorrect password")){
CPE.infoBox("The enable password was incorrect", "Bad Enable Password");
}
expect.expect(regexp((".*")));
commands.add("wall why doesnt this work?");
for(int i = 0; i < commands.size(); ++i){
expect.sendLine(commands.get(i));
}
} finally {
expect.close();
channel.disconnect();
session.disconnect();
}
}
Additionally something like this does not work as well
commands.add("wall what doesnt this work?");
expect.expect(anyString());
if(Hostname.contains(">")){
expect.expect(contains(">"));
expect.sendLine("enable");
expect.expect(contains("Password:"));
expect.sendLine(EnablePassword);
expect.expect(regexp((".*")));
expect.sendLine("Wall works");
expect.sendLine();
}
commands.add("wall test from loop 1");
commands.add("wall test from loop 2");
commands.add("wall test from loop 3");
commands.add("wall test from loop 4");
commands.add("wall test from loop 5");
for(int i = 0; i < commands.size(); ++i){
expect.expect(regexp((".*")));
expect.sendLine(commands.get(i));
}
In the router I am only getting the following:
VoIPLab-TA916e#Broadcast (WALL) from: SSH 1 (MY IP:52254)
works
Broadcast (WALL) from: SSH 1 (MY IP:52254)
test from loop 1
Broadcast (WALL) from: SSH 1 (MY IP:52254)
test from loop 2
if(Hostname.contains(">")){
expect.expect(contains(">"));
expect.sendLine("enable");
expect.expect(contains("Password:"));
expect.sendLine(EnablePassword);
expect.expect(regexp((".*")));
expect.sendLine("Wall works");
}
//Thread.sleep(500);
//init = wholeBuffer.toString();
//if(init.contains("% Incorrect password")){
//CPE.infoBox("The enable password was incorrect", "Bad Enable Password");
//}
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
expect.expect(regexp((".*")));
if(Hostname.contains(">")){
expect.expect(contains(">"));
expect.sendLine("enable");
expect.expect(contains("Password:"));
expect.sendLine(EnablePassword);
expect.expect(regexp((".*")));
expect.sendLine("Wall works");
expect.sendLine();
}
//These Successfully get sent to the router
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
expect.expect(contains("#"));
for(int i = 0; i < commands.size(); ++i){
expect.sendLine(commands.get(i));
}
Thread.sleep(500);
init = wholeBuffer.toString();
if(init.contains("% Incorrect password")){
CPE.infoBox("The enable password was incorrect", "Bad Enable Password");
}
//These do not get sent to the router
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
expect.expect(contains("#"));
if(Hostname.contains(">")){
expect.expect(contains(">"));
expect.sendLine("enable");
expect.expect(contains("Password:"));
expect.sendLine(EnablePassword);
expect.expect(regexp((".*")));
expect.sendLine("Wall works");
expect.sendLine();
}
//These do not get sent to the router
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
expect.expect(contains("#"));
for(int i = 0; i < commands.size(); ++i){
expect.sendLine(commands.get(i));
}
//These do not get sent to the router
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
commands.add("wall test from loop");
expect.expect(contains("#"));