我编写了一个小型 Java 程序,它使用 Jsch 将多个文本文件传输到远程服务器。由于单个文件可能会变得很大,因此传输最多需要 20 秒。
在远程服务器上,生成的文件将在我无法控制的不同时间点被访问(读取)。我已经测试过在服务器上进行 SFTP 传输期间复制文件。复制的文件没有完整所需的完整数据,因为在执行复制命令时它尚未完全可用。
如何确保文件传输完全完成后才能被访问,从而使文件能够被完整读取?由于我无法控制远程服务器上的文件访问,因此我需要一种方法来从我的 Java 程序中执行此操作。
这是我编写的代码的相关部分:
InputStream contentInputStream = null;
try {
contentInputStream = new ByteArrayInputStream(Files.readAllBytes(Paths.get("test1.txt")));
} catch (IOException e) {
e.printStackTrace();
}
sftpChannel.put(contentInputStream, "abc.txt");
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 JSch 与私钥配置一起使用。我已使用 PuTTYgen 生成了公钥和私钥,但不确定如何处理这两个文件。
哪个密钥(公共/私有)需要传输到服务器?
我正在尝试使用 JSch 将文件从 Windows 机器发送到 Linux 机器。因此,我将主机公钥从 Linux 机器复制到我的 Windows 机器,并将密钥添加到我的HostKeyRepository. 但由于某种原因,我收到“无效的密钥类型”异常。这是我的代码:
HostKeyRepository repo = jsch.getHostKeyRepository();
File file = new File("D:\\Uni\\Arbeit\\ssh_host_rsa_key.pub");
byte[] HK = Files.readAllBytes(file.toPath());
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
HostKey hk = new HostKey(null, HK);
repo.add(hk, null);
session.connect();
Run Code Online (Sandbox Code Playgroud) 我有两个 GitHub 帐户,它们都有不同的 SSH 密钥。
如果我使用终端检查连接,我可以使用两者进行连接,并且响应消息具有相应的用户名。
关键1:
ssh -i ~/.ssh/id_rsa.pub git@github.com
PTY allocation request failed on channel 0
Hi m-aamir-ashfaq! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
Run Code Online (Sandbox Code Playgroud)
关键2:
ssh -i ~/.ssh/id2_rsa.pub git@github.com
PTY allocation request failed on channel 0
Hi mutong2! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
Run Code Online (Sandbox Code Playgroud)
这里好像还行
但是当我使用 java (JSch) 时,我可以使用密钥 1 进行连接,但不能使用密钥 2 进行连接。
以下是代码:
JSch jsch = new JSch();
String host ="git@github.com"; …Run Code Online (Sandbox Code Playgroud) 我使用 Apache Common NetFTPClient并在上传文件之前使用如下所示的方法进行设置ftpClient。
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用 JSch。文件上传之前是否需要相同的设置以及它们的外观?谢谢。
我正在使用JSch从SFTP服务器获取文件,但我试图想办法只获取最旧的文件,并确保它当前没有被写入.我想象自己这样做的方法是首先找到指定的远程文件夹中的哪个文件是最旧的.然后我会检查文件大小,等待x秒(可能大约10秒,只是为了安全),然后再次检查.如果文件大小没有改变,我下载文件并进行处理.但是,我不知道该怎么做!如果有人知道如何做到这一点,或者知道其他支持具有此内置功能的SFTP(我知道Apache Commons,但只有FTPS),我们将不胜感激.
提前致谢.
我正在尝试连接到主机,然后使用"su - john"更改用户,然后执行命令为john.是否可以只使用JSch?
问题是,在我创建一个会话并打开通道并执行上述命令后,它应该请求密码,但没有任何反应.
这是我连接到远程机器的方式:
String address = "myremote.computer.com";
JSch jsch = new JSch();
String user = "tom";
String host = address;
String password = "l33tpaSSw0rd";
Session session = jsch.getSession( user, host, 22 );
java.util.Properties config = new java.util.Properties();
config.put( "StrictHostKeyChecking", "no" );
session.setConfig( config );
session.setPassword( password );
session.connect();
Run Code Online (Sandbox Code Playgroud)
然后我通过runSshCommand()方法执行命令,如下所示:
try
{
Channel channel = session.openChannel( "exec" );
channel.setInputStream( null );
channel.setOutputStream( System.out );
( (ChannelExec) channel ).setCommand( command );
channel.connect();
InputStream in = channel.getInputStream();
byte[] tmp …Run Code Online (Sandbox Code Playgroud) 我明白有重复的>>>从副本中复制>>>只要您的本地机器有一台运行<<<<<的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); …Run Code Online (Sandbox Code Playgroud) 我知道有人问过几次,但我已经尝试了许多已经接受的解决方案。
我正在使用JSch创建一个简单的SSH隧道。而且我一直在日志中收到此错误以及与此相关的信息:
INFO: diffie-hellman-group14-sha1 is not available.
Run Code Online (Sandbox Code Playgroud)
我已经将Java无限策略文件添加到了正确的文件夹中,并且将该算法添加到了sshd_config文件中的KexAlgorithms部分。以下是完整的日志明细。
INFO: Connecting to xx.xx.xxx.xxx port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_6.8
INFO: Local version string: SSH-2.0-JSCH-0.1.50
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192- cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: CheckKexes: diffie-hellman-group14-sha1
INFO: diffie-hellman-group14-sha1 is not available.
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
INFO: kex: server: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ssh-ed25519
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
INFO: kex: server: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
INFO: kex: server: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: …Run Code Online (Sandbox Code Playgroud) 我们的Gerrit项目之一在某个日期之后停止工作,并出现以下错误:
Gerrit日志中的错误/ var / gerrit / logs / error_log:[2016-07-29 17:59:51,676]
com.googlesource.gerrit.plugins.replication.ReplicationQueue错误:无法复制到git@bitbucket.org:company / product.git org.eclipse.jgit.errors.TransportException:git@bitbucket.org:company / product.git:渠道没有打开。在org.eclipse.jgit.transport.JschSession $ JschProcess。(JschSession.java:154)在org.eclipse.jgit.transport.JschSession $ JschProcess。(JschSession.java:118)在org.eclipse.jgit.transport.JschSession org.eclipse.jgit.transport.TransportGitSsh $。 org.eclipse.jgit.transport.Transport.push(Transport.java:1167)的.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:154)org.eclipse.jgit.transport.Transport.push(Transport) com.googlesource.gerrit.plugins.replication.PushOne.pushVia(PushOne.java:307)处的com.googlesource.gerrit.plugins.replication.PushOne.runImpl(PushOne.java:252)处的.java:1213)。 googlesource.gerrit.plugins.replication.PushOne。com.googlesource.gerrit.plugins.replication.PushOne.access $ 000(PushOne.java:71)处的runPushOperation(PushOne.java:207)com.googlesource.gerrit.plugins.replication.PushOne $ 1.call(PushOne.java: 186),位于com.google.gerrit.server.util.RequestScopePropagator的com.google.gerrit.server.util.RequestScopePropagator $ 5.call(Request.Propergator.java:222)的com.googlesource.gerrit.plugins.replication.PushOne $ 1.call(PushOne.java:183)处。 com.google.gerrit.server.git.PerThreadRequestScope $ Propagator $ 1.call(PerThreadRequestScope.java:75)的com.google.gerrit.server.git.PerThreadRequestScope $ Propagator $ 1.call(PerThreadRequestScope.java:75)的com.googlesource.gerrit.plugins .replication.PushOne.run(PushOne.java:183)(位于java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)位于java.util.concurrent.FutureTask.run(FutureTask.java:262) java.util.concurrent。在com.google.gerrit.server.git.WorkQueue $ Task.run()处的ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201(ScheduledThreadPoolExecutor.java:178)在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)。 .java:337),位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145),位于java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615),位于java.lang.Thread.run( Thread.java:745)由以下原因引起:java.lang.Thread.run(Thread.java:745)上的runWorker(ThreadPoolExecutor.java:1145)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)在原因:java.lang.Thread.run(Thread.java:745)上的runWorker(ThreadPoolExecutor.java:1145)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)在原因: com.jcraft.jsch.JSchException:通道未打开。在org.eclipse.jgit.transport.JschSession $ JschProcess。(JschSession.java:150)的com.jcraft.jsch.Channel.connect(Channel.java:197)处... 25另外
我们的Gerrit版本是2.6.1,安装在AWS的CentOS 6.8版上。
我的gerrit复制配置是:
[remote "bitbucket"]
url = git@bitbucket.org:qpidhealth/${name}.git
push …Run Code Online (Sandbox Code Playgroud)