Ssh Connection in Java Example
8 Answers 8
Reset to default
Introducing: Trending sort
You can now choose to sort by Trending, which boosts votes that have happened recently, helping to surface more up-to-date answers.
Trending is based off of the highest score sort and falls back to it if no posts are trending.
Use JSch
import com.jcraft.jsch.*; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Scanner; /** * @author World */ public class SSHReadFile { public static void main(String args[]) { String user = "john"; String password = "mypassword"; String host = "192.168.100.23"; int port = 22; String remoteFile = "/home/john/test.txt"; try { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); System.out.println("Establishing Connection..."); session.connect(); System.out.println("Connection established."); System.out.println("Crating SFTP Channel."); ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.connect(); System.out.println("SFTP Channel created."); InputStream inputStream = sftpChannel.get(remoteFile); try (Scanner scanner = new Scanner(new InputStreamReader(inputStream))) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } } } catch (JSchException | SftpException e) { e.printStackTrace(); } } } output:
Establishing Connection... Connection established. Crating SFTP Channel. SFTP Channel created. This is content from file /home/john/test.txt
answered Jan 26, 2012 at 13:51
WorldWorld
1,919 7 gold badges 26 silver badges 37 bronze badges
3
Java doesn't support that natively, but you can use a library like JSch to do it
answered Jun 18, 2010 at 17:21
Michael MrozekMichael Mrozek
162k 28 gold badges 165 silver badges 171 bronze badges
answered Jun 18, 2010 at 17:21
Brian ClapperBrian Clapper
24.5k 7 gold badges 63 silver badges 65 bronze badges
You must use a third-party library - JSch is one of them. Google with "Java ssh" and you will get plenty of other options.
answered Jun 18, 2010 at 17:22
Petar MinchevPetar Minchev
46k 11 gold badges 102 silver badges 118 bronze badges
answered Jun 18, 2010 at 17:34
3
-
I would prefer people do not use Ganymed since it is no longer maintained.
–user713867
Sep 16, 2013 at 11:14
-
@timonti because you were the maintainer?
Sep 16, 2013 at 11:37
-
" Please note: ETH Zurich does not maintain the code anymore. Please visit the following website, code.google.com/p/ganymed-ssh-2/, in case you need updates "
Aug 14, 2016 at 13:45
You could check JSSH, which is a Java SSH library.
answered Jun 18, 2010 at 17:22
FortegaFortega
19.2k 14 gold badges 74 silver badges 112 bronze badges
1
-
Hi.. I need to add some parameters in my ssh command, like ssh -v user@ip, can u tell me how can I do using this Library
Feb 28, 2019 at 7:48
Not 100% sure but I believe that the sockets have a great deal to do with ssh connections unless you are attempting to do a pre-installed command manipulation application. There are numerous outside packages that you can use to achieve this goal, but if you want to do your own socket creation, it's going to take a large amount of reading and deciphering/encrypting to get this done.
Also creating your own socket is dangerous if not done correctly with the right protocols and security specifications, so be wary about doing so until you are 100% sure you're capable of committing to learning enough.
answered Aug 14, 2019 at 20:38
I used this and worked for me
Channel channel=session.openChannel("exec"); String command = "Your Command here"; ((ChannelExec)channel).setCommand(command); InputStream in=channel.getInputStream(); ((ChannelExec)channel).setErrStream(System.err); channel.connect();
Tunaki
126k 44 gold badges 319 silver badges 401 bronze badges
answered Nov 16, 2016 at 18:23
Not the answer you're looking for? Browse other questions tagged java ssh connection or ask your own question.
Source: https://stackoverflow.com/questions/3071760/ssh-connection-with-java
Great answer worked for me but just make sure to close everything you open. session.disconnect and sftpChannel.quit .
Dec 16, 2015 at 19:49
When invoking the constructor the following exception is thrown:
GRAVE: JSF1073: se ha interceptado javax.faces.event.AbortProcessingException durante el procesamiento de INVOKE_APPLICATION 5 : UIComponent-ClientId=tipoMovimientoGrid:j_idt15, Mensaje=java.lang.ClassCircularityError: com/jcraft/jsch/JSchException java.lang.ClassCircularityError: com/jcraft/jsch/JSchException at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:178)Aug 24, 2016 at 17:53
getting the error while trying to connect windows server: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
Oct 3, 2017 at 11:31