gpt4 book ai didi

sockets - com.jcraft.jsch.JSchException : java.net.ConnectException:连接被拒绝:连接

转载 作者:行者123 更新时间:2023-12-03 11:49:29 27 4
gpt4 key购买 nike

我知道有重复的 >>> 从副本复制 >>>只要您的本地计算机有一个运行的 SSH 服务器 <<<<< 但我不能评论,也不能像问题一样(而且我没有提供答案。 ...)

它声明“只要您的本地计算机运行 SSH 服务器”,但我不知道如何运行 SSH 服务器。我打开我的腻子(双击它)(不确定这是否意味着SSH(?腻子?)服务器(?)正在运行......怀疑......

我对套接字编程真的很陌生。我正在使用 JSch (http://www.jcraft.com/jsch/) 尝试连接到远程服务器(后期)
目前,这是我使用的代码,我试图连接到我的本地计算机并执行命令(确切地说是 ls)来进行测试。但是,我一直点击连接被拒绝。我用谷歌搜索,我注意到有一些文章提到“让服务器监听”,但我不知道这是什么意思。请查看我的代码如下。

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import com.jcraft.jsch.*;



class SwingWorkerExample {

JTextField hostField;
JTextField userNameField;
JTextField passwordField;
JPanel panel;


public SwingWorkerExample() {
JPanel p = panel = new JPanel(new GridLayout(0,2));
hostField = new JTextField(20);
userNameField = new JTextField(20);
passwordField = new JPasswordField(20);
JButton testButton = new JButton("connect!");
testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
testConnectionButtonActionPerformed(ev);
}
});
p.add(new JLabel("host:"));
//127.0.0.1
p.add(hostField);
p.add(new JLabel("user:"));
//mycomputerusername
p.add(userNameField);
p.add(new JLabel("password:"));
//mycomputerpassword
p.add(passwordField);
p.add(testButton);
}

public JPanel getPanel() {
return panel;
}

private void testConnectionButtonActionPerformed(ActionEvent evt) {

SwingWorker sw = new SwingWorker(){

protected Object doInBackground() throws Exception {
try {
JSch jsch = new JSch();

String host = hostField.getText();
String username = userNameField.getText();
String password = passwordField.getText();

Session session = jsch.getSession(username, host);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");

session.setTimeout(20000);
System.out.println("Connecting to server...");
session.connect();

return session;
}
catch(Exception ex) {
ex.printStackTrace();
throw ex;
}
}

public void done(){
try {
System.out.println(get());
} catch (Exception ex) {
ex.printStackTrace();
}
}
};

sw.execute();

}


public static void main(String[] egal) {
EventQueue.invokeLater(new Runnable(){public void run() {
SwingWorkerExample ex = new SwingWorkerExample();
JFrame f = new JFrame("bla");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setContentPane(ex.getPanel());
f.pack();
f.setVisible(true);
}});
}

public void remoteLs() throws JSchException, IOException {
JSch js = new JSch();
Session s = js.getSession("kellyseo", "192.168.0.103", 22);
s.setPassword("S9031808z");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;

ce.setCommand("ls -l");
ce.setErrStream(System.err);

ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit code: " + ce.getExitStatus());

}



public void remoteMkdir() throws JSchException, IOException {
JSch js = new JSch();
Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
s.setPassword("mypassword");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;

ce.setCommand("mkdir remotetestdir");
ce.setErrStream(System.err);

ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit code: " + ce.getExitStatus());

}

public void remoteCopy() throws JSchException, IOException, SftpException {
JSch js = new JSch();
Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
s.setPassword("mypassword");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("sftp");
ChannelSftp ce = (ChannelSftp) c;

ce.connect();

ce.put("/home/myuser/test.txt","test.txt");

ce.disconnect();
s.disconnect();
}
}

顺便说一句,我使用命令提示 ping 127.0.0.1 没问题,但如果我使用 telnet 127.0.0.1,它会显示无法打开与主机的连接(我打开了 putty(?双击?),在端口 23:连接失败。
而且,SSH = PUTTY ...对吗? (我不能在命令提示符下使用 'ssh' 命令)

链接:
1) http://sourceforge.net/p/jsch/mailman/message/31745775/

和 2) http://javarevisited.blogspot.sg/2013/02/java-net-ConnectException-Connection-refused.html

和 3) http://www.jcraft.com/jsch/examples/
和 4) Run a command over SSH with JSch
5) Can we use JSch for SSH key-based communication?

和...谢谢提前!

哦,还有 http://www.ganymed.ethz.ch/ssh2/ (JSch 的替代品.. 欢迎任何建议!)但是当我尝试运行该示例时,它说没有 main。哪个..我不知道>.<到那时会坚持使用JSch....

顺便说一句,我试试 https://serverfault.com/questions/185153/free-public-ssh-server-for-testing-purposes对于服务器,但...我不知道地址、用户名和密码是什么。 (我也有一个 http://sdf.org 新创建的帐户,但是当我尝试连接它时,它显示为 unknownhost。仅供引用!)

忘了提一下,我使用的是 Windows 7,而“yum”不是我的命令提示符中的命令...

最佳答案

您正在尝试通过 SSH 协议(protocol)连接到本地主机。对于 JSCH,这不完全是套接字编程,但您的问题与套接字编程有关。

本质上,您的问题是您的程序正在尝试连接到未打开的端口,特别是在这种情况下,它是端口 22。您没有 SSH 服务器,因此您的 SSH 客户端无法执行任何操作。您正在给没有电话的人打电话。

要解决此问题,您需要找到一个运行 ssh 的测试服务器来进行开发,或者在本地 PC 上安装一个 ssh 服务器。对于 Windows 盒子,您最好的选择是 cygwin ,这将允许您模拟 posix 系统并在本地计算机上运行 SSHD。谷歌搜索 cygwin 和 sshd 将为您提供如何设置的示例。

关于sockets - com.jcraft.jsch.JSchException : java.net.ConnectException:连接被拒绝:连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27241952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com