我使用的是camel版本2.13.1和camel-ftp版本2.13.1。我正在尝试通过骆驼路由连接到 sftp 服务器。我收到一些与 jCraft Jsch 异常相关的错误,如下所示。
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot connect to sftp://uname@serveraddress.com:22
at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:143)
at org.apache.camel.component.file.remote.RemoteFileConsumer.connectIfNecessary(RemoteFileConsumer.java:154)
at org.apache.camel.component.file.remote.RemoteFileConsumer.recoverableConnectIfNecessary(RemoteFileConsumer.java:145)
at org.apache.camel.component.file.remote.RemoteFileConsumer.prePollCheck(RemoteFileConsumer.java:55)
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:106)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:187)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:114)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: com.jcraft.jsch.JSchException: Algorithm negotiation fail
at com.jcraft.jsch.Session.receive_kexinit(Session.java:582)
at com.jcraft.jsch.Session.connect(Session.java:320)
at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:115)
... 14 more
Run Code Online (Sandbox Code Playgroud) 我有一个用户帐户,但在尝试使用此帐户时,出现 rssh 异常。是否有可能使用除ChannelExec. 根据例外情况,我了解到此帐户无法用于ChannelExec授予目录或文件的权限。因此,该帐户是否有任何其他方式可以授予文件权限,而无需访问该用户帐户rssh。请给你想法。
代码 :
channelSftp = (ChannelSftp) channel;
ChannelExec channelexe = (ChannelExec) session.openChannel("exec");
channelexe.setCommand("chmod 777 -R " + depDir);
channelexe.connect();
System.out.println("channelexe.getExitStatus:"+channelexe.getExitStatus());
Run Code Online (Sandbox Code Playgroud)
输出 :
channelSftp = (ChannelSftp) channel;
ChannelExec channelexe = (ChannelExec) session.openChannel("exec");
channelexe.setCommand("chmod 777 -R " + depDir);
channelexe.connect();
System.out.println("channelexe.getExitStatus:"+channelexe.getExitStatus());
Run Code Online (Sandbox Code Playgroud) 我试图构建一个小代码,在其中创建一些字符串并将该字符串传输到位于远程服务器中的文件(应在运行时创建)。就我而言,远程服务器是 Linux。
有人可以帮我吗?我正在使用 JSCH 和 ChannelSftp 但无法执行此操作。下面是我的代码:
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, MachineIP, SFTPPORT);
String str = "Hello";
session.setPassword(SFTPPASS);
System.out.println(SFTPPASS);
java.util.Properties config = new java.util.Properties();
System.out.println("Config done");
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
System.out.println("Config set");
session.connect();
System.out.println("Session connected");
channel = session.openChannel("sftp");
channel.connect();
System.out.println("Connection Opened\n");
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
File f=new File("Test.txt");
//unable to do anything beyond this.
Run Code Online (Sandbox Code Playgroud)
抱歉,如果您觉得这很愚蠢,但我对此很陌生。
我正在尝试使用 Jcraft Jsch 库通过 Java 应用程序管理路由器。
我正在尝试通过 TFTP 服务器发送路由器配置。问题出在我的 Java 代码中,因为它适用于 PuTTY。
这是我的Java代码:
int port=22;
String name ="R1";
String ip ="192.168.18.100";
String password ="root";
JSch jsch = new JSch();
Session session = jsch.getSession(name, ip, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("enable");
channelExec.setCommand("copy run tftp : ");
//Setting the ip of TFTP server
channelExec.setCommand("192.168.50.1 : ");
// Setting the name of file
channelExec.setCommand("Config.txt ");
channelExec.connect();
BufferedReader reader = new BufferedReader(new …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行使用本地 Linux 逻辑路径(例如 )的命令cat $test_dir/test.dat,但逻辑路径$test_dir(这是用户环境变量)无法通过ChannelExec. 但是当我使用 Interactive 时ChannelShell,我可以看到用户变量和命令在交互式会话上运行良好。我只能从“exec”会话中查看系统级环境变量。使用 JSch 库是否可以实现这一点,如果是,那么我该如何实现它,如果不是,我应该使用什么库来实现这一目标?
添加我的类代码如下:
private static final Logger logger = LogManager.getLogger(SecureShell.class);
private String uName;
private String pWord;
private String hName;
private int port;
private Session session = null;
private Channel channel = null;
/**Create an instance to start and stop the remote shell and execute commands
* remotely via java.
*
* @param uName
* host username
* @param pWord
* host password
* @param …Run Code Online (Sandbox Code Playgroud) 我需要从 hadoop 集群连接到 sftp 服务器。我想知道是否有办法从 hdfs 中存储的私钥加载身份。实际上,JSch 对象似乎只接受本地路径:
try {
String privateKeyPath = "hdfs://namenode:8020/path/to/privatekey"; // need this one to be an hdfs path
JSch jsch = new JSch();
jsch.addIdentity(privateKeyPath);
// [..]
}
catch (Exception ex) {
// [..]
}
Run Code Online (Sandbox Code Playgroud)
任何想法?
我使用 Spring Boot 开发了一个调度程序。该调度程序在本地虚拟机中创建文本文件并将这些文件上传到远程 FTP 位置。调度程序每天运行 5 个时间段。最后时段为晚上 11.45。问题是,在晚上 11.45,文件上传无法正常工作,但文件正在本地位置创建。日志包含在这里
2018-10-19 00:00:26.338 ERROR --- [task-scheduler-4] ework.integration.handler.LoggingHandler : org.springframework.messaging.MessageDeliveryException: Error handling message for file [/apps/logs/lesipay-scheduler/to_ctf/ClientCreation18102018_23.txt -> ClientCreation18102018_23.txt]; nested exception is org.springframework.messaging.MessagingException: Failed to write to '/logs/dumpfiles/to_ctf/ClientCreation18102018_23.txt.writing' while uploading the file; nested exception is org.springframework.core.NestedIOException: failed to write file; nested exception is 4: java.io.IOException: inputstream is closed, failedMessage=GenericMessage [payload=/apps/logs/lesipay-scheduler/to_ctf/ClientCreation18102018_23.txt, headers={id=e914c7a2-2b4c-74e2-92d6-b8158ed72874, timestamp=1539886500712}]
at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:321)
at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:283)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:435)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:283)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:273)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:265)
at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:170)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.config.annotation.ServiceActivatorAnnotationPostProcessor$ReplyProducingMessageHandlerWrapper.handleRequestMessage(ServiceActivatorAnnotationPostProcessor.java:98)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at …Run Code Online (Sandbox Code Playgroud) 在jsch中如果我们发出命令cd ../ && pwd并且结果是/home.Next时间如果我执行命令ls将jsch输出我的内容./home我确实期望如此..
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("cd ../ && pwd");
channel.connect();
channel.run();
((ChannelExec)channel).setCommand("ls");
Run Code Online (Sandbox Code Playgroud) 我将它们移动到ftp站点后删除文件时遇到了一些问题.在之前的过程中,我生成文件并将它们放在一个文件夹中.此过程应将每个文件上载到ftp站点,然后删除本地副本.由于原因,我无法弄清楚删除是否失败,但我没有得到任何类型的错误消息或异常.最初我在ftp.put()之后立即调用了file.delete()但是我把它移动到它的当前位置,认为该文件可能被jsch锁定并且调用它会修复它.情况似乎并非如此.理想情况下,呼叫将返回那里,因此我不需要有两个"for"循环.
如果你也碰巧知道为什么覆盖不起作用会产生奖励.当我查看通过实际FTP程序上传的文件时,权限是rw-rw-rw.服务器上的现有文件已通过此过程上载.
这是代码:
JSch jsch = new JSch();
Session session = null;
try {
File sourceDir = new File(certSourceFolder);
if (!sourceDir.exists()) {
sourceDir.mkdirs();
}
session = jsch.getSession(ftpUser, ftpServer, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(ftpPassword);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp ftp = (ChannelSftp) channel;
ftp.cd(certDestFolder);
for (File file : sourceDir.listFiles()) {
if (file.isFile()) {
try {
ftp.put(new FileInputStream(file), file.getName(), ChannelSftp.OVERWRITE);
} catch (SftpException ex) {
//The overwrite is not working. For now I'm just going to trap the error and move …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用JSch在Java中实现图形ssh浏览器。我想检索一个ChannelSftp.ls调用的结果,但是它给了我一个nullpointer异常...我检查了会话,它已打开并登录。当我打开exec的通道并发送ls命令时,我可以检索到目录内容为String。代码如下:
public Vector listContents () {
Channel channel = null;
ConnectionThread conn = SSHBrowser.conn;
Session sess = conn.getSession();
System.out.println(sess.getUserName()); //prints out the given username, not null, so the session is initialized
try {
channel = sess.openChannel("sftp");
} catch (JSchException ex) {
ex.printStackTrace();
}
Vector<ChannelSftp.LsEntry> result = null;
try {
result = ((ChannelSftp) channel).ls("/srv"); // throws nullpointer here
for (ChannelSftp.LsEntry dir : result) {
System.out.print(dir.getLongname());
}
} catch (SftpException e) {
e.printStackTrace();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我得到的堆栈跟踪如下:
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1747) …Run Code Online (Sandbox Code Playgroud)