zoukankan      html  css  js  c++  java
  • 服务器磁盘查看平台化

    针对Linux服务器,用平台一键、磁盘占用,由于采用用户名,密码登录SSH远程登录拿取数据,所以不需要安装客户端

    原文地址:http://blog.csdn.net/chenjiazhu/article/details/77532484

    查看磁盘占用

    平台根据远程服务器的登录名,密码,ssh登录远程服务器,执行df -lh获取数据

     

    public int[] viewDisk(String ip) throws IOException, InterruptedException
    	{
    		 RemoteShellTool tool = new RemoteShellTool(ip,"utf-8");
    		 tool.loginCentos();
    		 String returnString = tool.exec("df -lh"); 
    		 
    		 int  data[];
    		 
    		 if(returnString.indexOf("Filesystem")<0)
    		 {
    			 logger.info("viewDisk:"+ip+" return "+returnString);	
    			 data=new int[]{0,0};
    		 }
    		 else {
    			 returnString=returnString.replaceAll(" +"," ");
    			 String result[]=returnString.split("
    ");
    			 String res1[] = result[1].split(" ");
    			 
    			 data=new int[]{Integer.parseInt(res1[3].substring(0, res1[3].length()-1)),Integer.parseInt(res1[1].substring(0, res1[1].length()-1))};
    		}
    		 return data;
    	}	

    附其他代码:

    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">
        <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="/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/vmdetails.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="vmdetailsvm">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">虚拟机 : {{vmip}}</h3>
                    </div>
                    <div class="panel-body">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <td class="width-50">ID</td>
                                    <td>域名</td>
                                    <td>名称</td>
                                    <td class="width-200">应用类型</td>
                                    <td class="width-100">部门</td>
                                    <td class="width-100">负责人</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ms-repeat="appenvs">
                                    <td>{{$index+1}}</td>
                                    <td>{{el.application.domain}}</td>
                                    <td>{{el.application.name}}</td>
                                    <td>{{el.application.applicationtype.type}}</td>
                                    <td>{{el.application.department.name}}</td>
                                    <td>{{el.application.devs}}</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <!--FOOTER -->
        <div ms-include-src="'/home/footer.html'"></div>
    </body>
    
    </html>

     

    js

     

    /**
     * Created by chenjiazhu on 2017/5/10.
     */
    var vmdisk = avalon.define({
    	$id : 'vmdisk',
    
    	// VM Start
    	pagesize1 : "20",
    	pagesize1Cls : "pageSizeSelected",
    	pagesize2 : "50",
    	pagesize2Cls : "",
    	pagesize3 : "100",
    	pagesize3Cls : "",
    	changePageSize : function(pgsize) {
    		vmdisk.jpageSize = pgsize;
    		vmdisk.listVmInfosByPage("init");
    	},
    	clearsearch : function() {
    		vmdisk.conType = "";
    		vmdisk.listVmInfosByPage("init");
    	},
    	jpageIndex : 1,
    	jpageSize : 20,
    	envType : "STRESS",
    	conType : "",
    
    	vmsList : [],
    
    	listVmInfosByPage : function(tag) {
    		if (tag) {
    			vmdisk.jpageIndex = 1;
    		}
    		$.ajax({
    			type : "post",
    			url : 'listVmInfosByPageByEnvType.action',
    			data : {
    				"pageindex" : vmdisk.jpageIndex,
    				"pagesize" : vmdisk.jpageSize,
    				"type" : vmdisk.conType,
    				"envType" : vmdisk.envType
    			},
    			dataType : "json",
    			success : function(data) {
    				if (tag) {
    					$('#pagination').bootpag({
    						total : data.pagenum,
    						page : vmdisk.jpageIndex
    					});
    				}
    				if (data.retCode == "1000") {
    					vmdisk.vmsList = data.vms;
    				} else {
    					alert(data.retMSG);
    				}
    
    				$("[class^=free]").html("");
    				$("[class^=total]").html("");
    				$("[class*=progress-bar]").css("width", "0%");
    			},
    			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 : 'viewDisk.action',
    					data : {
    						"ip" : ip,
    						"os" : os
    					},
    					dataType : "json",
    
    					success : function(data) {
    
    						if (data.retCode == "1") {
    							$(".free_" + name).html("fail");
    							$(".total_" + name).html("fail");
    						} else {
    							$(".free_" + name).html(data.result[0]);
    							$(".total_" + name).html(data.result[1]);
    							
    							var processdata = 100
    									- (data.result[0] * 100 / data.result[1]);
    							$(".process_" + name).css("width", processdata+"%");
    
    							if (processdata > 90) {
    								$(".process_" + name).removeClass(
    										"progress-bar-success");
    								$(".process_" + name).addClass(
    										"progress-bar-danger");
    							} else {
    								if (processdata > 60) {
    									$(".process_" + name).removeClass(
    											"progress-bar-success");
    									$(".process_" + name).addClass(
    											"progress-bar-warning");
    								}
    							}
    
    						}
    						$(".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();
    					}
    				});
    
    	},
    
    	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();
    			}
    
    		});
    	},
    
    	checkAll : function() {
    		$("input[type='checkbox']").each(function() {
    			$(this).attr("checked", "true");
    		});
    	},
    
    	uncheckAll : function() {
    		$("input[type='checkbox']").each(function() {
    			$(this).removeAttr("checked");
    		});
    	},
    
    	loadVmTAB : function() {
    		vmdisk.listVmInfosByPage("init");
    		$('#vms').tab('show');
    	},
    	// VM END
    	userOps : ops(4),
    	bootpagFuc : function() {
    
    		$('#pagination').bootpag({
    			total : 1,
    			maxVisible : 10
    		}).on('page', function(event, num) {
    			vmdisk.jpageIndex = num;
    			vmdisk.listVmInfosByPage();
    		});
    	}
    });
    
    /*
     * avalon.ready(function() { appsvm.bootpagFuc(); appsvm.listApp("init");
     * appsvm.depList = getAllDepartments(); appsvm.envsList = getAllEnvs();
     * appsvm.applicationsTypeList = getAllAppType(); });
     */
    
    avalon.ready(function() {
    	/*
    	 * if (vmdisk.userOps) { vmdisk.loadVmTAB(); } else {
    	 * redirectAdminIndexPage(); }
    	 */
    	vmdisk.bootpagFuc();
    	vmdisk.listVmInfosByPage("init");
    	// $(".loadDiv").hide();
    });
    
    vmdisk.$watch("jpageSize", function(newValue) {
    	vmdisk.pagesize1Cls = "";
    	vmdisk.pagesize2Cls = "";
    	vmdisk.pagesize3Cls = "";
    	if (newValue == vmdisk.pagesize1) {
    		vmdisk.pagesize1Cls = "pageSizeSelected";
    	} else if (newValue == vmdisk.pagesize2) {
    		vmdisk.pagesize2Cls = "pageSizeSelected";
    	} else if (newValue == vmdisk.pagesize3) {
    		vmdisk.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);  
      
        }  
      
    }
    

    查看磁盘占用

    平台根据远程服务器的登录名,密码,ssh登录远程服务器,执行df -lh获取数据

     

    [java] view plain copy
     
    1. public int[] viewDisk(String ip) throws IOException, InterruptedException  
    2.     {  
    3.          RemoteShellTool tool = new RemoteShellTool(ip,"utf-8");  
    4.          tool.loginCentos();  
    5.          String returnString = tool.exec("df -lh");   
    6.            
    7.          int  data[];  
    8.            
    9.          if(returnString.indexOf("Filesystem")<0)  
    10.          {  
    11.              logger.info("viewDisk:"+ip+" return "+returnString);     
    12.              data=new int[]{0,0};  
    13.          }  
    14.          else {  
    15.              returnString=returnString.replaceAll(" +"," ");  
    16.              String result[]=returnString.split(" ");  
    17.              String res1[] = result[1].split(" ");  
    18.                
    19.              data=new int[]{Integer.parseInt(res1[3].substring(0, res1[3].length()-1)),Integer.parseInt(res1[1].substring(0, res1[1].length()-1))};  
    20.         }  
    21.          return data;  
    22.     }     

    附其他代码:

    html

    [html] view plain copy
     
    1. <!DOCTYPE html>  
    2. <html lang="zh-CN">  
    3.   
    4. <head>  
    5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    6.     <link href="/css/reset.css" rel="stylesheet">  
    7.     <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">  
    8.     <link href="/css/layout.css" rel="stylesheet">  
    9.     <script type="text/javascript" src="/lib/jquery.js"></script>  
    10.     <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>  
    11.     <script type="text/javascript" src="/lib/avalon.js"></script>  
    12.     <script type="text/javascript" src="/js/common/util.js"></script>  
    13.     <script type="text/javascript" src="/js/common/common.js"></script>  
    14.     <script type="text/javascript" src="/js/index/vmdetails.js"></script>  
    15.     <title>虚拟机详情</title>  
    16. </head>  
    17.   
    18. <body ms-controller="vm">  
    19.     <!-- HEAD -->  
    20.     <!--HEAD -->  
    21.     <div ms-include-src="'/home/header.html'"></div>  
    22.     <!-- Content -->  
    23.     <div class="container">  
    24.         <div ms-controller="vmdetailsvm">  
    25.             <div class="panel panel-default">  
    26.                 <div class="panel-heading">  
    27.                     <h3 class="panel-title">虚拟机 : {{vmip}}</h3>  
    28.                 </div>  
    29.                 <div class="panel-body">  
    30.                     <table class="table table-bordered">  
    31.                         <thead>  
    32.                             <tr>  
    33.                                 <td class="width-50">ID</td>  
    34.                                 <td>域名</td>  
    35.                                 <td>名称</td>  
    36.                                 <td class="width-200">应用类型</td>  
    37.                                 <td class="width-100">部门</td>  
    38.                                 <td class="width-100">负责人</td>  
    39.                             </tr>  
    40.                         </thead>  
    41.                         <tbody>  
    42.                             <tr ms-repeat="appenvs">  
    43.                                 <td>{{$index+1}}</td>  
    44.                                 <td>{{el.application.domain}}</td>  
    45.                                 <td>{{el.application.name}}</td>  
    46.                                 <td>{{el.application.applicationtype.type}}</td>  
    47.                                 <td>{{el.application.department.name}}</td>  
    48.                                 <td>{{el.application.devs}}</td>  
    49.                             </tr>  
    50.                         </tbody>  
    51.                     </table>  
    52.                 </div>  
    53.             </div>  
    54.         </div>  
    55.     </div>  
    56.     <!--FOOTER -->  
    57.     <div ms-include-src="'/home/footer.html'"></div>  
    58. </body>  
    59.   
    60. </html>  

     

    js

     

    [javascript] view plain copy
     
    1. /** 
    2.  * Created by chenjiazhu on 2017/5/10. 
    3.  */  
    4. var vmdisk = avalon.define({  
    5.     $id : 'vmdisk',  
    6.   
    7.     // VM Start  
    8.     pagesize1 : "20",  
    9.     pagesize1Cls : "pageSizeSelected",  
    10.     pagesize2 : "50",  
    11.     pagesize2Cls : "",  
    12.     pagesize3 : "100",  
    13.     pagesize3Cls : "",  
    14.     changePageSize : function(pgsize) {  
    15.         vmdisk.jpageSize = pgsize;  
    16.         vmdisk.listVmInfosByPage("init");  
    17.     },  
    18.     clearsearch : function() {  
    19.         vmdisk.conType = "";  
    20.         vmdisk.listVmInfosByPage("init");  
    21.     },  
    22.     jpageIndex : 1,  
    23.     jpageSize : 20,  
    24.     envType : "STRESS",  
    25.     conType : "",  
    26.   
    27.     vmsList : [],  
    28.   
    29.     listVmInfosByPage : function(tag) {  
    30.         if (tag) {  
    31.             vmdisk.jpageIndex = 1;  
    32.         }  
    33.         $.ajax({  
    34.             type : "post",  
    35.             url : 'listVmInfosByPageByEnvType.action',  
    36.             data : {  
    37.                 "pageindex" : vmdisk.jpageIndex,  
    38.                 "pagesize" : vmdisk.jpageSize,  
    39.                 "type" : vmdisk.conType,  
    40.                 "envType" : vmdisk.envType  
    41.             },  
    42.             dataType : "json",  
    43.             success : function(data) {  
    44.                 if (tag) {  
    45.                     $('#pagination').bootpag({  
    46.                         total : data.pagenum,  
    47.                         page : vmdisk.jpageIndex  
    48.                     });  
    49.                 }  
    50.                 if (data.retCode == "1000") {  
    51.                     vmdisk.vmsList = data.vms;  
    52.                 } else {  
    53.                     alert(data.retMSG);  
    54.                 }  
    55.   
    56.                 $("[class^=free]").html("");  
    57.                 $("[class^=total]").html("");  
    58.                 $("[class*=progress-bar]").css("width""0%");  
    59.             },  
    60.             error : function(XMLHttpRequest, textStatus, errorThrown) {  
    61.                 alert("请求数据异常,状态码:" + XMLHttpRequest.status + ",Error:"  
    62.                         + errorThrown + ",textStatus:" + textStatus);  
    63.             }  
    64.         });  
    65.     },  
    66.   
    67.     postViewStatus : function(name, ip, os) {  
    68.   
    69.         $(".loadDiv_view_" + name).show();  
    70.         $(".buttonDiv_view_" + name).hide();  
    71.   
    72.         $  
    73.                 .ajax({  
    74.                     type : "post",  
    75.                     url : 'viewDisk.action',  
    76.                     data : {  
    77.                         "ip" : ip,  
    78.                         "os" : os  
    79.                     },  
    80.                     dataType : "json",  
    81.   
    82.                     success : function(data) {  
    83.   
    84.                         if (data.retCode == "1") {  
    85.                             $(".free_" + name).html("fail");  
    86.                             $(".total_" + name).html("fail");  
    87.                         } else {  
    88.                             $(".free_" + name).html(data.result[0]);  
    89.                             $(".total_" + name).html(data.result[1]);  
    90.                               
    91.                             var processdata = 100  
    92.                                     - (data.result[0] * 100 / data.result[1]);  
    93.                             $(".process_" + name).css("width", processdata+"%");  
    94.   
    95.                             if (processdata > 90) {  
    96.                                 $(".process_" + name).removeClass(  
    97.                                         "progress-bar-success");  
    98.                                 $(".process_" + name).addClass(  
    99.                                         "progress-bar-danger");  
    100.                             } else {  
    101.                                 if (processdata > 60) {  
    102.                                     $(".process_" + name).removeClass(  
    103.                                             "progress-bar-success");  
    104.                                     $(".process_" + name).addClass(  
    105.                                             "progress-bar-warning");  
    106.                                 }  
    107.                             }  
    108.   
    109.                         }  
    110.                         $(".loadDiv_view_" + name).hide();  
    111.                         $(".buttonDiv_view_" + name).show();  
    112.                     },  
    113.                     error : function(XMLHttpRequest, textStatus, errorThrown) {  
    114.                         alert("请求数据异常,状态码:" + XMLHttpRequest.status + ",Error:"  
    115.                                 + errorThrown + ",textStatus:" + textStatus);  
    116.                         $(".loadDiv_view_" + name).hide();  
    117.                         $(".buttonDiv_view_" + name).show();  
    118.                     }  
    119.                 });  
    120.   
    121.     },  
    122.   
    123.     viewAllStatus : function() {  
    124.         $("input[type='checkbox']").each(function() {  
    125.             if ($(this).get(0).checked) {  
    126.   
    127.                 var name = $(this).attr("class").substr(6);  
    128.                 // alert(name);  
    129.                 $(".i_view_" + name).click();  
    130.             }  
    131.   
    132.         });  
    133.     },  
    134.   
    135.     checkAll : function() {  
    136.         $("input[type='checkbox']").each(function() {  
    137.             $(this).attr("checked""true");  
    138.         });  
    139.     },  
    140.   
    141.     uncheckAll : function() {  
    142.         $("input[type='checkbox']").each(function() {  
    143.             $(this).removeAttr("checked");  
    144.         });  
    145.     },  
    146.   
    147.     loadVmTAB : function() {  
    148.         vmdisk.listVmInfosByPage("init");  
    149.         $('#vms').tab('show');  
    150.     },  
    151.     // VM END  
    152.     userOps : ops(4),  
    153.     bootpagFuc : function() {  
    154.   
    155.         $('#pagination').bootpag({  
    156.             total : 1,  
    157.             maxVisible : 10  
    158.         }).on('page'function(event, num) {  
    159.             vmdisk.jpageIndex = num;  
    160.             vmdisk.listVmInfosByPage();  
    161.         });  
    162.     }  
    163. });  
    164.   
    165. /* 
    166.  * avalon.ready(function() { appsvm.bootpagFuc(); appsvm.listApp("init"); 
    167.  * appsvm.depList = getAllDepartments(); appsvm.envsList = getAllEnvs(); 
    168.  * appsvm.applicationsTypeList = getAllAppType(); }); 
    169.  */  
    170.   
    171. avalon.ready(function() {  
    172.     /* 
    173.      * if (vmdisk.userOps) { vmdisk.loadVmTAB(); } else { 
    174.      * redirectAdminIndexPage(); } 
    175.      */  
    176.     vmdisk.bootpagFuc();  
    177.     vmdisk.listVmInfosByPage("init");  
    178.     // $(".loadDiv").hide();  
    179. });  
    180.   
    181. vmdisk.$watch("jpageSize"function(newValue) {  
    182.     vmdisk.pagesize1Cls = "";  
    183.     vmdisk.pagesize2Cls = "";  
    184.     vmdisk.pagesize3Cls = "";  
    185.     if (newValue == vmdisk.pagesize1) {  
    186.         vmdisk.pagesize1Cls = "pageSizeSelected";  
    187.     } else if (newValue == vmdisk.pagesize2) {  
    188.         vmdisk.pagesize2Cls = "pageSizeSelected";  
    189.     } else if (newValue == vmdisk.pagesize3) {  
    190.         vmdisk.pagesize3Cls = "pageSizeSelected";  
    191.     }  
    192. })  

     

    工具类

    [java] view plain copy
     
    1. package com.ymt.testplatform.util;  
    2.   
    3. import java.io.IOException;  
    4. import java.io.InputStream;  
    5. import java.nio.charset.Charset;  
    6.   
    7. import ch.ethz.ssh2.Connection;  
    8. import ch.ethz.ssh2.Session;  
    9.   
    10. public class RemoteShellTool {  
    11.     private Connection conn;    
    12.     private String ipAddr;    
    13.     private String charset = Charset.defaultCharset().toString();    
    14.     private String userName;    
    15.     private String password;    
    16.     
    17.     public RemoteShellTool(String ipAddr, String charset) {    
    18.         this.ipAddr = ipAddr;    
    19.         if (charset != null) {    
    20.             this.charset = charset;    
    21.         }    
    22.     }    
    23.       
    24.     public RemoteShellTool(String ipAddr, String userName, String password,    
    25.             String charset) {    
    26.         this.ipAddr = ipAddr;    
    27.         this.userName = userName;    
    28.         this.password = password;    
    29.         if (charset != null) {    
    30.             this.charset = charset;    
    31.         }    
    32.     }    
    33.     
    34.     public boolean login() throws IOException {    
    35.         conn = new Connection(ipAddr);    
    36.         conn.connect(); // 连接    
    37.         return conn.authenticateWithPassword(userName, password); // 认证    
    38.     }    
    39.       
    40.     public boolean loginCentos() throws IOException  
    41.     {  
    42.          conn = new Connection(ipAddr);    
    43.          conn.connect(); // 连接    
    44.           
    45.         String [] pass = {"ymt@123","abcd@1234","root@1234","1qaz@WSX","1234qwer"};  
    46.           
    47.         for (String pa : pass) {  
    48.             if(conn.authenticateWithPassword("root", pa))  
    49.             {  
    50.                 this.userName = "root";    
    51.                 this.password = pa;    
    52.                 return true;  
    53.             }  
    54.         }         
    55.           
    56.         return false;  
    57.           
    58.     }  
    59.     
    60.     public boolean login(String userName,String password) throws IOException {    
    61.         conn = new Connection(ipAddr);    
    62.         conn.connect(); // 连接    
    63.         this.userName = userName;    
    64.         this.password = password;    
    65.         return conn.authenticateWithPassword(userName, password); // 认证    
    66.     }    
    67.     
    68.     public String exec(String cmds) {    
    69.         InputStream in = null;    
    70.         String result = "";    
    71.         try {    
    72.             if (this.login()) {    
    73.                 Session session = conn.openSession(); // 打开一个会话    
    74.                 session.execCommand(cmds);    
    75.                     
    76.                 in = session.getStdout();    
    77.                 result = this.processStdout(in, this.charset);    
    78.                 session.close();    
    79.                 conn.close();    
    80.             }    
    81.         } catch (IOException e1) {    
    82.             e1.printStackTrace();    
    83.         }    
    84.         return result;    
    85.     }    
    86.     
    87.     public String processStdout(InputStream in, String charset) {    
    88.         
    89.         byte[] buf = new byte[1024];    
    90.         StringBuffer sb = new StringBuffer();    
    91.         try {    
    92.             while (in.read(buf) != -1) {    
    93.                 sb.append(new String(buf, charset));    
    94.             }    
    95.         } catch (IOException e) {    
    96.             e.printStackTrace();    
    97.         }    
    98.         return sb.toString();    
    99.     }    
    100.     
    101.     /**  
    102.      * @param args  
    103.      */    
    104.     public static void main(String[] args) {    
    105.     
    106.         RemoteShellTool tool = new RemoteShellTool("172.16.103.126""ftpuser",    
    107.                 "1qaz@WSX""utf-8");    
    108.     
    109.         String result = tool.exec("sh Deploy.sh 172.16.103.121");    
    110.         System.out.print("result:"+result);    
    111.           
    112.         RemoteShellTool tool2 = new RemoteShellTool("172.16.103.121""ftpuser",    
    113.                 "1qaz@WSX""utf-8");    
    114.     
    115.         String result2 = tool2.exec("sh tools/clientDeploy.sh");    
    116.         System.out.print("result2:"+result2);    
    117.     
    118.     }    
    119.     
    120. }  
  • 相关阅读:
    TreeList 树形控件 实现带三种状态的CheckBox
    SQL 左外连接,右外连接,全连接,内连接(转)
    在DataTable中进行数据查询 (转)
    uva10594 Data Flow最小费用流,两个代码区别不大(我没看出区别),为什么一个对,另一个超时!!
    SGU142 Keyword好题
    uva 10881
    南京理工1747(数论)WA了好多遍!
    CF161D 树形dp
    uva 11646(大水题几何分类)
    求a加到b二进制加法有多少次进位。
  • 原文地址:https://www.cnblogs.com/chenjiazhu/p/7473618.html
Copyright © 2011-2022 走看看