public void addHostKey(byte[] key) throws JSchException { HostKeyRepository hkr = this.jsch.getHostKeyRepository(); HostKey hk = new HostKey(this.host, key); UserInfo userInfo = new MyUserInfo(); hkr.add(hk, userInfo); log.info("hostkey added"); }
public Session getSession(String host, Integer port, String user, Boolean hostKeyChecking) throws JSchException { if (this.session != null) return this.session; this.setHost(host); this.setUser(user); this.session = jsch.getSession(user, host, port); java.util.Properties config = new java.util.Properties(); if (hostKeyChecking) { log.info("strict host key checking enabled"); config.put("StrictHostKeyChecking", "yes"); } else { log.info("strict host key checking disabled"); config.put("StrictHostKeyChecking", "no"); } session.setConfig(config); return session; }
public void createSession(String host, Integer port, String user, Boolean hostKeyChecking) throws JSchException { log.info("creating session"); this.session = this.getSession(host, port, user, hostKeyChecking); }
@Override public Integer call() { int exitstatus = Integer.MAX_VALUE; Boolean useIdentityFile = null; String credentials = null; String userName = nodeCredentials.getUsername(); if (nodeCredentials instanceof LoginCredentialsPassword) { useIdentityFile = false; credentials = ((LoginCredentialsPassword) nodeCredentials).getPassword(); } else if (nodeCredentials instanceof LoginCredentialsPrivateKey) { useIdentityFile = true; credentials = ((LoginCredentialsPrivateKey) nodeCredentials).getKey().getKeyPath(); } ChannelExec c = null; SshClient ssh = null; try { ssh = new SshClient(); ssh.createSession(nodeAddress, userName, false); log.info("connecting with username: "******"connecting using identity file: " + credentials); } else { log.info("connecting using password: "******"executing command: " + command); c.connect(); new Thread( new Runnable() { public void run() { String line; BufferedReader bufferedInputReader = new BufferedReader(new InputStreamReader(is)); try { while ((line = bufferedInputReader.readLine()) != null) { output.append(line); synchronized (timeStamp) { timeStamp = System.currentTimeMillis(); } } } catch (IOException e) { e.printStackTrace(); } } }) .start(); new Thread( new Runnable() { public void run() { String line; BufferedReader bufferedErrorReader = new BufferedReader(new InputStreamReader(err)); try { while ((line = bufferedErrorReader.readLine()) != null) { error.append(line); synchronized (timeStamp) { timeStamp = System.currentTimeMillis(); } } } catch (IOException e) { e.printStackTrace(); } } }) .start(); while (!c.isClosed()) { synchronized (this.timeStamp) { if (System.currentTimeMillis() - this.timeStamp > MAXIMUM_WAITING_TIME_INACTIVITY) { log.warn("command execution seems inactive, canceling!"); break; } } log.info("waiting for command to finish."); try { Thread.sleep(SshClient.DEFAULT_WAITING_TIME_PER_CYCLE); } catch (InterruptedException e) { e.printStackTrace(); } } exitstatus = c.getExitStatus(); } catch (JSchException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (c != null) { c.disconnect(); } if (ssh != null) { try { ssh.disconnectSession(); } catch (JSchException e) { e.printStackTrace(); } } } return exitstatus; }
public ChannelSftp getSftpChannel() throws JSchException { Channel channel = session.openChannel("sftp"); log.info("opening sftp channel"); return (ChannelSftp) channel; }
public ChannelShell getShellChannel() throws JSchException { Channel channel = session.openChannel("shell"); log.info("opening shell channel"); return (ChannelShell) channel; }
public ChannelExec getExecutionChannel() throws JSchException { Channel channel = session.openChannel("exec"); log.info("opening execution channel"); return (ChannelExec) channel; }
public void disconnectSession() throws JSchException { log.info("disconnecting session"); this.session.disconnect(); }
public void setSessionPassword(String pw) { log.info("password set"); this.session.setPassword(pw); }