sftp java 客户端在远程机器上保留 sshd 进程 运行
sftp java client keeps sshd process running on remote machine
我正在使用 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 class 调用了好几次,每次都在创建一个新对象,就像这样
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"。 用这个解决了
while (channel.getExitStatus() == -1){
try{Thread.sleep(1000);}catch(Exception e){System.out.println(e);}
}
我正在使用 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 class 调用了好几次,每次都在创建一个新对象,就像这样
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"。
while (channel.getExitStatus() == -1){
try{Thread.sleep(1000);}catch(Exception e){System.out.println(e);}
}