public class LinuxUtils {
private Connection con;
private String userName; //用户名
private String password; //密码
private String ip; //ip
private int port; //端口
boolean isAuthed = false;
public LinuxUtils(){}
public LinuxUtils(String ip, int port ,String userName, String password){
this.ip = ip;
this.port = port;
this.userName = userName;
// this.userName = "root";
this.password = password;
// this.password = "123456";
isAuthed = connection();//链接并登陆
}
public static void main(String[] args) {
//只需要传入想要操作的ip,端口,用户名,密码,即可远程调用相关,该过程执行完后,记得关闭connection
LinuxUtils lu1 = new LinuxUtils("192.168.237.101",22,"root","111111");
// FileUploadUtils fileUploadUtils=new FileUploadUtils();
// boolean tag =fileUploadUtils.fileUpload(myFile, myFile.getContentType(), myFile.getOriginalFilename(), "/home/cdpi");
// lu1.execCommandBySession("cd /home/cdpi/bin/test86;tar -zxvf storm10.9.tar.gz");
lu1.execCommandBySession("cd /root/bin/web_test;touch 124");
lu1.closeConnection();
//*********************************************************
//不传入参数,是在本地执行命令
// LinuxUtils lu2 = new LinuxUtils();
// lu2.execCommandAtLocal("");
}
//发送本地文件到远端
public void add2DestTable(String rootPath, String destPath){
//远程服务器的用户名密码
try{
SCPClient scpClient = con.createSCPClient();
//将本地文件上传到服务器端的目录下
scpClient.put(rootPath, destPath);
}catch(Exception e){
e.printStackTrace();
}
}
//远程执行命令
public void execCommandBySession(String cmd){
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
Session session = null;
try {
session = this.con.openSession();
session.execCommand(cmd);
BufferedReader brs = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
//这里是打印输出信息
while(true){
String line = brs.readLine();
if(line==null){
break;
}
System.out.println("反馈信息: "+line);
}
// System.out.println("执行了cmd: " + cmd);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
}
/**
* 判断kafka时候启动成功
* */
public String startKafkaJps(String comd){
String startKafka = "";
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
Session session = null;
try {
session = this.con.openSession();
session.execCommand(comd);
BufferedReader brs = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
while(true){
String line = brs.readLine();
if(line==null){
break;
}else if(line.indexOf("Kafka") > -1){
startKafka = "Start-up";
}
}
new StreamGobbler(session.getStderr());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
// System.out.println("反馈信息: " + killKafka);
return startKafka;
}
//远程执行命令
public void execSudoMkdir(String path, String group, String pwd){
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
Session session = null;
try {
session = this.con.openSession();
session.execCommand("cd /home/storm;./storm_sudo_mkdir.sh " + path + " " + group + " '" + pwd + "'");
new StreamGobbler(session.getStdout());
new StreamGobbler(session.getStderr());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
}
//远程执行命令
public void commandBySessionExec(String cmd){
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
final List<Session> session = new ArrayList<Session>();
try {
session.add(this.con.openSession());
session.get(0).execCommand(cmd);
new Thread(new Runnable() {
@Override
public void run() {
try{
StreamGobbler sg = new StreamGobbler(session.get(0).getStdout());
InputStreamReader isr = new InputStreamReader(sg);
BufferedReader brs = new BufferedReader(isr,9000000);
brs.close();
isr.close();
sg.close();
}catch (Exception e){
e.printStackTrace();
}finally{
session.add(null);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try{
StreamGobbler sg = new StreamGobbler(session.get(0).getStderr());
InputStreamReader isr = new InputStreamReader(sg);
BufferedReader brs = new BufferedReader(isr,9000000);
brs.close();
isr.close();
sg.close();
}catch (Exception e){
e.printStackTrace();
}finally{
session.add(null);
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}finally{
while(true){
if(session != null){
if(session.size() > 1 && session.get(0) != null){
session.get(0).close();
break;
}else{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//远程执行命令
public void commandBySessionExecsss(String cmd){
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
Session session = null;
try {
session = this.con.openSession();
session.execCommand(cmd);
new StreamGobbler(session.getStdout());
new StreamGobbler(session.getStderr());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
}
//远程执行命令
public boolean commandBySessionExecStorm(String[] cmds){
boolean flag=false;
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
Session session = null;
try {
session = this.con.openSession();
String runCMD = "";
for(String cmd: cmds){
runCMD = runCMD + cmd + " ";
}
session.execCommand(runCMD);
System.out.println("run :" + runCMD);
InputStream is = new StreamGobbler(session.getStdout());
BufferedReader brs = new BufferedReader(new InputStreamReader(is));
while(true){
String line = brs.readLine();
if(line==null){
break;
}
System.out.println(line);
}
/*
if(session == null) System.out.println("NULLNULLNULLNULLNULLNULLNULLNULL");
session.execCommand(cmd);
System.out.println("--------------------12311111111111111111111111111111111111111111111111111");
BufferedReader brs = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
System.out.println("12311111111111111111111111111111111111111111111111111");
while(true){
String line = brs.readLine();
if(line==null){
break;
}
System.out.println(line);
}*/
} catch (IOException e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
if(this.con != null){
this.con.close();
}
}
}
return flag;
}
/**
* 暂停kafka进程
* */
public String getKafkaPid(String comd){
String killKafka = "";
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
Session session = null;
try {
session = this.con.openSession();
session.execCommand(comd);
BufferedReader brs = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
while(true){
String line = brs.readLine();
if(line==null){
break;
}else if(line.indexOf("Kafka") > -1){
killKafka = "kill -9 " + line.substring(0,line.indexOf(" "));
}
}
new StreamGobbler(session.getStderr());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
// System.out.println("反馈信息: " + killKafka);
return killKafka;
}
/**
* 暂停storm进程
* */
public String killStorm(String comd){
String killStorm = "";
String stormId="";
if(this.con == null){
throw new RuntimeException("还未远程链接,请确定是否执行远程调用!****");
}else{
Session session = null;
try {
session = this.con.openSession();
session.execCommand(comd);
// new StreamGobbler(session.getStdout());
BufferedReader brs = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout())));
while(true){
String line = brs.readLine();
if(line==null){
break;
}else if(line.indexOf("nimbus") > -1){
stormId+=" " + line.substring(0,line.indexOf(" "));
}else if(line.indexOf("core")>-1){
stormId+=" " + line.substring(0,line.indexOf(" "));
}else if(line.indexOf("supervisor")>-1){
stormId+=" " + line.substring(0,line.indexOf(" "));
}else if(line.indexOf("logviewer")>-1){
stormId+=" " + line.substring(0,line.indexOf(" "));
}
}
new StreamGobbler(session.getStderr());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
killStorm = "kill -9 "+stormId;
// System.out.println("反馈信息: " + killStorm);
return killStorm;
}
//执行本地命令
public boolean execCommandAtLocal(String cmd){
Process exec = null;
boolean isSuccess = false;
try {
exec = Runtime.getRuntime().exec(cmd);
isSuccess = exec.waitFor() == 0?true:false;
exec.destroy();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return isSuccess;
}
//获取链接并注册
private boolean connection(){
con = new Connection(ip, port);
boolean isAuthed = false;
try {
con.connect();
isAuthed = con.authenticateWithPassword(userName, password);
} catch (IOException e) {
e.printStackTrace();
}
return isAuthed;
}
public void closeConnection(){
if(this.con != null )
this.con.close();
}
public Connection getCon() {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/***********压缩**************/
public static boolean executeCmds(String[] cmds){
boolean flag=false;
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
} catch (IOException e) {
e.printStackTrace();
} if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
for(String cmd: cmds){
out2.println(cmd);
}
// out2.println("cd /home/jstorm/bin/apache-tomcat-7.0.55/myfiles"); //执行该语句后返回上一级目录
// //out2.println("pwd"); //打印当前目录
// out2.println("tar -zcvf storm" + stormBaseConfInfo.getVersion() + ".tar.gz");//执行该目录下的jar文件,Linux下执行jar文件必须进入其所在的文件夹
out2.println("exit");
try {
flag = proc.waitFor() == 0?true:false;
in.close();
out2.close();
proc.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
return flag;
}
}