Created
January 29, 2015 03:00
-
-
Save yangl/9c039a01eb6c76ac2f19 to your computer and use it in GitHub Desktop.
JSch简单封装,具体见http://www.jcraft.com/jsch/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.iuni.ops.common; | |
import com.google.common.collect.Lists; | |
import com.google.common.io.Files; | |
import com.jcraft.jsch.*; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.util.Assert; | |
import org.springframework.util.DigestUtils; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.List; | |
/** | |
* Jsch简单封装,多台主机用户及密码共用并配置至ops.properties | |
* | |
* @author YAGNLiiN | |
* @date 2015-01-16 14:27 | |
*/ | |
public abstract class JschClient { | |
private static final Logger log = LoggerFactory.getLogger(JschClient.class); | |
private static PropertiesLoader prop = new PropertiesLoader("/ops.properties"); | |
/** | |
* 执行Linux命令,注:执行文件上传请用copyTo方法 | |
* | |
* 如: | |
* | |
* String commond = | |
* "scp -t JschClient.java [email protected]:/usr/local/tomcat-iuni-order-api/webapps/ROOT/" | |
* | |
* List<String> rs = JschClient.sh("18.8.5.137", commond); | |
* | |
* | |
* @param host | |
* @param commond | |
* @return | |
*/ | |
public static final List<String> sh(String host, String commond) { | |
Assert.hasLength(host, "host不能为空"); | |
Assert.hasLength(commond, "commond不能为空"); | |
List<String> rs = Lists.newArrayList(); | |
ChannelExec channelExec = null; | |
BufferedReader reader = null; | |
Session session = null; | |
try { | |
String username = prop.getProperty("ssh.username"); | |
String password = prop.getProperty("ssh.password"); | |
JSch jsch = new JSch(); | |
session = jsch.getSession(username, host); | |
session.setPassword(password); | |
// 设置第一次登陆的时候提示,可选值:(ask | yes | no) | |
session.setConfig("StrictHostKeyChecking", "no"); | |
session.connect(30000); | |
channelExec = (ChannelExec) session.openChannel("exec"); | |
channelExec.setCommand(commond); | |
channelExec.setInputStream(null); | |
channelExec.setErrStream(System.err); | |
channelExec.connect(); | |
reader = new BufferedReader(new InputStreamReader(channelExec.getInputStream())); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
rs.add(line); | |
} | |
} catch (JSchException e) { | |
log.error("****Jsch sh 出错了****", e); | |
} catch (IOException e) { | |
// do nothing | |
} finally { | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (IOException e) { | |
} | |
} | |
if (channelExec != null && channelExec.isConnected()) { | |
channelExec.disconnect(); | |
} | |
if (session != null && session.isConnected()) { | |
session.disconnect(); | |
} | |
} | |
return rs; | |
} | |
/** | |
* 文件上传 | |
* | |
* @param host | |
* @param localFile | |
* @param remoteFile | |
*/ | |
public static final void copyTo(String host, String localFile, String remoteFile) { | |
Assert.hasLength(host, "host不能为空"); | |
Assert.hasLength(localFile, "localFile不能为空"); | |
Assert.hasLength(remoteFile, "remoteFile不能为空"); | |
Assert.isTrue(Files.isFile().apply(new File(localFile)), "localFile必须为文件"); | |
ChannelSftp sftp = null; | |
Session session = null; | |
try { | |
String username = prop.getProperty("ssh.username"); | |
String password = prop.getProperty("ssh.password"); | |
JSch jsch = new JSch(); | |
session = jsch.getSession(username, host); | |
session.setPassword(password); | |
// 设置第一次登陆的时候提示,可选值:(ask | yes | no) | |
session.setConfig("StrictHostKeyChecking", "no"); | |
session.connect(30000); | |
sftp = (ChannelSftp) session.openChannel("sftp"); | |
sftp.connect(); | |
sftp.put(localFile, remoteFile, ChannelSftp.OVERWRITE); | |
sftp.quit(); | |
} catch (JSchException e) { | |
log.error("****Jsch copyTo 出错了****", e); | |
} catch (SftpException e) { | |
log.error("****Jsch copyTo 出错了****", e); | |
} finally { | |
if (sftp != null && sftp.isConnected()) { | |
sftp.disconnect(); | |
} | |
if (session != null && session.isConnected()) { | |
session.disconnect(); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
String host = "18.8.5.137"; | |
// 文件上传至tomcat部署的机子 | |
// String local = "D:/workspace/ops_mgr_plat/ops_mgr/target/ops.war"; | |
// String remote = | |
// "/usr/local/tomcat-iuni-order-api/webapps/test/ops2.zip"; | |
// JschClient.copyTo(host, local, remote); | |
// 查看日志 | |
// String commond = | |
// "tail -n 200 /usr/local/tomcat-iuni-order-api/logs/catalina.out"; | |
// 压缩文件,备份 | |
// String commond = | |
// "tar zcvf /usr/local/tomcat-iuni-order-api/webapps/ROOT/20150116.tar.gz /usr/local/tomcat-iuni-order-api/webapps/ROOT/WEB-INF/classes"; | |
// 解压文件,-o 覆盖 | |
// String commond = | |
// "unzip -o -d /usr/local/tomcat-iuni-order-api/webapps/test/ /usr/local/tomcat-iuni-order-api/webapps/test/ops2.zip"; | |
// 远程文件md5计算,然后取其第一段即可,如:"4656844fba00e9cc3bd4c834598e2222 /usr/local/tomcat-iuni-order-api/webapps/test/ops2.zip" | |
String commond = "md5sum /usr/local/tomcat-iuni-order-api/webapps/test/ops2.zip"; | |
List<String> rs = JschClient.sh(host, commond); | |
for (String s : rs) { | |
System.out.println(s); | |
} | |
// 本地文件md5计算 | |
try { | |
File f = new File("D:/workspace/ops_mgr_plat/ops_mgr/target/ops.war"); | |
String md5 = DigestUtils.md5DigestAsHex(Files.toByteArray(f)); | |
} catch (IOException e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment