我明白有重复的>>>从副本中复制>>>只要您的本地机器有一台运行<<<<<的SSH服务器,但我无法发表评论,也不能从问题中解决(并且我没有提供答案. ...)
它声明"只要您的本地计算机运行SSH服务器",但我不知道如何运行SSh服务器.我打开我的腻子(双击它)(不确定这是否意味着SSH(?Putty?)服务器(?)正在运行...怀疑...
我是套接字编程的新手.我正在利用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();
}
}
Run Code Online (Sandbox Code Playgroud)
BTW我使用命令提示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)用JSch通过SSH运行命令 和5)我们可以使用JSch进行基于SSH密钥的通信吗?
并且......谢谢是提前!!
哦,还有theres也是http://www.ganymed.ethz.ch/ssh2/(JSch 的替代品......任何建议欢迎!)但是当我试图运行这个例子时,它说没有主要的.哪个..我duno>.<会坚持w JSch直到那时....
顺便说一句,我尝试https://serverfault.com/questions/185153/free-public-ssh-server-for-testing-目的服务器,但......我不知道地址,用户名和密码是什么.(我也有一个新创建的http://sdf.org帐户,但是当我尝试连接到它时,它说的是unknownhost.fyi!)
忘了提,我使用Windows 7和'yum'不是命令提示符中的命令...
归档时间: |
|
查看次数: |
31457 次 |
最近记录: |