I wrote a program that will go in via SSH. This server takes an additional step in that you need to provide the area code and three digits of the phone number when it asks for "Destination:". So what I'm trying to do is to let my terminal wait for the Destination prompt: give it six digits, and then look at the prompts that appear to see where it went before executing the wait code. Following the Destination: prompt, there are five possible types of term, which means I need to test the returned stream after giving it six digits of the phone number using a regular expression.
Where I am stuck, the best way to get a Destination invitation is found out: send it my numbers, get the received code to execute my regular expression, and if then - statements, and then pass it to one of several classes I will do to execute using expect4j from this moment. Do I have to use the exec shell before moving on to waiting, and if so, how do I get input from it to run the regular expression? Should I send expect.expect ("Direction:"); followed by expect.send ("123456"); then get input to run the regex, and if so, how do I get input from it? (my code sample uses a buffer that somehow gets information from the execute method, I really donβt understand how it works completely),or, the last parameter, should I create two instances of my sample code that makes the session and the ssh channel, send my phone numbers, get input, run the regular expression, then send it to another expect4j class with new variables to start the remaining expectations and send?
SHORT: As far as I can tell, these are my three options: run the exec shell, run the single file expect.expect and expect.send before going into a more complex list building and, or running two lists using the wait methods one by one . Not sure where to go, and how to return my data to run a regular expression.
I managed to get him to enter one of my five terminal parameters, check and disconnect, and recently I created a code to enter the system, put in the numbers and read the received code with a regular expression to indicate which of my five types. I just can't get them to work together.
My code is built from an example found at http://nikunjp.wordpress.com/2011/07/30/remote-ssh-using-jsch-with-expect4j/
package switchoverlay;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import expect4j.Closure;
import expect4j.Expect4j;
import expect4j.ExpectState;
import expect4j.matches.Match;
import expect4j.matches.RegExpMatch;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.oro.text.regex.MalformedPatternException;
public class SSHLogin {
static final String controlB = "\u0002";
static final String controlC = "\u0003";
static final String controlV = "\u0016";
static final String cr = "\u0013";
static final String lf = "\u0010";
static final String sp = "\u0032";
String npa = OverlayGUI.tn.substring(1, 4);
String nxx = OverlayGUI.tn.substring(5, 8);
String xxxx = OverlayGUI.tn.substring(9, 13);
private static final int COMMAND_EXECUTION_SUCCESS_OPCODE = -2;
private static String ENTER_CHARACTER = "\r";
private static final int SSH_PORT = 22;
private List<String> loginLstCmds = new ArrayList<String>();
private static String[] switchPromptRegEx = new String[]
{"\r\nDestination: ",
"via.{12}\r\n"};
private Expect4j expect = null;
private StringBuilder loginBuffer = new StringBuilder();
private String userName;
private String password;
private String host;
public SSHLogin(String host, String userName, String password) {
this.host = host;
this.userName = userName;
this.password = password;
}
public String execute(List<String> LoginCmdsToExecute) {
this.loginLstCmds = LoginCmdsToExecute;
Closure closure = new Closure() {
public void run(ExpectState expectState) throws Exception {
loginBuffer.append(expectState.getBuffer());
}
};
List<Match> lstPattern = new ArrayList<Match>();
for (String regexElement : switchPromptRegEx) {
try {
Match mat = new RegExpMatch(regexElement, closure);
lstPattern.add(mat);
} catch (MalformedPatternException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
try {
expect = SSH();
boolean isSuccess = true;
for(String strCmd : loginLstCmds) {
isSuccess = isSuccess(lstPattern,strCmd);
if (!isSuccess) {
isSuccess = isSuccess(lstPattern,strCmd);
}
}
checkResult(expect.expect(lstPattern));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
testType();
}
return loginBuffer.toString();
}
private boolean isSuccess(List<Match> objPattern,String strCommandPattern) {
try {
boolean isFailed = checkResult(expect.expect(objPattern));
if (!isFailed) {
expect.send(strCommandPattern);
Thread.sleep( 8000);
return true;
}
return false;
} catch (MalformedPatternException ex) {
ex.printStackTrace();
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
private Expect4j SSH() throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(userName, host, SSH_PORT);
if (password != null) {
session.setPassword(password);
}
Hashtable<String,String> config = new Hashtable<String,String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(120000);
ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.connect();
Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
return expect;
}
private boolean checkResult(int intRetVal) {
if (intRetVal == COMMAND_EXECUTION_SUCCESS_OPCODE) {
return true;
}
return false;
}
private void testType() {
String lBString = loginBuffer.toString();
System.out.println(lBString);
Pattern gtd5 = Pattern.compile(".*GTD5.*");
Matcher gtd5Matcher = gtd5.matcher(lBString);
if (gtd5Matcher.find()) {
System.out.println("Your switch type is GTD5");
}
Pattern dms10 = Pattern.compile(".*DMS10^0.*");
Matcher dms10Matcher = dms10.matcher(lBString);
if (dms10Matcher.find()) {
System.out.println("Your switch type is DMS10");
}
Pattern dms100 = Pattern.compile(".*DMS100.*");
Matcher dms100Matcher = dms100.matcher(lBString);
if (dms100Matcher.find()) {
System.out.println("Your switch type is DMS100");
}
Pattern ess = Pattern.compile(".*5es.*");
Matcher essMatcher = ess.matcher(lBString);
if (essMatcher.find()) {
System.out.println("Your switch type is 5ESS");
}
Pattern dco = Pattern.compile(".*DCO.*");
Matcher dcoMatcher = dco.matcher(lBString);
if (dcoMatcher.find()) {
System.out.println("Your switch type is DCO");
}
}
}