我知道有重複>>>從副本複製>>>只要您的本地計算機有一個運行的SSH服務器< < < < <,但我不能發表評論,不能從問題(和IM不提供答案....)com.jcraft.jsch.JSchException:java.net.ConnectException:連接被拒絕:連接
它說,「只要你的本地機器具有運行SSH服務器」,但我不知道如何運行SSh服務器。我打開我的膩子(雙擊它)(不知道這意味着SSH(?膩子?)服務器(?)正在運行...無疑讓...
IM真正的新Socket編程我。我利用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();
}
}
BTW我使用commandprompt到ping 127.0.0.1是好的,但如果我使用Telnet 127.0.0.1它說無法打開連接到主機(我打開膩子(?雙擊?),端口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的替代品..任何建議歡迎!)但是當我嘗試運行該示例時,它說沒有主要。哪個..我duno>。 <將堅持w JSch到那時......
順便說一句,我嘗試https://serverfault.com/questions/185153/free-public-ssh-server-for-testing-purposes服務器,但...我不知道什麼是地址,用戶名和密碼。 (我也有一個http://sdf.org帳戶新建但是當我嘗試連接到它,它說unknownhost。僅供參考!)
忘了提,使用Windows 7 IM和「蔭」是不是我的命令的命令提示...
您需要安裝SSH服務器。膩子只作爲您的SSH客戶端。 – 2014-12-02 04:49:45