针对linux服务器,用平台一键同步时间、查看内存占用、磁盘占用,由于采用用户名,密码登录SSH远程登录拿取数据,所以不需要安装客户端
时间同步界面
平台根据远程服务器的登录名,密码,ssh登录远程服务器,执行ntpdate 172.16.3.3(时间服务器ip)来同步时间
public String syncDate(String ip) throws IOException, InterruptedException { RemoteShellTool tool = new RemoteShellTool(ip,"utf-8"); tool.loginCentos(); String returnString = tool.exec("ntpdate 172.16.3.2"); if(returnString.indexOf("adjust")<0) { logger.info("syncDate:"+ip+" return "+returnString); return "fail"; } Thread.sleep(1000); return viewDate(ip); }
附其他代码:
html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="/css/reset.css" rel="stylesheet"> <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <link href="/css/layout.css" rel="stylesheet"> <link href="/css/index/applications.css" rel="stylesheet"> <script type="text/javascript" src="/lib/jquery.js"></script> <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="/bootstrap/js/jquery.bootpag.min.js"></script> <script type="text/javascript" src="/lib/avalon.js"></script> <script type="text/javascript" src="/js/common/util.js"></script> <script type="text/javascript" src="/js/common/common.js"></script> <script type="text/javascript" src="/js/index/vmtime.js"></script> <title>虚拟机时间管理</title> </head> <body ms-controller="vm"> <!-- HEAD --> <!--HEAD --> <div ms-include-src="'/home/header.html'"></div> <!-- Content --> <div class="container"> <div ms-controller="vmtime"> <div class="tabbable"> <div class="tab-content"> <div class="tab-pane active" id="vms"> <div id="vmsTab-pane"> <br /> <div class="row" id="search1Div"> <div class="col-md-2"> <div class="input-group"> <span class="input-group-addon">环境:</span> <select class="form-control" ms-duplex="envType"> <option value="STRESS" selected>STRESS</option> <option value="ALL">全部</option> <option value="SIT1">SIT1</option> <option value="SIT2">SIT2</option> <option value="UAT">UAT</option> <option value="other">其他</option> </select> </div> </div> <div class="col-md-2" id="search2Div"> <div class="input-group"> <span class="input-group-addon">类型:</span> <select class="form-control" ms-duplex="conType"> <option value="" selected>请选择</option> <option value=".Net Web">.Net Web</option> <option value="Windows Service">Windows Service</option> <option value="Node">Node</option> <option value="Java App">Java App</option> <option value="Java Web">Java Web</option> <option value="其他">其他</option> </select> </div> </div> <div class="col-md-1"> <button type="button" id="searchBtn" class="btn btn-primary" ms-click="listVmInfosByPage('init')" style="margin: auto;"> 搜 索</button> </div> <div class="col-md-1"> <button type="button" id="allCheckBtn" class="btn btn-info" ms-click="checkAll()" style="margin: auto;">全选</button> </div> <div class="col-md-1"> <button type="button" id="allUncheckBtn" class="btn btn-info" ms-click="uncheckAll()" style="margin: auto;">全不选</button> </div> <div class="col-md-2"> <button type="button" id="allStatusBtn" class="btn btn-success" ms-click="viewAllStatus()" style="margin: auto;">查看当前时间</button> </div> <div class="col-md-2"> <button type="button" id="allDeployBtn" class="btn btn-warning" ms-click="deployAll()" style="margin: auto;">同步时间服务器</button> </div> </div> <div> <div id="pageSizeSelect"> <a><span ms-class="{{pagesize1Cls}}" ms-click="changePageSize(pagesize1)">{{pagesize1}}</span></a> | <a><span ms-class="{{pagesize2Cls}}" ms-click="changePageSize(pagesize2)">{{pagesize2}}</span></a> | <a> <spam ms-class="{{pagesize3Cls}}" ms-click="changePageSize(pagesize3)">{{pagesize3}}</spam> </a> </div> </div> <table class="table table-condensed table-hover"> <thead> <tr> <td class="width-50"></td> <td class="width-50">ID</td> <td>名称</td> <td class="width-125">IP</td> <td class="width-100">操作系统</td> <td class="width-300">当前时间</td> <td class="width-50">查看</td> <td class="width-50">同步</td> </tr> </thead> <tbody> <tr ms-repeat="vmsList"> <td><label><input type="checkbox" ms-class="check_{{el.name}}"></label></td> <td>{{$index+jpageSize*(jpageIndex-1)+1}}</td> <td>{{el.name}}</td> <td><a ms-href="'/admin/vmdetails.html?vmid='+el.vm.id" target="_blank">{{el.ip}}</a></td> <td>{{el.os}}</td> <td><label ms-class="time_{{el.name}}" /></td> <td> <div ms-class="buttonDiv_view_{{el.name}}"> <i style="color: #009100;" ms-class="glyphicon glyphicon-eye-open icon-white i_view_{{el.name}}" ms-click="postViewStatus(el.name,el.ip,el.os)"></i> </div> <div ms-class="loadDiv_view_{{el.name}} loadDiv" style="display: none"> <img src="/img/load2.jpg" style=" 16px; height: 16px;" /> </div> </td> <td> <div ms-class="buttonDiv_{{el.name}}"> <i style="color: #000066;" ms-class="glyphicon glyphicon-play icon-white i_{{el.name}}" ms-click="postDeploy(el.name,el.ip,el.os)"> </i> </div> <div ms-class="loadDiv_{{el.name}} loadDiv" style="display: none"> <img src="/img/load2.jpg" style=" 16px; height: 16px;" /> </div> </td> </tr> </tbody> </table> <div class="text-center"> <p id="pagination"></p> </div> </div> </div> </div> </div> </div> </div> <!--/.container--> <!--FOOTER --> <div ms-include-src="'/home/footer.html'"></div> </body> </html>
js
/** * Created by chenjiazhu on 2017/5/10. */ var vmtime = avalon.define({ $id : 'vmtime', // VM Start pagesize1 : "20", pagesize1Cls : "pageSizeSelected", pagesize2 : "50", pagesize2Cls : "", pagesize3 : "100", pagesize3Cls : "", changePageSize : function(pgsize) { vmtime.jpageSize = pgsize; vmtime.listVmInfosByPage("init"); }, clearsearch : function() { vmtime.conType = ""; vmtime.listVmInfosByPage("init"); }, jpageIndex : 1, jpageSize : 20, envType : "STRESS", conType : "", vmsList : [], listVmInfosByPage : function(tag) { if (tag) { vmtime.jpageIndex = 1; } $.ajax({ type : "post", url : 'listVmInfosByPageByEnvType.action', data : { "pageindex" : vmtime.jpageIndex, "pagesize" : vmtime.jpageSize, "type" : vmtime.conType, "envType" : vmtime.envType }, dataType : "json", success : function(data) { if (tag) { $('#pagination').bootpag({ total : data.pagenum, page : vmtime.jpageIndex }); } if (data.retCode == "1000") { vmtime.vmsList = data.vms; } else { alert(data.retMSG); } $("[class^=time]").html(""); }, error : function(XMLHttpRequest, textStatus, errorThrown) { alert("请求数据异常,状态码:" + XMLHttpRequest.status + ",Error:" + errorThrown + ",textStatus:" + textStatus); } }); }, postViewStatus : function(name, ip,os) { $(".loadDiv_view_" + name).show(); $(".buttonDiv_view_" + name).hide(); $.ajax({ type : "post", url : 'viewDate.action', data : { "ip" : ip, "os":os }, dataType : "json", success : function(data) { $(".time_" + name).html(data.retCode); $(".loadDiv_view_" + name).hide(); $(".buttonDiv_view_" + name).show(); }, error : function(XMLHttpRequest, textStatus, errorThrown) { alert("请求数据异常,状态码:" + XMLHttpRequest.status + ",Error:" + errorThrown + ",textStatus:" + textStatus); $(".loadDiv_view_" + name).hide(); $(".buttonDiv_view_" + name).show(); } }); }, postDeploy : function(name, ip, os) { $(".loadDiv_" + name).show(); $(".buttonDiv_" + name).hide(); $.ajax({ type : "post", url : 'syncDate.action', data : { "ip" : ip, "os":os }, dataType : "json", success : function(data) { $(".loadDiv_" + name).hide(); $(".buttonDiv_" + name).show(); $(".time_" + name).html(data.retCode); }, error : function(XMLHttpRequest, textStatus, errorThrown) { $(".loadDiv_" + name).hide(); $(".buttonDiv_" + name).show(); alert("请求数据异常,状态码:" + XMLHttpRequest.status + ",<br/>" + XMLHttpRequest.readyState + ",<br/>" + XMLHttpRequest.responseText + ",<br/>Error:" + errorThrown + ",<br/>textStatus:" + textStatus); } }); }, viewAllStatus : function() { $("input[type='checkbox']").each(function() { if ($(this).get(0).checked) { var name = $(this).attr("class").substr(6); // alert(name); $(".i_view_" + name).click(); } }); }, deployAll : function() { $("input[type='checkbox']").each(function() { if ($(this).get(0).checked) { var name = $(this).attr("class").substr(6); // alert(name); $(".i_" + name).click(); } }); }, checkAll : function() { $("input[type='checkbox']").each(function() { $(this).attr("checked", "true"); }); }, uncheckAll : function() { $("input[type='checkbox']").each(function() { $(this).removeAttr("checked"); }); }, loadVmTAB : function() { vmtime.listVmInfosByPage("init"); $('#vms').tab('show'); }, // VM END userOps : ops(4), bootpagFuc : function() { $('#pagination').bootpag({ total : 1, maxVisible : 10 }).on('page', function(event, num) { vmtime.jpageIndex = num; vmtime.listVmInfosByPage(); }); } }); /* avalon.ready(function() { appsvm.bootpagFuc(); appsvm.listApp("init"); appsvm.depList = getAllDepartments(); appsvm.envsList = getAllEnvs(); appsvm.applicationsTypeList = getAllAppType(); });*/ avalon.ready(function() { /* if (vmtime.userOps) { vmtime.loadVmTAB(); } else { redirectAdminIndexPage(); }*/ vmtime.bootpagFuc(); vmtime.listVmInfosByPage("init"); // $(".loadDiv").hide(); }); vmtime.$watch("jpageSize", function(newValue) { vmtime.pagesize1Cls = ""; vmtime.pagesize2Cls = ""; vmtime.pagesize3Cls = ""; if (newValue == vmtime.pagesize1) { vmtime.pagesize1Cls = "pageSizeSelected"; } else if (newValue == vmtime.pagesize2) { vmtime.pagesize2Cls = "pageSizeSelected"; } else if (newValue == vmtime.pagesize3) { vmtime.pagesize3Cls = "pageSizeSelected"; } })
工具类
package com.ymt.testplatform.util; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; public class RemoteShellTool { private Connection conn; private String ipAddr; private String charset = Charset.defaultCharset().toString(); private String userName; private String password; public RemoteShellTool(String ipAddr, String charset) { this.ipAddr = ipAddr; if (charset != null) { this.charset = charset; } } public RemoteShellTool(String ipAddr, String userName, String password, String charset) { this.ipAddr = ipAddr; this.userName = userName; this.password = password; if (charset != null) { this.charset = charset; } } public boolean login() throws IOException { conn = new Connection(ipAddr); conn.connect(); // 连接 return conn.authenticateWithPassword(userName, password); // 认证 } public boolean loginCentos() throws IOException { conn = new Connection(ipAddr); conn.connect(); // 连接 String [] pass = {"ymt@123","abcd@1234","root@1234","1qaz@WSX","1234qwer"}; for (String pa : pass) { if(conn.authenticateWithPassword("root", pa)) { this.userName = "root"; this.password = pa; return true; } } return false; } public boolean login(String userName,String password) throws IOException { conn = new Connection(ipAddr); conn.connect(); // 连接 this.userName = userName; this.password = password; return conn.authenticateWithPassword(userName, password); // 认证 } public String exec(String cmds) { InputStream in = null; String result = ""; try { if (this.login()) { Session session = conn.openSession(); // 打开一个会话 session.execCommand(cmds); in = session.getStdout(); result = this.processStdout(in, this.charset); session.close(); conn.close(); } } catch (IOException e1) { e1.printStackTrace(); } return result; } public String processStdout(InputStream in, String charset) { byte[] buf = new byte[1024]; StringBuffer sb = new StringBuffer(); try { while (in.read(buf) != -1) { sb.append(new String(buf, charset)); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } /** * @param args */ public static void main(String[] args) { RemoteShellTool tool = new RemoteShellTool("172.16.103.126", "ftpuser", "1qaz@WSX", "utf-8"); String result = tool.exec("sh Deploy.sh 172.16.103.121"); System.out.print("result:"+result); RemoteShellTool tool2 = new RemoteShellTool("172.16.103.121", "ftpuser", "1qaz@WSX", "utf-8"); String result2 = tool2.exec("sh tools/clientDeploy.sh"); System.out.print("result2:"+result2); } }