使用java执行linux的sheel命令
来源:互联网 发布:数据为王颠覆营销 编辑:程序博客网 时间:2024/05/16 01:47
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.swing.JOptionPane;
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 ExecSheelTool {
public static final int SSH2PORT = 22;
public static final int PASSWORD = 0;
public static final int PASSWORDVALIDTIMES = 0;
public static final int FILE = 1;
private static Session session;
private static Channel channel;
/**
*
* @param host 连接主机名
* @param port 连接端口
* @param username 连接用户名
* @param auth 连接认证信息,当method为0时,auth为用户密码;但method为1时,auth为私钥文件
* @param method 连接认证方式,0为密码认证,1为私钥文件认证
* @throws Exception 异常
*/
private static void getSession(String host, Integer port, String username, String auth, int method) throws Exception{
if(port == null){
port = SSH2PORT;
}
JSch jsch = new JSch();
if(method == FILE){
jsch.addIdentity(auth);
}
session = jsch.getSession(username, host, port);
if(method == PASSWORD){
int times = 0;
while(auth == null || "".equals(auth)){
auth = JOptionPane.showInputDialog("Please enter the password!");
times++;
if(times == PASSWORDVALIDTIMES){
throw new Exception();
}
}
session.setPassword(auth);
}
session.setTimeout(2000);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
}
private static void getChannel(String command) throws JSchException, IOException{
channel = session.openChannel("exec");
ChannelExec execChannel = (ChannelExec)channel;
execChannel.setCommand(command);
}
public static InputStream execute(String host, int port, String username, String password, String command, int method) throws Exception {
if(session == null){
getSession(host, port, username, password, method);
}
getChannel(command);
InputStream in = channel.getInputStream();
channel.connect();
return in;
}
/**
* 将输入流转化为字符串
* @param is 输入流
* @return 字符串
* @throws Exception 异常
*/
public static String toString(InputStream is) throws Exception {
StringBuffer sb = new StringBuffer();
int c = -1;
while((c = is.read()) != -1){
sb.append((char)c);
}
return sb.toString();
}
public static void release(){
if(session != null){
if(session.isConnected()){
session.disconnect();
}
}
if(channel != null){
if(channel.isConnected()){
channel.disconnect();
}
}
}
}
- 使用java执行linux的sheel命令
- linux 使用 java 程序执行 shell命令
- 使用java执行linux系统命令
- java使用Runtime.exec执行linux命令
- java使用Runtime.exec执行linux命令
- java使用Runtime.exec执行linux命令
- java使用ganymed-ssh2执行linux命令
- linux sheel command
- java执行linux命令
- java执行Linux命令
- java执行linux命令
- java执行Linux命令 .
- java执行linux命令
- java执行linux命令
- Java执行Linux命令
- java执行linux命令
- java执行Linux命令
- java执行linux命令
- HTTP中的Get和Post分析
- ATL_NO_VTABLE理解-转载
- (疑问已经解决了)求解答疑问,关于多线程的(没有人回答,我自己解决问题吧,二和三疑问已解决,第一个疑问扔没有解决)
- cocos2d-x:win8 + vs2012 + cocos2d-x-3.0alpha0 + Python2.7.5 创建多平台项目
- 【Android】菜单功能的实现:上下文菜单
- 使用java执行linux的sheel命令
- Qt中文乱码
- TX Text Control的样式表使用技巧
- 数据库设计 三个范式
- Mysql大小写敏感的问题
- 调用函数求最大公约数
- centos6.4 python 升级到2.7之后一些问题解决
- google gflag简单的使用方法
- mapreduce实现多文件自定义输出