session.connect(); System.out.println("Host connected."); channel = session.openChannel("sftp"); channel.connect(); System.out.println("sftp channel opened and connected.");
channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command);
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(); } } }
Session session = null; Channel channel = null; try { JSch ssh = new JSch(); ssh.setKnownHosts("/path/of/known_hosts/file"); session = ssh.getSession("username", "host", 22); session.setPassword("password"); session.connect(); channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftp = (ChannelSftp) channel; sftp.put("/path/of/local/file", "/path/of/ftp/file"); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } finally { if (channel != null) { channel.disconnect(); } if (session != null) { session.disconnect(); } }
Channel channel = session.openChannel( "sftp" ); channel.connect();
Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null);
Session session = jsch.getSession(user, host, 22); Channel channel = session.openChannel("shell"); OutputStream inputstream_for_the_channel = channel.getOutputStream(); PrintStream commander = new PrintStream(inputstream_for_the_channel, true); channel.setOutputStream(System.out, true); channel.connect(); commander.println("ls -la"); commander.println("cd folder"); commander.println("ls -la"); commander.println("exit"); commander.close(); do { Thread.sleep(1000); } while(!channel.isEOF()); session.disconnect();
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(); String path = "PATH TO PRIVATE KEY"; jsch.addIdentity(path); jsch.setConfig("StrictHostKeyChecking", "no"); Session session = jsch.getSession(userName, ipToConnect, portToConnect); session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec)channel).setCommand("COMMAND TO FIRE"); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); //Read Response Here channel.disconnect(); session.disconnect();
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();
JSch jsch = new JSch(); String path = "PATH TO PRIVATE KEY"; jsch.addIdentity(path); jsch.setConfig("StrictHostKeyChecking", "no"); Session session = jsch.getSession(userName, ipToConnect, portToConnect); session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec)channel).setCommand("COMMAND TO FIRE"); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); //Read Response Here channel.disconnect(); session.disconnect();
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"); } }
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(); }
Session sessionRead = jsch.getSession("username", "127.0.0.1", 22); sessionRead.connect(); Session sessionWrite = jsch.getSession("username", "127.0.0.1", 22); sessionWrite.connect(); ChannelSftp channelRead = (ChannelSftp)sessionRead.openChannel("sftp"); channelRead.connect(); ChannelSftp channelWrite = (ChannelSftp)sessionWrite.openChannel("sftp"); channelWrite.connect(); PipedInputStream pin = new PipedInputStream(2048); PipedOutputStream pout = new PipedOutputStream(pin); channelRead.get("/path/to/your/file/including/filename.txt", pout); channelWrite.put(pin, "/path/to/your/file/destination/including/filename.txt"); channelRead.disconnect(); channelWrite.disconnect(); sessionRead.disconnect(); sessionWrite.disconnect();
JSch jsch = new JSCH(); Session session = jsch.getSession(config.getUsername(), config.getHostname(), config.getPort()); //port is usually 22 session.setPassword(config.getPassword()); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp cFTP = (ChannelSftp) channel; String sourceFile = "---", targetFile = "---"; cFTP.put(sourceFile , targetFile ); cFTP.disconnect(); session.disconnect();
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(); } }