- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道有重复的 >>> 从副本复制 >>>只要您的本地计算机有一个运行的 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();
}
}
最佳答案
您正在尝试通过 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/
我搜索了很多,但我找不到很好的答案。我使用 try_catch block 来处理这个异常(如果抛出这个异常,将向用户显示一帧,我会告诉他/她一个消息),但它仍然在控制台中显示异常。请帮助我。谢谢。
我不明白这一点...我正在 try catch java.net.ConnectException,以防我的下游 API 处于离线状态。但是 Eclipse 警告我它无法访问 - 暗示代码不能抛出 C
这个问题在这里已经有了答案: Java RMI : connection refused (2 个答案) 关闭 5 年前。 当我尝试访问服务器 lonrs07471 (linux) 上的应用程序时出
我使用下面的 Java 代码来发送电子邮件。 import java.util.*; import javax.mail.*; import javax.mail.internet.*; import
所以简而言之,我只是想得到一个小的工作骨架程序,我可以用它来了解 Http 通信并“感觉”我的方式来弄清楚我最终需要什么来构建一个更大的程序我正在努力。此处的这段特定代码实际上只是 Apache 库示
所以我有一个通过Socket与服务器通信的android应用程序。昨天在某个时候工作。晚上,我想做更多的工作,但是它根本引发了异常,而我根本没有修改代码。 重建项目2次后,我测试了一个应用程序的连接,
我尝试通过REST发送请求。 使用firefox rest-client时,我得到成功响应, 但是在SoapUI中,我在下面附加了异常。 如何解决这个问题? 谢谢。 java.net.ConnectE
如果远程参与者因断电而无法使用,监督策略可以处理这种情况吗? 我已经对示例进行了编码,并且已经关闭了远程参与者系统,但似乎监督策略仅考虑远程参与者在到达后抛出的异常。 由于我不是 Akka 专家,我想
我正在尝试使用以下脚本将一些本地文件复制到HDFS: bin/hadoop fs -copyFromLocal '/home/czy/IdeaProjects/HadoopInAction/First
我有以下代码片段: private static String post(String msg) throws Exception { URL urlObj = new URL(host);
我正在将 NetBeans 与 Java 结合使用。我正在尝试从需要日志记录的网站中提取数据,我进入一个帐户提取数据,然后进入另一个帐户,我的代码正在工作,但总是在一段时间后得到: java.net.
我创建了一个简单的 java RMI 程序来理解它是如何工作的。但是当我尝试运行我的服务器端时,它引发了以下异常。 编辑:我们正在使用代理连接... Remote exception: java.r
我正在开发一个使用套接字的 Android 应用程序。我有一个名为 initializeStreams() 的函数,它打开套接字并尝试连接。如果无法建立连接,此函数将抛出 ConnectExcepti
我有一个像这样定义的kafka制作人 public KafkaMessageProducer(String kafkaHost, String kafkaPort, Map map) {
我有 2 个虚拟机(Ubuntu),第一个作为主机,第二个作为从机,当我在远程机器上运行测试时,我得到以下信息: rconfigure() 方法 java.rmi.ConnectException 中
我想使用 Groovy 脚本访问网页。但是我支持代理。 这是一个失败的测试脚本...... println "Google page is..." println 'http://www.google
我有一个从eclipse运行的android模拟器,只是试图连接到我在.NET(C#)中编写的简单套接字服务器。 服务器在端口4505的同一台计算机上运行。我能够通过PuTTy telnet到服务器,
我正在使用用于Apache-spark的scala API编写一个简单的独立程序。我需要从HDFS访问文件。来自shell的简单命令(例如hadoop fs -ls)可以正常工作。但是,当我尝试从程序
连接被拒绝,因为服务器未按预期处理连接。我希望服务器使用线程或池,以便它可以处理多个客户端。如果客户端断开连接,就像这里发生的那样,这不会对服务器产生影响,也不会阻止其他客户端连接。 将此功能添加到服
我正在尝试实现 TCP 连接,从服务器端一切正常,但当我运行客户端程序(从客户端计算机)时,出现以下错误: java.net.ConnectException: Connection refused
我是一名优秀的程序员,十分优秀!