实现文件上传下载
package com.sinosun;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class Test1 {
public static void main(String[] args)
throws Exception {
downloadSftpFile(
"192.168.238.129",
"root",
"123456",
22,
"/root",
"C:",
"aa.log");
}
/**
* 利用JSch包实现SFTP下载、上传文件(用户名密码方式登陆)
* @param ip 主机IP
* @param user 主机登陆用户名
* @param psw 主机登陆密码
* @param port 主机ssh2登陆端口,如果取默认值(默认值22),传-1
*
*/
public static void sshSftp(String ip, String user, String psw
,
int port)
throws Exception{
System.out.println(
"开始用户名密码方式登陆");
Session session =
null;
JSch jsch =
new JSch();
if(port <=
0){
session = jsch.getSession(user, ip);
}
else{
session = jsch.getSession(user, ip ,port);
}
if (session ==
null) {
throw new Exception(
"session is null");
}
session.setPassword(psw);
session.setConfig(
"StrictHostKeyChecking",
"no");
session.connect(
30000);
sftp_put(session,
"aa.log");
System.out.println(
"sftp成功");
}
private static void sftp_put(Session session, String uploadFileName)
throws Exception {
Channel channel =
null;
try {
channel = (Channel) session.openChannel(
"sftp");
channel.connect(
1000);
ChannelSftp sftp = (ChannelSftp) channel;
sftp.cd(
"/root");
Vector v = sftp.ls(
"/");
for(
int i=
0;i<v.size();i++){
System.out.println(v.get(i));
}
OutputStream outstream = sftp.put(uploadFileName);
InputStream instream =
new FileInputStream(
new File(
"C:\\aa.txt"));
byte b[] =
new byte[
1024];
int n;
while ((n = instream.read(b)) != -
1) {
outstream.write(b,
0, n);
}
outstream.flush();
outstream.close();
instream.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
session.disconnect();
channel.disconnect();
}
}
public static void downloadSftpFile(String ftpHost, String ftpUserName,
String ftpPassword,
int ftpPort, String ftpPath, String localPath,
String fileName)
throws JSchException {
Session session =
null;
Channel channel =
null;
JSch jsch =
new JSch();
session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
session.setPassword(ftpPassword);
session.setTimeout(
100000);
Properties config =
new Properties();
config.put(
"StrictHostKeyChecking",
"no");
session.setConfig(config);
session.connect();
channel = session.openChannel(
"sftp");
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
String ftpFilePath = ftpPath +
"/" + fileName;
String localFilePath = localPath + File.separatorChar + fileName;
try {
chSftp.get(ftpFilePath, localFilePath);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
}
}
执行命令或者sell脚本
package com.sinosun;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class JSchDemo {
private String charset =
"UTF-8";
private String user;
private String passwd;
private String host;
private JSch jsch;
private Session session;
/**
*
* @param user用户名
* @param passwd密码
* @param host主机IP
*/
public JSchDemo(String user, String passwd, String host) {
this.user = user;
this.passwd = passwd;
this.host = host;
}
/**
* 连接到指定的IP
*
* @throws JSchException
*/
public void connect()
throws JSchException {
jsch =
new JSch();
session = jsch.getSession(user, host,
22);
session.setPassword(passwd);
java.util.Properties config =
new java.util.Properties();
config.put(
"StrictHostKeyChecking",
"no");
session.setConfig(config);
session.connect();
}
/**
* 执行相关的命令
*/
public void execCmd() {
BufferedReader br =
new BufferedReader(
new InputStreamReader(System.in));
String command =
"ls -al";
BufferedReader reader =
null;
Channel channel =
null;
try {
channel = session.openChannel(
"exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(
null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
reader =
new BufferedReader(
new InputStreamReader(in, Charset.forName(charset)));
String buf =
null;
while ((buf = reader.readLine()) !=
null) {
System.out.println(buf);
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (JSchException e) {
e.printStackTrace();
}
finally {
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
channel.disconnect();
session.disconnect();
}
}
public static void main(String[] args)
throws Exception {
String user =
"root";
String passwd =
"123456";
String host =
"192.168.238.129";
JSchDemo demo =
new JSchDemo(user, passwd, host);
demo.connect();
demo.execCmd();
}
}
jsch的封装sshxcute
sshxcute是对jsch的封装,目前 JSch 正是这样一个满足上述基本需求的类库,JSch 是 SSH2 的一个纯 Java 实现。它可以连接到一个 sshd 服务器,使用端口转发,X11 转发,文件传输等等。但是这个类库毕竟偏向底层,上手与实际运行起来不太方便,sshxcute 框架正是基于 JSch 封装的,提供了更为便捷的 API 借口,更加灵活实用的功能,从而可以让开发与测试人员更加得心应手的使用。sshxcute 是一个框架,它允许工程师利用 Java 代码通过 SSH 连接远程执行 Linux/UNIX 系统上的命令或者脚本,这种方式不管是针对软件测试还是系统部署,都简化了自动化测试与系统环境部署的步骤。
面向 Java 开发与测试人员的远程执行 Linux/UNIX 系统上任务的框架 – sshxcute