Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JSCh input and output issues
- (expecting) Destination:
- (send) XXXXXXXr
- (expecting)Several lines of code finishing with Connected to etc. via etc.rn
- package switchoverlay;
- import com.jcraft.jsch.Channel;
- 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.io.ByteArrayInputStream;
- import java.io.InputStream;
- 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); //Get first three digits from tn
- String nxx = OverlayGUI.tn.substring(5, 8); //Get first three digits from tn
- String xxxx = OverlayGUI.tn.substring(9, 13); //Get last four digits from tn
- private static final int COMMAND_EXECUTION_SUCCESS_OPCODE = -2;
- private static String ENTER_CHARACTER = "r"; //NOT GOOD FOR A GTD5!
- private static final int SSH_PORT = 22;
- private List<String> loginLstCmds = new ArrayList<String>();
- //private static String[] switchPromptRegEx = new String[]
- //{"via.{12}rn", //Connected, send ctrl+B
- //"I@\d\d:\d\d:\d\d u0003"}; //disconnect
- private Expect4j expect = null;
- private StringBuilder loginBuffer = new StringBuilder();
- private StringBuilder firstBuffer = new StringBuilder();
- private String userName;
- private String password;
- private String host;
- /**
- *
- * @param host
- * @param userName
- * @param password
- */
- public SSHLogin(String host, String userName, String password) {
- this.host = host;
- this.userName = userName;
- this.password = password;
- }
- /**
- *
- * @param LoginCmdsToExecute
- */
- 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 : OverlayGUI.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 {
- closeConnection();
- }
- return loginBuffer.toString();
- }
- /**
- *
- * @param objPattern
- * @param strCommandPattern
- * @return
- */
- private boolean isSuccess(List<Match> objPattern,String strCommandPattern) {
- try {
- boolean isFailed = checkResult(expect.expect(objPattern));
- if (!isFailed) {
- expect.send(strCommandPattern);
- Thread.sleep( 4000);
- //expect.send(ENTER_CHARACTER); //NOT GOOD FOR A GTD5!
- return true;
- }
- return false;
- } catch (MalformedPatternException ex) {
- ex.printStackTrace();
- return false;
- } catch (Exception ex) {
- ex.printStackTrace();
- return false;
- }
- }
- /**
- *
- * @param hostname
- * @param username
- * @param password
- * @param port
- * @return
- * @throws Exception
- */
- 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");
- Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
- channel.connect();
- Closure loginClosure = new Closure() {
- public void run(ExpectState loginExpectState) throws Exception {
- firstBuffer.append(loginExpectState.getBuffer());
- }
- };
- expect.expect("rnDestination: ", loginClosure);
- expect.send(npa+nxx+ENTER_CHARACTER); //logged in to switch, now use regex to find switch type.
- expect.expect("via.{12}rn", loginClosure);
- //expect.send("u0003");
- System.out.println("Printing firstBuffer...");
- System.out.println(firstBuffer);
- return expect;
- }
- /**
- *
- * @param intRetVal
- * @return
- */
- private boolean checkResult(int intRetVal) {
- if (intRetVal == COMMAND_EXECUTION_SUCCESS_OPCODE) {
- return true;
- }
- return false;
- }
- /**
- *
- */
- private void handOffConnection() {
- Pattern gtd5 = Pattern.compile(".*GTD5.*");
- Matcher gtd5Matcher = gtd5.matcher(loginBuffer);
- if (gtd5Matcher.find()) {
- System.out.println("Your switch type is GTD5.");
- OverlayGUI.switchType="GTD5";
- if (!OverlayGUI.GUISwitchRadio.equals("GTD5")) {
- OverlayGUI.GTD5RadioButton.setSelected(true);
- OverlayGUI.ExamTNButton.doClick();
- }
- //if switch type in gui is not gtd5, make radiobutton gtd5 and rerun exam tn button,
- //get switch code, and move on to remaining expect code,
- //else move on to remaining expect code.
- }
- Pattern dms10 = Pattern.compile(".*DMS10^0.*");
- Matcher dms10Matcher = dms10.matcher(loginBuffer);
- if (dms10Matcher.find()) {
- System.out.println("Your switch type is DMS10.");
- OverlayGUI.switchType="DMS10";
- }
- Pattern dms100 = Pattern.compile(".*DMS100.*");
- Matcher dms100Matcher = dms100.matcher(loginBuffer);
- if (dms100Matcher.find()) {
- System.out.println("Your switch type is DMS100.");
- OverlayGUI.switchType="DMS100";
- }
- Pattern ess = Pattern.compile(".*5es.*");
- Matcher essMatcher = ess.matcher(loginBuffer);
- if (essMatcher.find()) {
- System.out.println("Your switch type is 5ESS.");
- OverlayGUI.switchType="5ESS";
- }
- Pattern dco = Pattern.compile(".*DCO.*");
- Matcher dcoMatcher = dco.matcher(loginBuffer);
- if (dcoMatcher.find()) {
- System.out.println("Your switch type is DCO.");
- OverlayGUI.switchType="DCO";
- }
- //else { System.out.println("Switch type Error."); }
- //SSHGTD5ExamClient runSsh = new SSHGTD5ExamClient(give args!);
- }
- private void closeConnection() {
- if (expect!=null) {
- expect.close();
- }
- }
- }
- public class SSHRunner implements Runnable {
- private volatile Thread runnerThread;
- private volatile boolean stopRequested = false;
- public void start() {
- runnerThread = new Thread(this);
- runnerThread.start();
- System.out.println("thread started.");
- }
- public void run() {
- while (!stopRequested) {
- System.out.println("run started.");
- SSHLogin ssh = new SSHLogin(IPComboBox.getSelectedItem().toString(), "v735587", "kasper1");
- String outputLog = ssh.execute(loginCmdsToExecute);
- ReadoutArea.setText(outputLog);
- LaunchButton.setText("Launch switch and post code");
- LaunchButton.setEnabled(true);
- SwitchCodeExecuteButton.setText("Execute");
- SwitchCodeExecuteButton.setEnabled(true);
- CodeArmed = false;
- requestStop();
- }
- }
- public void requestStop() {
- stopRequested = true;
- System.out.println("thread stopping.");
- SwitchCodeField.append("Commands executed, see Switch tab for results.n");
- }
- }
- public class CodePrinter {
- public void print() {
- for ( String x: loginCmdsToExecute )
- SwitchCodeField.append(x+"n");
- CodeArmed = true;
- SwitchCodeField.append("----------------------------------------n");
- }
- }
Add Comment
Please, Sign In to add comment