0

trying to setup ssh server using java , i checked my machines ip and username it seems to be correct but still getting connection refused error no matter what I do.still getting connection refused error no matter what I do. Not sure whats going on , Im using windows machine can someone please explain how to setup a ssh server in java ? Thanks !

package javaapplication3;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author World
 */
public class JavaApplication3 {

    public static void main(String args[]) throws UnsupportedEncodingException, IOException {
        String user = "admin";
        String password = "123";
        String host = "192.168.1.161";
        int port = 22;
        String remoteFile = "pass.txt";

        try {
            JSch jsch = new JSch();

        
 Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            Properties properties = new Properties();
           

          
            properties.put("StrictHostKeyChecking", "no");
            session.setConfig(properties);
            session.setTimeout(2300);
            System.out.println("Establishing Connection...");
            session.connect();

//            session.setTimeout(20000);
//            session.connect();
            System.out.println("Connection established.");
            System.out.println("Crating SFTP Channel.");
            ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
            sftpChannel.connect(20000);
            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();
                    String[] sArray = line.split(",");
                    System.out.println(sArray[0]); //Just to verify that file is being read
                    System.out.println(sArray[1]);
                    if (user == sArray[0] && password == sArray[1]) {

                        File file = new File("C:/Users/Me/Desktop/directory/file.txt");
                        PrintWriter printWriter = null;
                        try {
                            printWriter = new PrintWriter("file.txt");
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(JavaApplication3.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        printWriter.println("hello");
                        printWriter.close();

                    } else {
                        System.out.println(line);
                    }

                }
            }
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }
    }
}
Leo Bogod
  • 369
  • 8
  • 28

0 Answers0