1.调用代码
import ch.ethz.ssh2.*; import java.io.*; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.Date; public class RemotShellTool { public static void main(String[] args) throws Exception { // 上传文件 RemotShellTool.updateFile(); // 修改文件权限 RemotShellTool.chmodFile(); // 打包 RemotShellTool.pack("D:\\dev\\workspace\\taiping-dianshang-core-release2"); // 运行项目 RemotShellTool.execCommond(); } // 打包 public static void pack(String path) { try { Process process = Runtime.getRuntime().exec("cmd /c mvn install -Dmaven.test.skip=true "); new Thread(new ThreadStream(process.getErrorStream())).start(); new Thread(new ThreadStream(process.getInputStream())).start(); process.waitFor(); process.destroy(); process.getOutputStream().close(); process.getErrorStream().close(); process.getInputStream().close(); }catch (Exception e) { e.printStackTrace(); } } // 执行命令 public static void execCommond() throws Exception { String fileName = "taiping-jinfu-task-20191009102720.jar"; String port = "8888"; Connection connection = new Connection("ip"); connection.connect();//连接 connection.authenticateWithPassword("username","password");//认证 Session session=connection.openSession(); session.execCommand("cd /home/loginuser && sh test.sh "+fileName+" "+port); InputStream is = new StreamGobbler(session.getStdout());//获得标准输出流 BufferedReader brs = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String temp = ""; while ((temp=brs.readLine()) != null) { System.out.println(temp); } InputStream errStream = new StreamGobbler(session.getStderr()); BufferedReader errReader = new BufferedReader(new InputStreamReader(errStream,Charset.forName("UTF-8"))); String errTemp = ""; while((errTemp=errReader.readLine()) !=null) { int i=1; System.out.println(errTemp); } // new Thread(new ThreadStream(session.getStdout())).start(); if (session != null) { session.close(); } session.close(); brs.close(); // errReader.close(); connection.close(); } // 修改文件权限 public static void chmodFile() throws Exception { String fileName = "taiping-jinfu-task-20191009102720.jar"; Connection connection = new Connection("ip"); connection.connect();//连接 connection.authenticateWithPassword("username","password");//认证 Session session=connection.openSession(); session.execCommand("chmod 664 /home/loginuser/"+fileName); if (session != null) { session.close(); } session.close(); } public static void updateFile() throws Exception{ Connection connection = new Connection("ip"); connection.connect();//连接 connection.authenticateWithPassword("username","password");//认证 SCPClient scpClient = connection.createSCPClient(); String localFilePath = "D:\\tpdata\\upload\\taiping-jinfu-task-0.0.1-SNAPSHOT.jar"; File localFile = new File(localFilePath); String remoteFilePath = "/home/loginuser/"; String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); String remoteFileName = "taiping-jinfu-task-"+timeStamp+".jar"; SCPOutputStream os= scpClient.put(remoteFileName,localFile.length(),remoteFilePath,"0222"); byte[] b = new byte[4096]; FileInputStream fis = new FileInputStream(localFile); int i; while ((i = fis.read(b)) != -1) { os.write(b, 0, i); } os.flush(); fis.close(); os.close(); } }
2.引用maven,版本使用262的,使用较低的版本,api会不一样
<dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>262</version> </dependency>
3.test.sh脚本
#!/bin/bash export JAVA_HOME=/home/loginuser/jdk export JRE_HOME=/home/loginuser/jdk/jre export PATH=/usr/local/bin:/usr/local/lib:$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib PID=$(netstat -anp|grep $2|awk '{printf $7;exit;}'|cut -d/ -f1) echo 'PID:'$PID if [$PID = ""]; then echo '没有端口占用' else echo 'kill 占用端口' kill -9 $PID fi nohup java -jar $1 -Dserver.port=$2 > nohup.out tail -200f nohup.out
4.操作日志线程类
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class ThreadStream implements Runnable { private InputStream inputStream; public ThreadStream(InputStream inputStream) { this.inputStream=inputStream; } @Override public void run() { try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"GBK")); String s = ""; while((s = bufferedReader.readLine())!=null) { System.out.println("输出结果:"+s); } bufferedReader.close(); System.out.println("输出结果:"+s); }catch (Exception e) { e.printStackTrace(); } } }
相关推荐
利用ch.ethz.ssh2操作进行SSH进行操作下载文件代码,主要利用Linux命令进行操作,方便简单
ganymed-ssh2-build ssh2 ch.ethz.ssh2.Connection ch.ethz.ssh2.SCPClient
详细全面的jar包
费劲整理的SSH2 jar包,拷贝到项目的lib下就可以使用
留作纪念。小伙伴们也可参考一下。感觉我这jar包垒的还算是比较用心的
ssh2 一些错误问题的记录
<groupId>ch.ethz.ssh2 <artifactId>ssh2 <version>1.0 </dependency>
开发web项目完整的SSH2最新版jar包合集,本人亲测有效,可以节省您大量的容错时间,因为其版本更新快,所以配置会耗费您大量的宝贵的开发时间,故万望采纳,你值得拥有哦!
import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.SCPClient; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; import common.Logger; import org.apache.commons.lang.StringUtils; import...
Struts2+hibernate+spring实现多文件的上传与下载功能,主要包括:上传、下载、删除 1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.Struts2进行下载处理,能对上传的所有文件进行...
一个很简单的登录功能,但是诠释了strut2、spring、hibernate项目的架构,使用maven管理jar包
基于maven的ssh2框架项目
SSH2框架,当今最为流行的项目开发框架组合,我们在搭建框架之前,首先一定要注意各个框架的版本,不同的版本集成方式和所需的jar包是有区别的。 SSH2框架的版本为:struts2 + spring2 + hibernate3
在网上看了很多关于SSH2依赖包的文章,按照上面的提示试了很多次,终于成功部署了,如果能够一步到位,就省了很多事,特制作本lib供需要的同学下载。在tomcat6.0.35上测试通过。依赖包版本: Spring 3.0.4 Struts ...
本压缩文件中有SSH2(struts2,spring,hibernate)框架开发所需的所有jar包
最新版本的struts2 hibernate spring框架jar包
NULL 博文链接:https://killeraction.iteye.com/blog/1022141
解决openssh升级到8.2以后无法登录的问题,支持jdk1.5及以上版本
首先在程序里import ch.ethz.ssh2.SCPClient; 然后通过下面的方法来实现: SCPClient scpClient = conn.createSCPClient(); scpClient.put("localFiles", "remoteDirectory"); //从本地复制文件到远程目录 ...
极深切割(DEXTR)请访问我们的项目页面以获取论文和预先计算的结果。 这是我们的工作深度极限切割(DEXTR)的实现,用于从极限点进行对象分割。 深度极限切割(DEXTR)请访问我们的项目页面以访问纸张和预先计算的...