Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.List;
  3. import com.sshtools.j2ssh.SshClient;
  4. import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
  5. import com.sshtools.j2ssh.authentication.KBIAuthenticationClient;
  6. import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
  7. import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
  8.  
  9.  
  10.  
  11.  
  12. public class checkThat {
  13.  
  14.  
  15. public static void main(String s[])
  16. {
  17. try {
  18. connect();
  19. } catch (Exception e) {
  20. // TODO Auto-generated catch block
  21. e.printStackTrace();
  22. }
  23. }
  24.  
  25. public static void connect() throws Exception {
  26. try {
  27. int result =-1;
  28. String m_sUser= "user";
  29. String m_sPassword="pswd";
  30.  
  31. SshClient ssh = new SshClient();
  32. try{
  33. ssh.connect("1.1.1.1", 22, new IgnoreHostKeyVerification());
  34. }catch (IOException ie)
  35. {
  36. ie.printStackTrace();
  37. }
  38.  
  39. //System.out.println( "Connect successful");
  40.  
  41.  
  42. List authType = ssh.getAvailableAuthMethods(m_sUser);
  43. if(!authType.isEmpty() && authType.contains("password"))
  44. {
  45. //m_logger.severe("FTSSH:Connected to host via password");
  46. PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
  47. pwd.setUsername(m_sUser);
  48. pwd.setPassword(m_sPassword);
  49. // Authenticate the user
  50. result = ssh.authenticate(pwd);
  51. }
  52. else
  53. {
  54. //m_logger.severe("FTSSH:Connected to host via KBI");
  55. KBIAuthenticationClient kbi = new KBIAuthenticationClient();
  56. kbi.setUsername(m_sUser);
  57. /*ASCKBIRequestHandler kbiHandler = new ASCKBIRequestHandler();
  58. kbiHandler.setPassCode(m_sPassword);
  59. kbi.setKBIRequestHandler(kbiHandler);*/
  60. // Authenticate the user
  61. result = ssh.authenticate(kbi);
  62. }
  63.  
  64. /* Authenticate.
  65. * If you get an IOException saying something like
  66. * "Authentication method password not supported by the server at this stage."
  67. */
  68. PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
  69. pwd.setUsername("user");
  70. pwd.setPassword("pswd");
  71.  
  72. result = ssh.authenticate(pwd);
  73.  
  74. if (result != AuthenticationProtocolState.COMPLETE){
  75. throw new Exception("Login to failed");
  76. }
  77.  
  78. /*session = ssh.openSessionChannel();
  79. sor = new SessionOutputReader(session);
  80. // Request a pseudo terminal, if you do not you may not see the prompt
  81. session.requestPseudoTerminal("gogrid",80,48, 0 , 0, "");
  82. // Start the users shell
  83. session.startShell();*/
  84.  
  85. } catch (Exception e) {
  86. //log.error(e.getMessage(), e);
  87. System.out.println( e.getMessage());
  88. e.printStackTrace();
  89. throw new Exception("Login to failed");
  90. }
  91.  
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement