实现文件上传下载
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 {
//Test1.sshSftp("192.168.238.129", "root", "123456", 22);
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);//设置密码
//设置第一次登陆的时候提示,可选值:(ask | yes | no)
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 {
//创建sftp通信通道
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();
}
}
/*
* 从SFTP服务器下载文件
*
* @param ftpHost SFTP IP地址
*
* @param ftpUserName SFTP 用户名
*
* @param ftpPassword SFTP用户名密码
*
* @param ftpPort SFTP端口
*
* @param ftpPath SFTP服务器中文件所在路径 格式: ftptest/aa
*
* @param localPath 下载到本地的位置 格式:H:/download
*
* @param fileName 文件名称
*/
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; // 主机IP
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 ;rm -f aa.log; ls ";
//String command = "tar -czf aa.tar.gz aa.log";
//String command = "tar -zxvf aa.tar.gz";
//String command = "tar -zxvf aa.tar.gz";
//String command = "/root/test.sh";
//String command = "reboot";
//String command = "echo abc >> xx.txt";
String command = "ls -al";
//String command = "echo \"always\" >/sys/kernel/mm/transparent_hugepage/enabled ";
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 系统上的命令或者脚本,这种方式不管是针对软件测试还是系统部署,都简化了自动化测试与系统环境部署的步骤。