- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 Apache Mina 和 JSch 编写单元测试,我遇到了一个问题,我确定这与我在 Mina 上设置文件系统的方式有关。
这是Mina 设置代码:
sshd = SshServer.setUpDefaultServer();
sshd.setPort(8002);
sshd.setFileSystemFactory(new NativeFileSystemFactory() {
@Override
public void setCreateHome(boolean createHome)
{
super.setCreateHome(true);
}
@Override
public FileSystemView createFileSystemView(final Session session) {
String userName = session.getUsername();
// create home if does not exist
String homeDirStr = "/home/testusr";
File homeDir = new File(homeDirStr);
if ((!homeDir.exists()) && (!homeDir.mkdirs())) {
System.out.println("Cannot create user home :: " + homeDirStr);
}
return new NativeFileSystemView(session.getUsername(), false) {
@Override
public String getVirtualUserDir() {
return "/home/testusr";
}
};
};
});
sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.setCommandFactory(new ScpCommandFactory());
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthNone.Factory());
sshd.setUserAuthFactories(userAuthFactories);
sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
public boolean authenticate(String username, PublicKey key, ServerSession session) {
return true;
}
});
sshd.start();
JSch代码:
JSch jsch = new JSch();
String appPublicKey = "c:\\conf\\test_private";
jsch.addIdentity(new File(appPublicKey).getAbsolutePath());
com.jcraft.jsch.Session session = jsch.getSession("testusr","localhost", 8002);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(30000);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
String filename = "c:\\temp\\test.tar.gz";
File f = new File(filename);
sftpChannel.put(new FileInputStream(f), "/home/testusr");
异常:
4:
at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2403)
at com.jcraft.jsch.ChannelSftp.getCwd(ChannelSftp.java:2412)
at com.jcraft.jsch.ChannelSftp.remoteAbsolutePath(ChannelSftp.java:2904)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:517)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:492)
at com.SftpServiceTest.sendFile(SftpServiceTest.java:183)
at com.SftpServiceTest.main(SftpServiceTest.java:218)
Caused by: java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2871)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2895)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2315)
at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2397)
... 6 more
我似乎找不到任何创建 FileSystemView 的好例子。希望有人能帮助我,我已经坚持了好几天了!
提前致谢。
最佳答案
这就是我最终在单元测试中设置 Jsch 和 Mina 的方式:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:context.xml"})公共(public)类 SftpTaskletLetterTest 扩展 AbstractAomTest {
@Autowired
private SftpTaskletLetter cut;
private SshServer sshd = null;
@Before
public void setUp() throws Exception {
sshd = createAndStartSSHServer();
}
@After
public void tearDown() throws Exception {
sshd.stop();
}
@Test
public void testPutAndGetFile() throws Exception {
JSch jsch = new JSch();
Hashtable<String, String> config = new Hashtable<>();
JSch.setConfig(config);
Session session = jsch.getSession("assentisftp", "127.0.0.1", 22);
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.connect();
// Channel channel = session.openChannel("session"); // works
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
final String testFileContents = "some file contents";
String uploadedFileName = "uploadFile";
sftpChannel.put(new ByteArrayInputStream(testFileContents.getBytes()), uploadedFileName);
String downloadedFileName = "downLoadFile";
sftpChannel.get(uploadedFileName, downloadedFileName);
File downloadedFile = new File(downloadedFileName);
assertTrue(downloadedFile.exists());
if (sftpChannel.isConnected()) {
sftpChannel.exit();
}
if (session.isConnected()) {
session.disconnect();
}
}
public static SshServer createAndStartSSHServer() throws IOException {
SshServer result = SshServer.setUpDefaultServer();
result.setPort(22);
result.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
result.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
return "assentisftp".equals(username);
}
});
result.setSubsystemFactories(Arrays.<NamedFactory<Command>> asList(new SftpSubsystem.Factory()));
result.start();
return result;
}
@Override
protected MandatorType getMandator() {
return MandatorType.MAN;
}
public static class MyUserInfo implements UserInfo {
@Override
public String getPassphrase() {
return "";
}
@Override
public String getPassword() {
return "";
}
@Override
public boolean promptPassword(String message) {
return true;
}
@Override
public boolean promptPassphrase(String message) {
return true;
}
@Override
public boolean promptYesNo(String message) {
return true;
}
@Override
public void showMessage(String message) {
}
}
关于java - Apache Mina 和 JSch 无法设置主目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312011/
我正在使用 jsch 进行 sftp 文件传输。当我通过设置缓冲区大小 512(-B 选项)sftp B 512 [sftp 服务器名称] 并调用 put 命令使用 sftp 命令发送文件时,我可以以
我正在尝试使用 JSch 连接到服务器,然后执行交互式 su 命令。 你能告诉我我错过了什么吗?这些东西每次都会挂起,我认为这种行为没有任何理由。特别是在发送密码之前,我专门等待密码字符串(实际上是
在 JSch 中每个 session 可以打开多少个 channel 是否有限制?我试图打开的 channel 是“ChannelShell”类型 我收到“com.jcraft.jsch.JSchEx
我正在尝试从我的 android 手机通过 ssh 连接到 ssh 服务器。 我的进口线: import com.jcraft.jsch.*; 剩下的就在这里 try{
尝试使用 ssh key 身份验证连接到主机。下面是我的代码: package com.mkyong.common; import com.jcraft.jsch.Channel; import co
我正在将一些 java 代码移植到 GRAILS 框架中,但在导入库 (jcraft) 时不断收到此错误: import com.jcraft.jsch.ChannelSftp; import com
我编写了一个连接到远程 LINUX 计算机并执行命令的程序。我使用 jsch-0.1.42.jar 文件连接到远程计算机。我的程序运行良好并返回输出。现在我想调用方法,该方法是使用以下代码从 JSP
当使用 jsch 版本 0.1.51 连接到远程主机时,我们偶尔会在 ChannelExec 上调用 Channel.connect() 时遇到以下异常。 com.jcraft.jsch.JSchEx
GerritTrigger插件以前工作得很好,但从一个我无法确定的时间点来看,连接永远无法建立,错误消息如下:。Gerit触发器版本:2.39.0。SSH密钥是通过ssh-keygen生成的。我注意到
我正在尝试连接到我的一个 unix 服务器,但由于上述错误而失败,请有人帮忙。 JSch jsch = new JSch(); java.util.Properties configuration =
我使用他下面的代码连接到 ssh 服务器。 JSch jsch = new JSch(); Session session = jsch.getSession("username", "ip", po
我有运行 ssh 服务器的 windows 机器。我知道那台机器上的路径。让它成为 D:/Folder1/Folder2 .我正在使用 JSCH 创建 sftp channel 。但是当我尝试 cd
我正在使用 Jsch 连接 sshd 服务器,我是这方面的新手。我成功发送了非控制字符,例如 a、b ...等等。但是,我需要发送“Esc”键命令(控制字符)来执行特定任务。根据link ,我尝试了几
我无法将一台unix服务器的安全服务器连接到另一台unix服务器。通过ssh从putty我可以轻松连接,但从jsch连接时出现以下错误。 成功putty步骤->在putty中连接server1->成功
我的命令有第一部分,其结果通过管道传输到第二部分。当我运行代码时,我总是返回第一部分的结果,而第二部分应用的过滤器显然没有执行。 所以我得到: CONTAINER ID IMAGE COMMAN
我正在尝试使用 JSCH 库通过 SSH 执行多个命令。我面临一个奇怪的问题。当我尝试在 Eclipse 中运行该程序时,代码完成了执行,但命令(mkdir test)未在服务器中执行但是当我一步步调
我正在使用 JSCH 通过 SSH 访问主机,然后启动一些命令。 主机未使用“通用”操作系统,因为它是 BNG:http://en.wikipedia.org/wiki/Border_Network_
好吧,我收到 UnknownHostKey 异常。我不想像这篇文章 com.jcraft.jsch.JSchException: UnknownHostKey 那样使用 StrictHostKeyCh
我正在使用 JSch 执行 sftp get 操作。当我使用 main 方法运行测试时,一切都很好,但使用单元测试时我遇到了问题。错误如下: java.lang.RuntimeException: c
我正在使用 Jsch lib 访问网络中的路由器,但在某些路由器中存在登录横幅。所以我需要读取输出,识别横幅,将其删除并仅获取命令的输出。我正在执行模式下使用 channel 。我认为的一个解决方案是
我是一名优秀的程序员,十分优秀!