Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.List;
- import com.sshtools.j2ssh.SshClient;
- import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
- import com.sshtools.j2ssh.authentication.KBIAuthenticationClient;
- import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
- import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
- public class checkThat {
- public static void main(String s[])
- {
- try {
- connect();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void connect() throws Exception {
- try {
- int result =-1;
- String m_sUser= "user";
- String m_sPassword="pswd";
- SshClient ssh = new SshClient();
- try{
- ssh.connect("1.1.1.1", 22, new IgnoreHostKeyVerification());
- }catch (IOException ie)
- {
- ie.printStackTrace();
- }
- //System.out.println( "Connect successful");
- List authType = ssh.getAvailableAuthMethods(m_sUser);
- if(!authType.isEmpty() && authType.contains("password"))
- {
- //m_logger.severe("FTSSH:Connected to host via password");
- PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
- pwd.setUsername(m_sUser);
- pwd.setPassword(m_sPassword);
- // Authenticate the user
- result = ssh.authenticate(pwd);
- }
- else
- {
- //m_logger.severe("FTSSH:Connected to host via KBI");
- KBIAuthenticationClient kbi = new KBIAuthenticationClient();
- kbi.setUsername(m_sUser);
- /*ASCKBIRequestHandler kbiHandler = new ASCKBIRequestHandler();
- kbiHandler.setPassCode(m_sPassword);
- kbi.setKBIRequestHandler(kbiHandler);*/
- // Authenticate the user
- result = ssh.authenticate(kbi);
- }
- /* Authenticate.
- * If you get an IOException saying something like
- * "Authentication method password not supported by the server at this stage."
- */
- PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
- pwd.setUsername("user");
- pwd.setPassword("pswd");
- result = ssh.authenticate(pwd);
- if (result != AuthenticationProtocolState.COMPLETE){
- throw new Exception("Login to failed");
- }
- /*session = ssh.openSessionChannel();
- sor = new SessionOutputReader(session);
- // Request a pseudo terminal, if you do not you may not see the prompt
- session.requestPseudoTerminal("gogrid",80,48, 0 , 0, "");
- // Start the users shell
- session.startShell();*/
- } catch (Exception e) {
- //log.error(e.getMessage(), e);
- System.out.println( e.getMessage());
- e.printStackTrace();
- throw new Exception("Login to failed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement