我正在使用jcraft库,以便使用scp将文件从一台服务器发送到另一台服务器。代码是这样的

public class Scp {
 String DestinationHost;//host IP
 String DestinationUserName;//username
 String DestinationPassword;//password
 String DestinationPublicKeyFile;//public key-if any
 String DestinationDirectory;//where to save on remote
 String SourceFile;
 /*
    setters-getters
  */
 public void send() throws  SftpException, FileNotFoundException, JSchException{
    JSch jsch = new JSch();
    Session session = null;
    session = jsch.getSession(DestinationUserName,DestinationHost,22);
    jsch.addIdentity(getDestinationPublicKeyFile(),getDestinationPassword());

    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();

    ChannelSftp channel = null;

    //try to connect
    channel = (ChannelSftp)session.openChannel("sftp");
    channel.connect(30000);
    channel.connect();

    File localFile = new File(getSourceFile());
    File remoteFile=new File(getDestinationDirectory());
    channel.cd(remoteFile.getParent());

    channel.put(new FileInputStream(localFile),//the source file
    remoteFile.getParentFile().getName());//the destination name(not the path)

    channel.disconnect();
    session.disconnect();
  }
}


这是从另一个java类中多次调用的,每次都创建一个新对象,如下所示

Scp scp=new Scp();

scp.setDestinationHost(CopyDestHost);
scp.setDestinationPassword(CopyDestPassword);
scp.setDestinationPublicKeyFile(CopyDestKey);
scp.setDestinationUserName(CopyDestUser);
scp.setSourceFile(temp.getAbsolutePath());
scp.setDestinationDirectory(filepath);

stream.flush();
stream.close();
scp.send();


由于我正在使用channel.disconnect();session.disconnect();断开连接,为什么在关闭连接的几个小时后,为什么我的远程计算机上运行着大量的sshd进程列表?例如

root     13251   863  0 11:54 ?        00:00:00 sshd: skaros [priv]
user     13300 13251  0 11:54 ?        00:00:00 sshd: skaros@notty
skaros   13301 13300  0 11:54 ?        00:00:00 /usr/lib/openssh/sftp-server
root     14885   863  0 10:35 ?        00:00:00 sshd: skaros [priv]
skaros   14986 14885  0 10:35 ?        00:00:00 sshd: skaros@notty
skaros   14987 14986  0 10:35 ?        00:00:00 /usr/lib/openssh/sftp-server


那是问题吗?我应该手动杀死他们吗?我会像那样离开他们吗?

最佳答案

原来是一个“ bug”。 This post以此解决了

  while (channel.getExitStatus() == -1){
       try{Thread.sleep(1000);}catch(Exception e){System.out.println(e);}
  }

08-05 14:28