java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); System.out.println("Host connected.");
java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.setPassword(password); session.connect();
import com.jcraft.jsch.*; public class TestJSch { public static void main(String args[]) { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("username", "127.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("password"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.get("remotefile.txt", "localfile.txt"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } }
java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config);
public static void main(String args[]) throws JSchException, InterruptedException, IOException { JSch objJSch = new JSch(); Session objSession = objJSch.getSession("userName", "ip_address"); objSession.setPassword("password"); objSession.setConfig("StrictHostKeyChecking", "no"); System.out.println("Establishing Connection..."); objSession.connect(); System.out.println("Connection established."); Channel objChannel = objSession.openChannel("exec"); ((ChannelExec) objChannel).setCommand("ps aux"); InputStream in = objChannel.getInputStream(); objChannel.connect(); String s; BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((s = br.readLine()) != null) System.out.println(s); System.out.println("disconnecting..."); objChannel.disconnect(); objSession.disconnect(); System.out.println("disconnected."); }
Session session = new JSch().getSession(user, hostName, 22); session.setPassword(password); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand("some command here"); String result = IOUtils.toString(channel.getInputStream()); channel.disconnect(); session.disconnect();
final JSch jsch = new JSch(); final Session sessionJSH = jsch.getSession(user, host, 22); sessionJSH.setPassword(pwd); final Hashtable configJSH = new Hashtable(); configJSH.put("StrictHostKeyChecking", "no"); sessionJSH.setConfig(configJSH); sessionJSH.connect(); final Channel channel = sessionJSH.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.connect(0); // we'll hold it until 30 minutes = 30m*60s*1000ms/300ms = 6000 times int i = 0; while (channel.getExitStatus() == -1 && i < 6000) { log.debug("Exit status" + channel.getExitStatus()); Thread.sleep(300); i++; } log.info("Exit status" + channel.getExitStatus()); channel.disconnect(); sessionJSH.disconnect();
JSch jSch = new JSch(); Session session = jSch.getSession("new", "sdf.org"); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel("shell"); Expect expect = new ExpectBuilder() .withOutput(channel.getOutputStream()) .withInputs(channel.getInputStream(), channel.getExtInputStream()) .withEchoOutput(adapt(System.out)) // .withInputFilters(removeColors(), removeNonPrintable()) .withErrorOnTimeout(true) .build(); // try-with-resources is omitted for simplicity channel.connect(); expect.expect(contains("[RETURN]")); expect.sendLine(); String ipAddress = expect.expect(regexp("Trying (.*)\\.\\.\\.")).group(1); System.out.println("Captured IP: " + ipAddress); expect.expect(contains("login:")); expect.sendLine("new"); expect.expect(contains("(Y/N)")); expect.send("N"); expect.expect(regexp(": $")); // finally is omitted channel.disconnect(); session.disconnect(); expect.close();
JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("user", "hostname", 22); // default UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.setPassword("password".getBytes()); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); System.out.println("Connected"); } catch (JSchException e) { e.printStackTrace(System.out); } catch (Exception e) { e.printStackTrace(System.out); } finally { session.disconnect(); System.out.println("Disconnected"); } }
new Thread(new Runnable() { @Override public void run() { Session session; JSch jsch; try { jsch = new JSch(); jsch.addIdentity(privateKey, "yourprivatekey"); session = jsch.getSession("git", "github.com", 22); session.setPassword("yourpass"); // Avoid asking for key confirmation Properties prop = new Properties(); prop.put("StrictHostKeyChecking", "no"); session.setConfig(prop); session.connect(); if(session.isConnected()){ System.out.println(this.getClass().getSimpleName() + " CONNECTED"); System.out.println(this.getClass().getSimpleName() + " YOO " + jsch.getIdentityRepository().getName()+" "+session.getClientVersion() + " " + session.isConnected()); }else{ System.out.println(this.getClass().getSimpleName() + " NOT CONNECTED"); } }catch (Exception e){ e.printStackTrace(); } } }).start();
JSch jsch = new JSch(); Session session = jsch.getSession(user, host); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(timeout); Channel channel = session.openChannel("exec"); ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); channel.connect(); // read channel.getInputStream() here if you want to capture the output channel.disconnect(); session.disconnect();
Session session = jsch.getSession(user, host, 22); session.setPassword(password); session.setConfig(config); session.connect(); System.out.println("Connected");
session.setConfig(config);
Thread connectionThread = new Thread(new Runnable() { public void run() { JSch jsch = new JSch(); Session session = null; session = jsch.getSession("******", "*********",22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("******"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.exit(); session.disconnect(); } });
JSch jsch = new JSch(); Session session = null; session = jsch.getSession("***", "***.***.***.***",22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("****"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; System.out.println(sftpChannel.pwd()); System.out.println(sftpChannel.lpwd()); sftpChannel.exit(); session.disconnect(); }catch(JSchException e){writeToSDFile(" "+e.toString());}
JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("ragesh", "10.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("password"); session.connect(); System.out.println("Connected to session successfully"); Channel channel = session.openChannel("sftp"); channel.connect(); System.out.println("Connected to Channel successfully"); ChannelSftp sftpChannel = (ChannelSftp) channel; // this will read file with the name test.txt and write to remote directory sftpChannel.cd("/root/usr/path"); File f = new File("test.txt"); sftpChannel.put(new FileInputStream(f), f.getName()); // here you can also change the target file name by replacing f.getName() with your choice of name sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); }
public boolean transferFileSFTP(Node node,String srcDir,String targetDir,String srcFileName,String targetFileName, String direction) { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession(node.getUserName(), node.getHost(), 22); session.setConfig("StrictHostKeyChecking", "no"); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setPassword(node.getPassword()); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; System.out.println("src:" + srcDir+srcFileName ); System.out.println("target:" + targetDir+targetFileName ); sftpChannel.get(targetDir+targetFileName, srcDir+srcFileName); sftpChannel.exit(); session.disconnect(); return true; } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } return false; }
java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); Session session = null; ChannelExec channel = null; try { JSch jSch = new JSch(); jSch.addIdentity("/tmp/privatekey"); session = jSch.getSession("testuser", address, 22); session.setConfig(config); session.connect(); channel = (ChannelExec) session.openChannel("exec"); BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream())); channel.setCommand(command); channel.connect(); if (channel.getExitStatus() == 0 || channel.isClosed() || channel.isEOF()) { logger.info("SSH connection is successful!"); } in.close(); } catch (JSchException jsche) { logger.error("Trying to SSH to host: {} but got exception {}", address, jsche); } finally { if (channel != null) channel.disconnect(); if (session != null) session.disconnect(); }
import com.jcraft.jsch.*; public class TestJSch { public static void main(String args[]) { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("username", "127.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("password"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.get("remotefile.txt", "localfile.txt"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (SftpException e) { e.printStackTrace(); } } }
public static void main(String[] args) { putFile("user", "host", "passwd", "/../test.txt", "C:/test.txt"); } public static void putFile(String username, String host, String password, String remotefile, String localfile){ JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession(username, host, 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.put(localfile, remotefile); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } }