Guest User

Untitled

a guest
Sep 10th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.06 KB | None | 0 0
  1. JSCh input and output issues
  2. (expecting) Destination:
  3. (send) XXXXXXXr
  4. (expecting)Several lines of code finishing with Connected to etc. via etc.rn
  5.  
  6. package switchoverlay;
  7.  
  8. import com.jcraft.jsch.Channel;
  9. import com.jcraft.jsch.ChannelShell;
  10. import com.jcraft.jsch.JSch;
  11. import com.jcraft.jsch.Session;
  12. import expect4j.Closure;
  13. import expect4j.Expect4j;
  14. import expect4j.ExpectState;
  15. import expect4j.matches.Match;
  16. import expect4j.matches.RegExpMatch;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.InputStream;
  19. import java.util.ArrayList;
  20. import java.util.Hashtable;
  21. import java.util.List;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. import org.apache.oro.text.regex.MalformedPatternException;
  25.  
  26.  
  27.  
  28. public class SSHLogin {
  29.  
  30. static final String controlB = "u0002";
  31. static final String controlC = "u0003";
  32. static final String controlV = "u0016";
  33. static final String cr = "u0013";
  34. static final String lf = "u0010";
  35. static final String sp = "u0032";
  36.  
  37. String npa = OverlayGUI.tn.substring(1, 4); //Get first three digits from tn
  38. String nxx = OverlayGUI.tn.substring(5, 8); //Get first three digits from tn
  39. String xxxx = OverlayGUI.tn.substring(9, 13); //Get last four digits from tn
  40.  
  41. private static final int COMMAND_EXECUTION_SUCCESS_OPCODE = -2;
  42. private static String ENTER_CHARACTER = "r"; //NOT GOOD FOR A GTD5!
  43. private static final int SSH_PORT = 22;
  44. private List<String> loginLstCmds = new ArrayList<String>();
  45. //private static String[] switchPromptRegEx = new String[]
  46. //{"via.{12}rn", //Connected, send ctrl+B
  47. //"I@\d\d:\d\d:\d\d u0003"}; //disconnect
  48. private Expect4j expect = null;
  49. private StringBuilder loginBuffer = new StringBuilder();
  50. private StringBuilder firstBuffer = new StringBuilder();
  51. private String userName;
  52. private String password;
  53. private String host;
  54.  
  55.  
  56. /**
  57. *
  58. * @param host
  59. * @param userName
  60. * @param password
  61. */
  62. public SSHLogin(String host, String userName, String password) {
  63. this.host = host;
  64. this.userName = userName;
  65. this.password = password;
  66. }
  67. /**
  68. *
  69. * @param LoginCmdsToExecute
  70. */
  71. public String execute(List<String> LoginCmdsToExecute) {
  72. this.loginLstCmds = LoginCmdsToExecute;
  73.  
  74. Closure closure = new Closure() {
  75. public void run(ExpectState expectState) throws Exception {
  76. loginBuffer.append(expectState.getBuffer());
  77. }
  78. };
  79.  
  80. List<Match> lstPattern = new ArrayList<Match>();
  81. for (String regexElement : OverlayGUI.switchPromptRegEx) {
  82. try {
  83. Match mat = new RegExpMatch(regexElement, closure);
  84. lstPattern.add(mat);
  85. } catch (MalformedPatternException e) {
  86. e.printStackTrace();
  87. } catch(Exception e) {
  88. e.printStackTrace();
  89. }
  90. }
  91.  
  92. try {
  93. expect = SSH();
  94. boolean isSuccess = true;
  95.  
  96. for(String strCmd : loginLstCmds) {
  97. isSuccess = isSuccess(lstPattern,strCmd);
  98. if (!isSuccess) {
  99. isSuccess = isSuccess(lstPattern,strCmd);
  100. }
  101. }
  102.  
  103. checkResult(expect.expect(lstPattern));
  104. } catch (Exception ex) {
  105. ex.printStackTrace();
  106. } finally {
  107. closeConnection();
  108. }
  109. return loginBuffer.toString();
  110. }
  111. /**
  112. *
  113. * @param objPattern
  114. * @param strCommandPattern
  115. * @return
  116. */
  117. private boolean isSuccess(List<Match> objPattern,String strCommandPattern) {
  118. try {
  119. boolean isFailed = checkResult(expect.expect(objPattern));
  120.  
  121. if (!isFailed) {
  122.  
  123. expect.send(strCommandPattern);
  124. Thread.sleep( 4000);
  125. //expect.send(ENTER_CHARACTER); //NOT GOOD FOR A GTD5!
  126. return true;
  127. }
  128. return false;
  129. } catch (MalformedPatternException ex) {
  130. ex.printStackTrace();
  131. return false;
  132. } catch (Exception ex) {
  133. ex.printStackTrace();
  134. return false;
  135. }
  136. }
  137. /**
  138. *
  139. * @param hostname
  140. * @param username
  141. * @param password
  142. * @param port
  143. * @return
  144. * @throws Exception
  145. */
  146. private Expect4j SSH() throws Exception {
  147. JSch jsch = new JSch();
  148. Session session = jsch.getSession(userName, host, SSH_PORT);
  149. if (password != null) {
  150. session.setPassword(password);
  151. }
  152. Hashtable<String,String> config = new Hashtable<String,String>();
  153. config.put("StrictHostKeyChecking", "no");
  154. session.setConfig(config);
  155. session.connect(120000);
  156. ChannelShell channel = (ChannelShell) session.openChannel("shell");
  157. Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
  158. channel.connect();
  159.  
  160.  
  161.  
  162. Closure loginClosure = new Closure() {
  163. public void run(ExpectState loginExpectState) throws Exception {
  164. firstBuffer.append(loginExpectState.getBuffer());
  165. }
  166. };
  167.  
  168.  
  169. expect.expect("rnDestination: ", loginClosure);
  170. expect.send(npa+nxx+ENTER_CHARACTER); //logged in to switch, now use regex to find switch type.
  171. expect.expect("via.{12}rn", loginClosure);
  172. //expect.send("u0003");
  173.  
  174.  
  175. System.out.println("Printing firstBuffer...");
  176. System.out.println(firstBuffer);
  177.  
  178.  
  179.  
  180. return expect;
  181. }
  182. /**
  183. *
  184. * @param intRetVal
  185. * @return
  186. */
  187. private boolean checkResult(int intRetVal) {
  188. if (intRetVal == COMMAND_EXECUTION_SUCCESS_OPCODE) {
  189. return true;
  190. }
  191. return false;
  192. }
  193. /**
  194. *
  195. */
  196. private void handOffConnection() {
  197.  
  198. Pattern gtd5 = Pattern.compile(".*GTD5.*");
  199. Matcher gtd5Matcher = gtd5.matcher(loginBuffer);
  200. if (gtd5Matcher.find()) {
  201. System.out.println("Your switch type is GTD5.");
  202. OverlayGUI.switchType="GTD5";
  203.  
  204. if (!OverlayGUI.GUISwitchRadio.equals("GTD5")) {
  205. OverlayGUI.GTD5RadioButton.setSelected(true);
  206. OverlayGUI.ExamTNButton.doClick();
  207. }
  208. //if switch type in gui is not gtd5, make radiobutton gtd5 and rerun exam tn button,
  209. //get switch code, and move on to remaining expect code,
  210. //else move on to remaining expect code.
  211. }
  212.  
  213. Pattern dms10 = Pattern.compile(".*DMS10^0.*");
  214. Matcher dms10Matcher = dms10.matcher(loginBuffer);
  215. if (dms10Matcher.find()) {
  216. System.out.println("Your switch type is DMS10.");
  217. OverlayGUI.switchType="DMS10";
  218. }
  219.  
  220. Pattern dms100 = Pattern.compile(".*DMS100.*");
  221. Matcher dms100Matcher = dms100.matcher(loginBuffer);
  222. if (dms100Matcher.find()) {
  223. System.out.println("Your switch type is DMS100.");
  224. OverlayGUI.switchType="DMS100";
  225. }
  226.  
  227. Pattern ess = Pattern.compile(".*5es.*");
  228. Matcher essMatcher = ess.matcher(loginBuffer);
  229. if (essMatcher.find()) {
  230. System.out.println("Your switch type is 5ESS.");
  231. OverlayGUI.switchType="5ESS";
  232. }
  233.  
  234. Pattern dco = Pattern.compile(".*DCO.*");
  235. Matcher dcoMatcher = dco.matcher(loginBuffer);
  236. if (dcoMatcher.find()) {
  237. System.out.println("Your switch type is DCO.");
  238. OverlayGUI.switchType="DCO";
  239.  
  240.  
  241. }
  242.  
  243.  
  244. //else { System.out.println("Switch type Error."); }
  245.  
  246.  
  247. //SSHGTD5ExamClient runSsh = new SSHGTD5ExamClient(give args!);
  248.  
  249. }
  250.  
  251. private void closeConnection() {
  252. if (expect!=null) {
  253. expect.close();
  254. }
  255. }
  256.  
  257. }
  258.  
  259. public class SSHRunner implements Runnable {
  260. private volatile Thread runnerThread;
  261. private volatile boolean stopRequested = false;
  262.  
  263. public void start() {
  264. runnerThread = new Thread(this);
  265. runnerThread.start();
  266. System.out.println("thread started.");
  267. }
  268.  
  269. public void run() {
  270. while (!stopRequested) {
  271. System.out.println("run started.");
  272. SSHLogin ssh = new SSHLogin(IPComboBox.getSelectedItem().toString(), "v735587", "kasper1");
  273. String outputLog = ssh.execute(loginCmdsToExecute);
  274. ReadoutArea.setText(outputLog);
  275. LaunchButton.setText("Launch switch and post code");
  276. LaunchButton.setEnabled(true);
  277. SwitchCodeExecuteButton.setText("Execute");
  278. SwitchCodeExecuteButton.setEnabled(true);
  279. CodeArmed = false;
  280. requestStop();
  281. }
  282. }
  283.  
  284. public void requestStop() {
  285. stopRequested = true;
  286. System.out.println("thread stopping.");
  287. SwitchCodeField.append("Commands executed, see Switch tab for results.n");
  288. }
  289. }
  290.  
  291.  
  292. public class CodePrinter {
  293.  
  294. public void print() {
  295.  
  296.  
  297. for ( String x: loginCmdsToExecute )
  298. SwitchCodeField.append(x+"n");
  299. CodeArmed = true;
  300. SwitchCodeField.append("----------------------------------------n");
  301. }
  302.  
  303. }
Add Comment
Please, Sign In to add comment