com.jcraft.jsch.JSchException:java.net.ConnectException:拒绝连接:connect [英] com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
问题描述
我知道有重复的>>>是从重复的>>>复制的,只要您的本地计算机具有运行<<<<<的SSH服务器.但我不能评论,也不能从问题中提出意见(而且我没有提供答案....)
I understand that there is duplicate >>> copied from the duplicate >>>As long as your local machine has an SSH server running <<<<< but i cannot comment and cannot as from the question (and im not providing an answer....)
它指出只要您的本地计算机上运行的是SSH服务器",但我不知道如何运行SSh服务器.我打开我的腻子(双击它)(不确定这是否意味着SSH(?Putty?)服务器(?)正在运行...怀疑如此...
It stated that "As long as your local machine has an SSH server running" but I do not know how to have a SSh server running. I turn on my putty (double click on it) (not sure if this means the SSH(?Putty?) server (?) is running... doubt so...
对于套接字编程来说真的很新.我正在使用JSch( http://www.jcraft.com/jsch/)来尝试连接到远程服务器(稍后阶段) 目前,这是我使用的代码,我试图连接到本地计算机并执行命令(准确地说是ls)进行测试.但是,我一直打连接被拒绝.我在Google上搜索,发现有些文章提到让服务器监听",但我不知道这是什么意思.请按如下方式查看我的代码.
im really new to socket programming. i am making use of JSch (http://www.jcraft.com/jsch/) to try to connect to a remote server (later stage) currently, this is the code i use and im trying to connect to my local computer and execute a command (ls to be exact) to do a test. However, i keep hitting the connection refused. I googled and i noticed that there are some articles that mentions on "having a server listening" but I have no idea what it means. Please view my code as below.
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,它说无法打开与主机的连接(我在端口23上打开了putty(?doubleclick?),连接失败:连接失败. 而且,SSH = PUTTY ...对吗? (我无法在命令提示符下使用"ssh"命令)
BTW i uses commandprompt to ping 127.0.0.1 is okay but if i use telnet 127.0.0.1 it says Could not open connection to the host (I turned on putty(?double click?) , on port 23: Connect failed. and, SSH = PUTTY ... right? (i cannot use the 'ssh' command in command prompt)
链接: 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)使用JSch通过SSH运行命令 和5)我们可以使用JSch进行基于SSH密钥的通信吗?
and 3) http://www.jcraft.com/jsch/examples/ and 4) Run a command over SSH with JSch and 5) Can we use JSch for SSH key-based communication?
然后...谢谢你!
哦,还有 http://www.ganymed.ethz.ch/ssh2/(是JSch的替代.欢迎任何建议!),但是当我尝试运行该示例时,它说没有主要内容.其中..我不知道>.<直到那时都将坚持w JSch....
oh, and theres also http://www.ganymed.ethz.ch/ssh2/ (an alternative to JSch.. any advise welcomed!) but when i try to run the example, it says no main. which.. i duno >.< Will stick w JSch till then....
顺便说一句,我尝试 https://serverfault.com/questions/185153 /free-public-ssh-server-for-testing-目的用于服务器,但是...我不知道地址,用户名和密码是多少. (我还有一个 http://sdf.org 帐户新创建的,但是当我尝试连接到它,上面写着unknownhost.fyi!)
BTW, i try https://serverfault.com/questions/185153/free-public-ssh-server-for-testing-purposes for server but... i have no idea whats the address, username and password. (i also have a http://sdf.org account newly created but when i try to connect to it, it says unknownhost. fyi!)
忘了提及,即时通讯使用Windows 7和'yum'不是我的命令提示符中的命令...
forgot to mention, im using windows 7 and 'yum' is not a command in my command prompt...
推荐答案
您正尝试通过SSH协议连接到本地主机.使用JSCH,这不完全是套接字编程,但是您的问题与套接字编程有关.
You're attempting to connect to your local host via the SSH protocol. With JSCH this isn't exactly socket programming, but your issue is related to socket programming.
本质上,您的问题是您的程序试图连接到未打开的端口,特别是在这种情况下,该端口是22.您没有SSH服务器,因此SSH客户端无法执行任何操作.您正在打电话给没有电话的人.
Essentially your issue is that your program is trying to connect to a port which isn't open, specifically in this instance it's port 22. You don't have an SSH server so your SSH client can't do anything. You're making a telephone call to someone who doesn't have a phone.
要解决此问题,您需要找到上面运行ssh的测试服务器以进行开发,或者在本地PC上安装ssh服务器.对于Windows盒,最好的选择是 cygwin ,这将允许您模拟posix系统并在您的计算机上运行SSHD本地机器.谷歌搜索cygwin和sshd将为您提供有关如何进行设置的示例.
To resolve this issue you need to either find a test server which does have ssh running on it to do your development against or install an ssh server on your local PC. For a windows box your best bet is cygwin, which will allow you to emulate a posix system and run SSHD on your local machine. A google search for cygwin and sshd will provide you with examples of how to set this up.
这篇关于com.jcraft.jsch.JSchException:java.net.ConnectException:拒绝连接:connect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!