执行shell命令、下载文件...

package com.sunsheen.blockchain.admin.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Properties; import org.apache.http.util.Asserts; import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.StreamGobbler; import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.sunsheen.blockchain.admin.common.ServersConstant; public class SSHUtil { /**
* 连接服务器
* @param host
* @param user
* @param pwd
* @return
* @throws JSchException
*/
public static synchronized Session connect(String host,String user,String pwd) throws JSchException {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
// java.util.Properties config = new java.util.Properties();
// config.put("StrictHostKeyChecking", "no");
session.setPassword(pwd);
session.connect();
return session;
} /**
* 执行命令集
* @param session
* @param cmds
* @return
* @throws IOException
* @throws JSchException
*/
public static String execCommandByShell(Session session,String[] cmds)
throws IOException, JSchException {
String result = "";
// 2.尝试解决 远程ssh只能执行一句命令的情况
ChannelShell channelShell = (ChannelShell) session.openChannel("shell");
InputStream inputStream = channelShell.getInputStream();// 从远端到达的数据都能从这个流读取到
channelShell.setPty(true);
channelShell.connect(); OutputStream outputStream = channelShell.getOutputStream();// 写入该流的数据
// 都将发送到远程端
// 使用PrintWriter 就是为了使用println 这个方法
// 好处就是不需要每次手动给字符加\n
PrintWriter printWriter = new PrintWriter(outputStream);
for (String cmd:cmds) {
printWriter.println(cmd);
}
printWriter.println("exit");// 为了结束本次交互
printWriter.flush();// 把缓冲区的数据强行输出
return result;
} /**
* 单个文件上传
* @param file 上传的文件
* @param remoteFolder 服务器上存放当前文件的文件夹
* @param uploadFileName 上传文件的名字
*/
public static void postFile(InputStream fileStream,String remoteFolder,String uploadFileName) throws Exception{
//上传文件的个数应该跟对应文件夹相同
if((null==fileStream || null==remoteFolder))
return; String username = ServersConstant.USERNAME;
String password = ServersConstant.PASSWORD;
String address = ServersConstant.ADDRES;
int port = ServersConstant.PORT; ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
//创建连接
JSch jsch = new JSch();
sshSession = jsch.getSession(username, address, port);
sshSession.setPassword(password);
//获取session
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
//得到sftp
channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel; sftp.cd(remoteFolder);//进入对应存放日志文件的目录
sftp.put(fileStream, uploadFileName);//写入文件 } finally {
//关闭sftp信道
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
//关闭channel管道
if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
}
}
//关闭session
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
}
}
} /**
* 转换指令到服务器执行
* @param command 要执行的指令
*/
public static void transferCommand(String... commands){
String romoteAddr = ServersConstant.ADDRES;
String username = ServersConstant.USERNAME;
String password = ServersConstant.PASSWORD;
try {
Connection connection = new Connection(romoteAddr);// 创建一个连接实例
connection.connect();// Now connect
boolean isAuthenticated = connection.authenticateWithPassword(username, password);//認證
Asserts.check(isAuthenticated, "用戶名或密碼錯誤!");
ch.ethz.ssh2.Session sess = connection.openSession();// 創建一個會話
sess.requestPTY("bash");
sess.startShell();
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
//向服务器上输入命令
PrintWriter out = new PrintWriter(sess.getStdin());
for(String command : commands){
out.println(command);
}
out.close();
sess.waitForCondition(ChannelCondition.CLOSED|ChannelCondition.EOF | ChannelCondition.EXIT_STATUS,100);
//关闭连接
sess.close();
connection.close();
stderrReader.close();
stdoutReader.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 下载指定文件到本地指定文件夹oo
* @param serversFolder 服务器文件所在目录 /opt/log
* @param fileName 需要下载的文件名 monitor.log
* @param localFolder 本地存放文件的文件夹 d:\\logs
*/
public static void download(String serversFolder,String fileName,String localFolder) {
String username = ServersConstant.USERNAME;
String password = ServersConstant.PASSWORD;
String address = ServersConstant.ADDRES;
int port = ServersConstant.PORT; ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
// 创建连接
JSch jsch = new JSch();
sshSession = jsch.getSession(username, address, port);
sshSession.setPassword(password);
// 获取session
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
// 得到sftp
channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
// 进入服务器文件夹
sftp.cd(serversFolder);
//创建本地文件夹
File downloadFile = new File(localFolder);
if(!downloadFile.exists())
downloadFile.mkdirs();
// 下载
String serversFile = serversFolder +"/"+ fileName;
sftp.get(serversFile,localFolder);
System.out.println(fileName+"已下载到:"+localFolder);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭sftp信道
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
// 关闭channel管道
if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
}
}
// 关闭session
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
}
}
} }

最新文章

  1. [麦先生]TP3.2之微信开发那点事[基础篇](微信支付签名算法)
  2. c常用字符串函数
  3. CBV进阶(一)
  4. Mie散射 文献图片
  5. 【HDOJ 1286】找新朋友
  6. JDK8 HashMap--treeify()树形化方法
  7. 【linux】工作时使用的命令
  8. 初识Dubbo+Zookeeprt搭建SOA项目
  9. Spring基于注解和XML混合方式的使用
  10. IO流总结笔记三
  11. day7:set和深浅copy
  12. java代码求阶乘n!
  13. HTML_body标签
  14. Python入门之面向对象编程(一)面向对象概念及优点
  15. python selenium-7自动发送邮件
  16. Internet History, Technology and Security (Week 4)
  17. 学习笔记之PHP
  18. Unix系统编程()发送信号的其他方式:raise和killpg
  19. Python3.x:logging模块对运行过程记录
  20. Tomcat 映射虚拟目录和程序热部署

热门文章

  1. 2020-04-07:假如你们系统接收十几种报文,用什么方式对应的各自的service,总不能都用if-else判断吧
  2. 03 Arduino-模拟输出与PWM的操作方法
  3. 聊聊MySQL主从复制的几种复制方式
  4. 前端面试?这份手撸Promise请你收下
  5. asp.net core mvc和angular项目的一些问题
  6. put数据到topic
  7. PythonCrashCourse 第四章习题
  8. WordCloud教程(上)
  9. 进阶6:连接查询 二、sql99语法
  10. LeetCode 92 | 大公司常考的面试题,翻转链表当中指定部分