如何解决java ssh连接被拒绝
尝试使用java设置ssh服务器,我检查了我的机器的IP和用户名,这似乎是正确的,但是无论我做什么都仍然会出现连接被拒绝的错误。无论我做什么都仍然会出现连接被拒绝的错误。不知道发生了什么,我正在使用Windows机器,有人可以解释一下如何在Java中设置ssh服务器吗?谢谢!
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();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。