Guest User

Untitled

a guest
Feb 6th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package sshexample;
  2.  
  3. import com.jcraft.jsch.*;
  4. import java.io.*;
  5.  
  6. public class SSHexample
  7. {
  8. public static void main(String[] args)
  9. {
  10. String user = "user";
  11. String password = "password";
  12. String host = "192.168.100.103";
  13. int port=22;
  14. try
  15. {
  16. JSch jsch = new JSch();
  17. Session session = jsch.getSession(user, host, port);
  18. session.setPassword(password);
  19. session.setConfig("StrictHostKeyChecking", "yes");
  20. System.out.println("Establishing Connection...");
  21. session.connect();
  22. System.out.println("Connection established.");
  23. System.out.println("Crating SFTP Channel.");
  24. ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
  25. sftpChannel.connect();
  26. }catch(Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. hostname,10.1.1.120, ssh-rsa AAAAC3NzaC1yc2EAAAADAQABAAABAQCi5b647581SwC0uUDQw1ENjKSz3rhJMRRZEgIjHylvF4fbuAEzj645YoAf9SItb51MhetFAJrq98jYsHpedSm3IoMG+aR/P1CjsBz1RtJKlfR2NfYDCZ7Dyx11P8FnJbwbYif/GeG0xEujekwF1pyL0tNPmf0H4/GPR4mwrv/llGlB3Lo3BzxrGtl4f4X/oSHDoo7FrQkDwqOfeSM++3vPPHxyVO5zhFJ5u9f7M/uuxUeHS+YS5JWAI7NLXKgbiM9dluGzZU/6Awo3ux4x5ojL+kf29JEVxK+o6GfW2bIW+LhgIGZNThnN5nHzBVfNNHvQ7KC5ic0h2z2gbVpwJr1h
  32.  
  33. ssh-keyscan -t rsa 10.1.1.120
  34.  
  35. 192.168.0.1 ssh-rsa
  36. AAAAC3NzaC1yc2EAAAADAQABAAABAQCi5b647581SwC0uUDQw1ENjKSz3rhJMRRZEgIjHylvF4fbuAEzj645YoAf9SI
  37. tb51MhetFAJrq98jYsHpedSm3IoMG+aR/P1CjsBz1RtJKlfR2NfYDCZ7Dyx11P8FnJbwbYif
  38. /GeG0xEujekwF1pyL0tNPmf0H4/GPR4mwrv/llGlB3Lo3BzxrGtl4f4X
  39. /oSHDoo7FrQkDwqOfeSM++3vPPHxyVO5zhFJ5u9f7M/uuxUeHS+YS5JWAI7NLXKgbiM9dluGzZU
  40. /6Awo3ux4x5ojL+kf29JEVxK+o6GfW2bIW+LhgIGZNThnN5nHzBVfNNHvQ7KC5ic0h2z2gbVpwJr1h
  41.  
  42. session=null;
  43. com.jcraft.jsch.Channel channel =null;
  44. try{
  45. ssh=new JSch();
  46. ssh.setKnownHosts("test");
  47. session=ssh.getSession(userTextField.getText(),ip,22);
  48. session.setPassword(passwordField1.getText());
  49.  
  50. System.out.println(session.getHostKey());
  51. session.connect();
  52. channel=session.openChannel("sftp");
  53. channel.connect();
  54. ChannelSftp sftp=(ChannelSftp) channel;
  55. System.out.println(sftp.getHome());
  56. for (Object o : sftp.ls(sftp.getHome())) {
  57. System.out.println(((ChannelSftp.LsEntry)o).getFilename());
  58. }
  59.  
  60. } catch (JSchException e1) {
  61. e1.printStackTrace();
  62. addHost(session.getHostKey().getKey());
  63. } catch (SftpException e1) {
  64. e1.printStackTrace();
  65. }
  66. }
  67.  
  68. private void addHost(String key){
  69. try {
  70. FileWriter tmpwriter=new FileWriter("test",true);
  71.  
  72. tmpwriter.append(ip + " ssh-rsa " + key+"n");
  73. System.out.println(ip + " ssh-rsa " + key);
  74.  
  75. tmpwriter.flush();
  76. tmpwriter.close();
  77.  
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82.  
  83. user/.ssh/known_hosts
  84.  
  85. config-service:
  86. image: <host>/<bla>-config-service:<version>
  87. hostname: config-service
  88. ports:
  89. - 3000:3000
  90. depends_on:
  91. - discovery
  92. environment:
  93. - CONSUL_HOST=discovery
  94. - CONSUL_PORT=<port>
  95. - CONFIG_GIT_URI=git@<host>:<group>/<repository>.git
  96. - CONFIG_GIT_BRANCH=development
  97. volumes:
  98. - ~/.ssh/:/root/.ssh/:ro
  99.  
  100. chcon -Rt svirt_sandbox_file_t ~/.ssh
Add Comment
Please, Sign In to add comment