--
You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thank you for the quick reply. That does look like it would do exactly what I want. However when I try to put it into my Method, Eclipse throws an unhandled IOexception error. The method is set up to handle IOExceptions and I tried to surround with a try/catch but eclipse keeps suggesting to surround with a try/catch regardless.
I have only been coding for a few months so I do not have much experiance in handing these situations.
Here is my full Method if it helps. Please excuse the sloppy code.
public static List<String> SSHConnect(List<String> commands, String IPAddress, String Username, String Password, String EnablePassword) throws JSchException, IOException, InterruptedException {
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) {
Responses.add("# Unable to connect to: " + IPAddress + "\n" );
//e.printStackTrace();
String error = e.toString();
if(error.contains("Auth fail")){
Responses.add("# Incorrect Username or Password\n");
}
return Responses;
}
Channel channel = session.openChannel("shell");
channel.connect();
Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoInput(wholeBuffer)
.withEchoOutput(System.err)
.withInputFilters(removeColors(), removeNonPrintable())
.withExceptionOnFailure()
.build();
try {
expect.expect(contains("."));
expect.sendLine("sh run");
expect.interact()
.when(contains("MORE"))
.then(r -> expect.sendLine(" "))
.until(contains("#"));
commands.add("exit");
commands.add("exit");
commands.add("sh sip trunk reg");
commands.add("sh voice users");
commands.add("wr");
for(int i = 0; i < commands.size(); ++i){
expect.sendLine(commands.get(i));
}
//Thread.sleep(8500);
//expect.expect(allOf(regexp("xyz"), regexp("abc.*def")));;
String response = wholeBuffer.toString();
//Make sure the enable password was correct
if(response.contains("% Incorrect password")){
Responses.add("# Incorrect Enable mode password");
return Responses;
}
System.out.println("wholebuffer: ");
System.out.println(response);
String UserReg = "";
String TrunkReg = "";
int begin = response.indexOf("sh sip trunk reg");
int end = response.indexOf("Total Displayed");
if (begin > 1){
TrunkReg = response.substring(begin - 16, end + 19);
System.out.println(TrunkReg);
}
begin = response.indexOf("sh voice users");
end = response.indexOf("Total number of configured voice users:");
if (begin > 1){
UserReg = response.substring(begin - 16, end + 43);
System.out.println(UserReg);
}
Responses.add(TrunkReg);
Responses.add(UserReg);
//expect.expect(allOf(regexp("xyz"), regexp("abc.*def")));
return Responses;
//expect.expect(contains(">"));
//expect.expect(times(5, contains("\n")))
} finally {
expect.close();
channel.disconnect();
session.disconnect();
}
}
}
expect.expect(contains("."));
expect.sendLine("en");
expect.sendLine(enablepassword);
expect.sendLine("sh run");
expect.interact()
.when(contains("--MORE--"))
.then(r -> {
try {
expect.sendLine(" ");
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.until(contains("VoIPLab-TA916e#"));
It still sits there, with the System.out showing:
VoIPLab-TA916e#sh run
Building configuration...
!
!
! ADTRAN, Inc. OS version R11.10.0.E
! Boot ROM version 14.05.00.SA
! Platform: Total Access 916e (2nd Gen), part number 4242916L1
! Serial number SN
!
!
hostname "VoIPLab-TA916e"
enable password encrypted 272c6dbc90ad8c576186a407084
!
!
clock timezone -5-Eastern-Time
!
ip subnet-zero
ip classless
ip routing
ipv6 unicast-routing
!
!
name-server 8.8.8.8
!
--MORE--
expect.expect(contains("."));
expect.sendLine("en");
expect.sendLine(enablepassword);
expect.sendLine("sh run");
expect.interact()
.when(contains("."))
.then(r -> {
System.out.println("Match was found");
})
.until(contains("VoIPLab-TA916e"));
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
Match was found
VoIPLab-TA916e>en
Password:
VoIPLab-TA916e#sh run
Building configuration...
!
!
! ADTRAN, Inc. OS version R11.10.0.E
! Boot ROM version 14.05.00.SA
! Platform: Total Access 916e (2nd Gen), part number 4242916L1
! Serial number SN
!
!
hostname "VoIPLab-TA916e"
enable password encrypted 272c6dbca407084
Alexey,
Thank you for your guidance on this. I was able to get this to work thanks to your explanation.
For whatever reason I could not get expect.expect(contains("MORE")); to accurately trigger
Using regexp to match string works perfectly
expect.interact()
.when(regexp("\\bMORE\\b"))
.then(r -> {
try {
expect.sendLine(" ");
//expect.sendLine(" ");
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.until(regexp("\\bend\\b"));
Thank you. I have thrown that command and everything now works flawlessly.
Alexey,
I have not had any issues with the regex MORE matching. It correctly disengages the interact loop when "end" is observed. The method I created is only for show run and I haven't tested how it would respond if I was doing more than just a show run