Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try using <code>ChannelShell</code> by opening <code>shell</code> rather then <code>exec</code>. </p> <p>Following is small code sample for log in and executing file fro remote java class. </p> <pre><code>private Expect4j SSH(String hostname, String username,String password, int port) throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(username, hostname, port); if (password != null) { session.setPassword(password); } Hashtable&lt;String,String&gt; config = new Hashtable&lt;String,String&gt;(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(60000); channel = (ChannelShell) session.openChannel("shell"); Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream()); channel.connect(); return expect; } </code></pre> <p>This method will open up SSH stream to remote server which will be used by expect4j for sending commands. </p> <pre><code>private boolean executeCommands() { boolean isSuccess = true; Closure closure = new Closure() { public void run(ExpectState expectState) throws Exception { buffer.append(expectState.getBuffer());//buffer is string buffer for appending output of executed command expectState.exp_continue(); } }; List&lt;Match&gt; lstPattern = new ArrayList&lt;Match&gt;(); String[] regEx = SSHConstants.linuxPromptRegEx; if (regEx != null &amp;&amp; regEx.length &gt; 0) { synchronized (regEx) { for (String regexElement : regEx) {//list of regx like, :&gt;, /&gt; etc. it is possible command prompts of your remote machine try { RegExpMatch mat = new RegExpMatch(regexElement, closure); lstPattern.add(mat); } catch (MalformedPatternException e) { return false; } catch(Exception e) { return false; } } lstPattern.add(new EofMatch( new Closure() { // should cause entire page to be collected public void run(ExpectState state) { } })); lstPattern.add(new TimeoutMatch(defaultTimeOut, new Closure() { public void run(ExpectState state) { } })); } } try { Expect4j expect = SSH(objConfig.getHostAddress(), objConfig.getUserName(), objConfig.getPassword(), SSHConstants.SSH_PORT); expect.setDefaultTimeout(defaultTimeOut); if(isSuccess) { for(String strCmd : lstCmds) isSuccess = isSuccess(lstPattern,strCmd); } boolean isFailed = checkResult(expect.expect(lstPattern)); return !isFailed; } catch (Exception ex) { return false; } finally { closeConnection(); } } private boolean isSuccess(List&lt;Match&gt; objPattern,String strCommandPattern) { try { boolean isFailed = checkResult(expect.expect(objPattern)); if (!isFailed) { expect.send(strCommandPattern); expect.send("\r"); return true; } return false; } catch (MalformedPatternException ex) { return false; } catch (Exception ex) { return false; } } private boolean checkResult(int intRetVal) { if (intRetVal == SSHConstants.COMMAND_EXECUTION_SUCCESS_OPCODE) { return true; } return false; } public static final int COMMAND_EXECUTION_SUCCESS_OPCODE = -2; public static final String BACKSLASH_R = "\r"; public static final String BACKSLASH_N = "\n"; public static final String COLON_CHAR = ":"; public static String ENTER_CHARACTER = BACKSLASH_R; public static final int SSH_PORT = 22; public static String[] linuxPromptRegEx = new String[]{"~]#","~#","#",":~#","/$",} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload