Java Secure Channel Jsch Example
Java Code Examples for com.jcraft.jsch.Channel
The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.
Example 1
From project gerrit-trigger-plugin, under directory /gerrit-events/src/main/java/com/sonyericsson/hudson/plugins/gerrit/gerritevents/ssh/.
Source file: SshConnectionImpl.java
27
/** * Execute an ssh command on the server, without closing the session so that a Reader can be returned with streaming data from the server. * @param command the command to execute. * @return a Reader with streaming data from the server. * @throws IOException if it is so. * @throws SshException if there are any ssh problems. */ @Override public synchronized Reader executeCommandReader(String command) throws SshException, IOException { if (!isConnected()) { throw new IllegalStateException("Not connected!"); } try { Channel channel=connectSession.openChannel("exec"); ((ChannelExec)channel).setCommand(command); InputStreamReader reader=new InputStreamReader(channel.getInputStream()); channel.connect(); return reader; } catch ( JSchException ex) { throw new SshException(ex); } }
Example 2
From project JGit, under directory /org.spearce.jgit/src/org/spearce/jgit/transport/.
Source file: TransportSftp.java
27
ChannelSftp newSftp() throws TransportException { initSession(); try { final Channel channel=sock.openChannel("sftp"); channel.connect(); return (ChannelSftp)channel; } catch ( JSchException je) { throw new TransportException(uri,je.getMessage(),je); } }
Example 3
From project koneki.ldt, under directory /plugins/org.eclipse.koneki.ldt.remote.debug.core/src/org/eclipse/koneki/ldt/remote/debug/core/internal/sshprocess/.
Source file: SshProcess.java
26
/** * Create and launch a process corresponding to the given command throw an ssh connection<br> <b>/!\ Session must be connected !</b> A .PID file will be create in the working directory for this process.(which contains the PID of this process)<br> This file is used to be able to stop (kill) the process when needed.<br> So, User must have the write to create file in the working directory and so 2 process must not be run at the same time in the same working directory * @throws CoreException if exec channel can not be created, or session is down */ public SshProcess(Session session,ILaunch launch,String workingDirectoryPath,String command,Map<String,String> envVars) throws CoreException { this.launch=launch; this.currentSession=session; this.workingDir=workingDirectoryPath; Channel channel; try { channel=session.openChannel("exec"); } catch ( JSchException e) { throw new CoreException(new Status(IStatus.ERROR,Activator.PLUGIN_ID,"Unable to create SShProcess",e)); } if (!(channel instanceof ChannelExec)) throw new CoreException(new Status(IStatus.ERROR,Activator.PLUGIN_ID,"Unable to create SShProcess")); channelExec=(ChannelExec)channel; this.label=command; String composedCommand=createLaunchCommand(workingDirectoryPath,command,envVars); channelExec.setCommand(composedCommand); }
Example 4
From project koneki.ldt, under directory /plugins/org.eclipse.koneki.ldt.remote.debug.core/src/org/eclipse/koneki/ldt/remote/debug/core/internal/sshprocess/.
Source file: SshProcess.java
26
/** * try to kill the process with the PID equal to the value of the .PID file contained in pidContainerFolder */ public static void killProcess(Session session,String pidContainerFolder) throws DebugException { try { Channel channel=session.openChannel("exec"); if (!(channel instanceof ChannelExec)) throw new JSchException("Unable to create exec channel"); ChannelExec killChannel=(ChannelExec)channel; String killCommand=createKillCommand(pidContainerFolder); killChannel.setCommand(killCommand); killChannel.connect(); } catch ( Exception e) { throw new DebugException(new Status(IStatus.ERROR,Activator.PLUGIN_ID,"An exception occurred when trying to stop the application.",e)); } }
Example 5
From project Twister, under directory /src/plugins/.
Source file: GITPlugin.java
26
public void initializeSFTP(){ try { JSch jsch=new JSch(); String user=variables.get("user"); Session session=jsch.getSession(user,variables.get("host"),22); session.setPassword(variables.get("password")); Properties config=new Properties(); config.put("StrictHostKeyChecking","no"); session.setConfig(config); session.connect(); Channel channel=session.openChannel("sftp"); channel.connect(); c=(ChannelSftp)channel; System.out.println("SFTP successfully initialized"); } catch ( Exception e) { System.out.println("SFTP could not be initialized"); e.printStackTrace(); } }
Example 6
From project aws-tasks, under directory /src/main/java/datameer/awstasks/ssh/.
Source file: ScpDownloadCommand.java
25
@Override public void execute(Session session) throws IOException { String command=constructScpInitCommand(_remoteFile,_recursive); Channel channel=SshUtil.openExecChannel(session,command); try { OutputStream out=channel.getOutputStream(); InputStream in=channel.getInputStream(); SshUtil.sendAckOk(out); download(in,out,_localFile); } finally { if (channel != null) { channel.disconnect(); } } }
Example 7
From project aws-tasks, under directory /src/main/java/datameer/awstasks/ssh/.
Source file: ScpUploadCommand.java
25
@Override public void execute(Session session) throws IOException { String command=constructScpUploadCommand(_localFile.isDirectory(),_targetPath); Channel channel=SshUtil.openExecChannel(session,command); try { OutputStream out=channel.getOutputStream(); InputStream in=channel.getInputStream(); SshUtil.checkAcknowledgement(in); if (_localFile.isDirectory()) { uploadFolder(_localFile,in,out); } else { uploadFile(_localFile,in,out); } } finally { if (channel != null) { channel.disconnect(); } } }
Example 8
From project aws-tasks, under directory /src/main/java/datameer/awstasks/ssh/.
Source file: SshExecCommand.java
25
private void executeCommand(Session session,String command) throws IOException { final Channel channel=SshUtil.openExecChannel(session,command); channel.setOutputStream(_outputStream); channel.setExtOutputStream(_outputStream); try { do { Thread.sleep(500); } while (!channel.isClosed()); } catch ( InterruptedException e) { Thread.interrupted(); } int exitCode=channel.getExitStatus(); if (exitCode != 0) { String msg="Remote command failed with exit status " + exitCode; throw new IOException(msg); } }
Example 9
private void executeCommand(Session session,String command) throws IOException { final Channel channel=SshUtil.openExecChannel(session,command); ToLineOutputStream outputStream=new ToLineOutputStream(_outputHandler); try { channel.setOutputStream(outputStream); channel.setExtOutputStream(outputStream); try { do { Thread.sleep(250); } while (!channel.isClosed()); } catch ( InterruptedException e) { Thread.interrupted(); } int exitCode=channel.getExitStatus(); if (exitCode != 0 && _command.failOnError()) { throw new IOException("could not execute command '" + command + "', got exit code "+ exitCode); } _result=_outputHandler.getResult(exitCode); } finally { outputStream.close(); } }
Example 10
From project CBCJVM, under directory /consoleDownload/src/helpers/.
Source file: Ssh.java
25
public int sendCommand(String command){ int ret=-1; try { Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in=channel.getInputStream(); channel.connect(); byte[] tmp=new byte[1024]; while (true) { while (in.available() > 0) { int i=in.read(tmp,0,1024); if (i < 0) break; System.out.print(new String(tmp,0,i)); } if (channel.isClosed()) { ret=channel.getExitStatus(); break; } try { Thread.sleep(1000); } catch ( Exception ee) { } } channel.disconnect(); } catch ( JSchException e) { e.printStackTrace(); } catch ( IOException e) { e.printStackTrace(); } return ret; }
Example 11
From project CBCJVM, under directory /eclipse/CBC/src/cbc/helpers/.
Source file: Ssh.java
25
public int sendCommand(String command){ int ret=-1; try { Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in=channel.getInputStream(); channel.connect(); byte[] tmp=new byte[1024]; while (true) { while (in.available() > 0) { int i=in.read(tmp,0,1024); if (i < 0) break; System.out.print(new String(tmp,0,i)); } if (channel.isClosed()) { ret=channel.getExitStatus(); break; } try { Thread.sleep(1000); } catch ( Exception ee) { } } channel.disconnect(); } catch ( JSchException e) { e.printStackTrace(); } catch ( IOException e) { e.printStackTrace(); } return ret; }
Example 12
From project CBCJVM, under directory /eclipse/src/cbcdownloader/.
Source file: Ssh.java
25
public String sendCommand(String command){ String ret=new String(); try { Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in=channel.getInputStream(); channel.connect(); byte[] tmp=new byte[1024]; while (true) { while (in.available() > 0) { int i=in.read(tmp,0,1024); if (i < 0) break; ret+=new String(tmp,0,i); } if (channel.isClosed()) { break; } try { Thread.sleep(100); } catch ( Exception ee) { } } channel.disconnect(); } catch ( JSchException e) { e.printStackTrace(); } catch ( IOException e) { e.printStackTrace(); } return ret; }
Example 13
From project gerrit-trigger-plugin, under directory /gerrit-events/src/main/java/com/sonyericsson/hudson/plugins/gerrit/gerritevents/ssh/.
Source file: SshConnectionImpl.java
25
/** * Execute an ssh command on the server. After the command is sent the used channel is disconnected. * @param command the command to execute. * @return a String containing the output from the command. * @throws SshException if so. */ @Override public synchronized String executeCommand(String command) throws SshException { if (!isConnected()) { throw new IllegalStateException("Not connected!"); } try { logger.debug("Opening channel"); Channel channel=connectSession.openChannel(CMD_EXEC); ((ChannelExec)channel).setCommand(command); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(channel.getInputStream())); logger.debug("connecting channel."); channel.connect(); String incomingLine=null; StringBuilder commandOutput=new StringBuilder(); while ((incomingLine=bufferedReader.readLine()) != null) { commandOutput.append(incomingLine); commandOutput.append('\n'); logger.trace("Incoming line: {}",incomingLine); } logger.trace("Closing reader."); bufferedReader.close(); logger.trace("disconnecting channel."); channel.disconnect(); return commandOutput.toString(); } catch ( JSchException ex) { throw new SshException(ex); } catch ( IOException ex) { throw new SshException(ex); } }
Example 14
From project rhevm-api, under directory /powershell/expectj/src/test/java/com/redhat/rhevm/api/powershell/expectj/.
Source file: TestSshSpawn.java
25
/** * Verify that a channel session holds together. * @throws Exception If testing goes exceptionally bad. */ public void testSshSpawnChannel() throws Exception { InputStream inputStream=(InputStream)Mockito.mock(InputStream.class); OutputStream outputStream=(OutputStream)Mockito.mock(OutputStream.class); Channel channel=(Channel)Mockito.mock(Channel.class); Mockito.when(channel.getInputStream()).thenReturn(inputStream); Mockito.when(channel.getOutputStream()).thenReturn(outputStream); Spawnable testMe=new SshSpawn(channel); testMe.start(); assertSame(inputStream,testMe.getStdout()); assertSame(outputStream,testMe.getStdin()); assertNull(testMe.getStderr()); assertFalse(testMe.isClosed()); ((Channel)(Mockito.verify(channel))).isClosed(); testMe.stop(); ((Channel)(Mockito.verify(channel))).disconnect(); assertTrue(testMe.isClosed()); assertTrue(testMe.getExitValue() == 0); }
Example 15
From project Twister, under directory /src/plugins/.
Source file: SVNPlugin.java
25
public void initializeSFTP(){ try { JSch jsch=new JSch(); String user=variables.get("user"); Session session=jsch.getSession(user,variables.get("host"),22); session.setPassword(variables.get("password")); Properties config=new Properties(); config.put("StrictHostKeyChecking","no"); session.setConfig(config); session.connect(); Channel channel=session.openChannel("sftp"); channel.connect(); c=(ChannelSftp)channel; System.out.println("SFTP successfully initialized"); } catch ( Exception e) { System.out.println("SFTP could not be initialized"); e.printStackTrace(); } }
Example 16
From project aws-tasks, under directory /src/main/java/datameer/awstasks/util/.
Source file: SshUtil.java
23
public final static Channel openExecChannel(Session session,String command) throws IOException { try { ChannelExec channel=(ChannelExec)session.openChannel("exec"); channel.setCommand(command); channel.connect(); return channel; } catch ( JSchException e) { throw new IOException("could not open exec channel with command " + command,e); } }
Example 17
From project rhevm-api, under directory /powershell/expectj/src/main/java/com/redhat/rhevm/api/powershell/expectj/.
Source file: SshSpawn.java
23
/** * Takes control over an existing SSH channel. * @param channel The channel we should control. If this channel isn'talready connected, {@link Channel#connect()} will be called. * @throws IOException If connecting the channel fails. */ public SshSpawn(Channel channel) throws IOException { if (!channel.isConnected()) { try { channel.connect(); } catch ( JSchException e) { throw new IOException("Failed connecting the channel",e); } } this.m_channel=channel; m_toSocket=m_channel.getInputStream(); m_fromSocket=m_channel.getOutputStream(); }
genovesedresseppy.blogspot.com
Source: http://www.javased.com/index.php?api=com.jcraft.jsch.Channel
0 Response to "Java Secure Channel Jsch Example"
Enregistrer un commentaire