ssh 库 java_SSH Java库

从2015年初起,埃里诺姆斯就开始保持自己的形象。下面是旧的、不再维护的Gizub链接:

有一个GSOC项目:

代码质量似乎比JSch更好,虽然JSch是一个完整和工作的实现,但缺乏文档。项目页面发现了即将发布的beta版本,最后一次提交到存储库是在8月中旬。

比较API:SSHClient ssh = new SSHClient();

//ssh.useCompression();

ssh.loadKnownHosts();

ssh.connect("localhost");

try {

ssh.authPublickey(System.getProperty("user.name"));

new SCPDownloadClient(ssh).copy("ten", "/tmp");

} finally {

ssh.disconnect();

}Session session = null;Channel channel = null;try {JSch jsch = new JSch();session = jsch.getSession(username, host, 22);

java.util.Properties config = new java.util.Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);

session.setPassword(password);session.connect();// exec 'scp -f rfile' remotelyString command = "scp -f " + remoteFilename;

channel = session.openChannel("exec");((ChannelExec) channel).setCommand(command);

// get I/O streams for remote scpOutputStream out = channel.getOutputStream();InputStream in = channel.getInputStream();

channel.connect();byte[] buf = new byte[1024];// send '\0'buf[0] = 0;out.write(buf, 0, 1);out.flush();while (true) {

int c = checkAck(in);

if (c != 'C') {

break;

}

// read '0644 '

in.read(buf, 0, 5);

long filesize = 0L;

while (true) {

if (in.read(buf, 0, 1) 

// error

break;

}

if (buf[0] == ' ') {

break;

}

filesize = filesize * 10L + (long) (buf[0] - '0');

}

String file = null;

for (int i = 0;; i++) {

in.read(buf, i, 1);

if (buf[i] == (byte) 0x0a) {

file = new String(buf, 0, i);

break;

}

}

// send '\0'

buf[0] = 0;

out.write(buf, 0, 1);

out.flush();

// read a content of lfile

FileOutputStream fos = null;

fos = new FileOutputStream(localFilename);

int foo;

while (true) {

if (buf.length 

foo = buf.length;

} else {

foo = (int) filesize;

}

foo = in.read(buf, 0, foo);

if (foo 

// error

break;

}

fos.write(buf, 0, foo);

filesize -= foo;

if (filesize == 0L) {

break;

}

}

fos.close();

fos = null;

if (checkAck(in) != 0) {

System.exit(0);

}

// send '\0'

buf[0] = 0;

out.write(buf, 0, 1);

out.flush();

channel.disconnect();

session.disconnect();}} catch (JSchException jsche) {

System.err.println(jsche.getLocalizedMessage());} catch (IOException ioe) {

System.err.println(ioe.getLocalizedMessage());} finally {

channel.disconnect();

session.disconnect();}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值