将多个文件从 Windows 计算机发送到 Linux 远程服务器“Jsch 代码”
smoeone 可以尝试使用 Jsch 将多个文件发送到远程服务器吗?这可能吗?
我正在尝试发送多个脚本 perl 来检查远程服务器中的一些现有文件和命令,之后必须在本地计算机中返回包含结果的文件,问题是我必须为此使用 eclipse。
错误:
Bareword found where operator expected at /root/port.pl line 27, near "10.156.31.46 ssh"
(Missing operator before ssh?)
Bareword found where operator expected at /root/port.pl line 27, near "8HcmdrGQDgXKL"
(Missing operator before HcmdrGQDgXKL?)
Bareword found where operator expected at /root/port.pl line 27, near "9N1XgcLKUcC1DgFmcYoNLb1VsU9KYRhLAOGiIjWBdRkr6gLhZTJsnd2e464"
(Missing operator before N1XgcLKUcC1DgFmcYoNLb1VsU9KYRhLAOGiIjWBdRkr6gLhZTJsnd2e464?)
Bareword found where operator expected at /root/port.pl line 27, near "2iIe3zJSlBH1tVd8PCifJaZ6Xu4KqsDFWjIILv8wY5XMWYdxVcPcMOsQhpl7vB2EgZxrc3yxE6rwfGnyxEKWez9W9t6e2hOvx3J21PxAGTaCIPjgxbcM4G"
(Missing operator before iIe3zJSlBH1tVd8PCifJaZ6Xu4KqsDFWjIILv8wY5XMWYdxVcPcMOsQhpl7vB2EgZxrc3yxE6rwfGnyxEKWez9W9t6e2hOvx3J21PxAGTaCIPjgxbcM4G?)
syntax error at /root/port.pl line 27, near "10.156.31.46 ssh"
Execution of /root/port.pl aborted due to compilation errors.
exit-status: 255
当我使用 FileOutput 时会出现此错误
java.io.FileNotFoundException: C:\Users\Computer\workspace\proj (Accès refusé)
Could smoeone try to send multiple files to a remote server with Jsch, is this can be possible!!
I am trying to send a multiple of scripts perl to check some existing files and commandes in the remote server and after that a file which contains results must be returned in my local machine,the problem is that I have to use eclipse for that.
errors:
Bareword found where operator expected at /root/port.pl line 27, near "10.156.31.46 ssh"
(Missing operator before ssh?)
Bareword found where operator expected at /root/port.pl line 27, near "8HcmdrGQDgXKL"
(Missing operator before HcmdrGQDgXKL?)
Bareword found where operator expected at /root/port.pl line 27, near "9N1XgcLKUcC1DgFmcYoNLb1VsU9KYRhLAOGiIjWBdRkr6gLhZTJsnd2e464"
(Missing operator before N1XgcLKUcC1DgFmcYoNLb1VsU9KYRhLAOGiIjWBdRkr6gLhZTJsnd2e464?)
Bareword found where operator expected at /root/port.pl line 27, near "2iIe3zJSlBH1tVd8PCifJaZ6Xu4KqsDFWjIILv8wY5XMWYdxVcPcMOsQhpl7vB2EgZxrc3yxE6rwfGnyxEKWez9W9t6e2hOvx3J21PxAGTaCIPjgxbcM4G"
(Missing operator before iIe3zJSlBH1tVd8PCifJaZ6Xu4KqsDFWjIILv8wY5XMWYdxVcPcMOsQhpl7vB2EgZxrc3yxE6rwfGnyxEKWez9W9t6e2hOvx3J21PxAGTaCIPjgxbcM4G?)
syntax error at /root/port.pl line 27, near "10.156.31.46 ssh"
Execution of /root/port.pl aborted due to compilation errors.
exit-status: 255
this errors apears when I use FileOutput
java.io.FileNotFoundException: C:\Users\Computer\workspace\proj (Accès refusé)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这可以相当简单地完成。您需要打开一个通道并将二进制对象放入通道中适当的位置。如果您想对多个文件执行此操作,您可以按顺序执行或连续提交给执行器。
这当然是使用jSch。有关更多信息,您可以查看此示例
Yes, it can be done rather simply. You need to open a channel and simply put the binary object into the channel with the appropriate location. If you want to do it with multiple files you can either do it sequentially or continuously submit to an Executor.
This is of course using jSch. For more information you can check out this example
我使用 java 和 Ant 脚本为 jsch SCP Get 和 Put 创建了教程。
jsch 教程
I created tutorials for jsch SCP Get and Put using java and Ant script.
jsch tutorials
您一次只能发送一个文件——但也许您可以将其与 ZipInput/OutputStream 结合使用,从而以这种方式发送多个文件?
You can only send one file at a time -- but perhaps you can use this in combination with a ZipInput/OutputStream and as such send multiple files this way?
是的,但这并不容易。 Sftp 确实允许在一个会话中发送多个文件。我不知道 scp 在发送多个文件时是否使用一个会话。
我建议使用 ant 任务 或使用 apache Camel 拥有更高层次、易于使用的界面来处理。
ant 任务是一个简单易用的工具,只完成一项工作。
另一方面,Camel 是一种非常灵活的工具,可以帮助解决基于企业集成模式的不同系统之间传输信息时的各种问题。它还提供了更多功能,使这些传输具有“工业强度”。
Yes, but it is not easy. Sftp does allow sending mutiple files in one session. I do not know if scp uses one session or not when it sends multiple files.
I would recommend using the ant task or using apache camel to have a higher level, easy to use interface to deal with.
The ant task is a straightforward simple to use tool which does one job.
Camel on the other hand is a very flexible tools helping with all kind of problems when transferring information between different systems based on the Enterprise Integration Patterns. It offers also more features to make these transfers "industrial strength".
问题澄清后进行编辑。
您需要一个
ChannelSftp
,并使用它的一个(或多个)put
方法。多次调用它们没有问题,甚至同时调用它们(尽管这不会增加带宽,但它可能有助于高延迟。(请参阅约翰的答案以获取示例)。然后打开一个
ChannelExec
< /a> 来执行脚本。您通常可以向通道发出多个命令(用;
或换行符分隔),它会将它们传递给 shell,然后 shell 会顺序执行它们。频道在这里(一个接一个或同时)。最后一个命令完成后,您可以使用
ChannelSftp
再次下载文件(此处使用get
方法)。这与 Eclipse 并没有真正的关系,除了如何将库包含在构建和运行路径中(如果是这样,请在注释中说明)。
另一种选择是,如果您确实不需要将脚本作为服务器上的文件,则可以使用 perl 命令的变体,该命令允许在命令行上(即在exec 通道命令字符串)或作为标准输入(即在 exec 通道的输入流中)。有关详细信息,请参阅 perlrun 文档。
您可以调整脚本,使结果不会写入文件中,而是作为输出生成(然后您可以从 exec 通道输出中读取)。
我在我的版本中添加了一个 示例 JSch 示例集合。
这是重要的部分:
好的,让我们看看错误。
首先,您的 Perl 脚本中有一些错误。第 27 行看起来语法错误。看一下这一行,如果没有发现问题,请就此提出一个新问题。
(您应该首先尝试使用普通 SSH 连接运行此程序,而不使用 JSch,以隔离问题。或者更好的是,首先尝试在计算机上本地运行它。)
其次,您的 FileNotFoundException(您应该使用
e.顺便说一句,printStackTrace()
而不是chatch 块中的System.out.println(e);
)。这个
Accès refusé
表示你的程序无权在这里写。从您的其他路径来看,这不是文件名,而是目录名。您不能用文件覆盖目录。使用文件应保存的名称:Edited after the question was clarified.
You need a
ChannelSftp
, and use one (or more) of it'sput
methods. There is no problem in invoking them more than once, even concurrently (though this does not increase the bandwith, it might help for high-latency . (See John's answer for an example).Then open a
ChannelExec
to execute your scripts. You can usually give multiple commands (separated by;
or newlines) to the channel, it will pass them to the shell, which will execute them sequentially. Alternatively, use multiple channels here (one after another or concurrently).After the last command finished, you use the
ChannelSftp
again to download the file (use aget
method here).This is not really related to Eclipse, other than maybe how to include the library in the build and run path (if so, state in a comment).
An alternative, if you do not really need to have the scripts as files on the server, would be to use a variant of the
perl
command which allows giving the script either on the command line (i.e. in the exec channels command string) or as standard input (i.e. in the exec channel's input stream). See the perlrun documentation for details.You could adapt your scripts that the result will not be written in a file, but produced as output (which you then can read from the exec channels output).
I added an example to my version of the JSch examples collection.
Here the important parts:
Okay, let's look at the errors.
First, you have some errors in your Perl script. It looks like wrong syntax in line 27. Have a look at this line, and if you don't find the problem, make a new question about this.
(You should first try to run this with a plain SSH connection, without JSch, to isolate the problems. Or better, first try to run it locally on your computer.)
Second, your FileNotFoundException (you should have used
e.printStackTrace()
instead ofSystem.out.println(e);
in the chatch block, by the way).This
Accès refusé
means that your program has no right to write here. From your other paths, this is not a file name, but a directory name. You can't overwrite a directory with a file. Use the name your file should be saved under: