java使用ssh调用shell命令获取KVM数据(KVM需要通过libvirt管理)
来源:互联网 发布:在淘宝上怎么赚钱 编辑:程序博客网 时间:2024/05/21 17:41
1.查询所有的主机(运行和未运行的):
[root@localhost ~]# virsh list --all Id 名称 状态---------------------------------------------------- 1 bg-119 running 2 bg-120 running 3 bg-121 running 4 bg-132 running - bg-122 关闭 - bg-239 关闭 - bg-240 关闭 - bg-241 关闭 - centos7 关闭
2.查询单个的某台主机的基本信息:
[root@localhost ~]# virsh dominfo bg-119Id: 1名称: bg-119UUID: bb8e2dab-111f-4151-****-924ec2f7376bOS 类型: hvm状态: runningCPU: 4CPU 时间: 112076.1s最大内存: 16777216 KiB使用的内存: 16777216 KiB持久: 是自动启动: 禁用管理的保存: 否安全性模式: none安全性 DOI: 0
3.java中通过ssh调用shell命令获取数据:
3.1 maven添加jsch的jar包
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.53</version></dependency>
3.2 java代码(shell命令返回的数据为字符串,通过一些列解析获取对应值)
package com.mdl;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.Properties;import com.google.gson.Gson;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;import com.jcraft.jsch.SftpException;import com.purvar.vo.KVM;public class JSchUtils { private static JSch jsch; private static Session session; /** * 连接到指定的IP * * @throws JSchException */ public static void connect(String user, String passwd, String host, int port) throws JSchException { jsch = new JSch();// 创建JSch对象 session = jsch.getSession(user, host, port);// 根据用户名、主机ip、端口号获取一个Session对象 session.setPassword(passwd);// 设置密码 Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config);// 为Session对象设置properties session.connect();// 通过Session建立连接 } /** * 关闭连接 */ public static void close() { session.disconnect(); } /** * 执行相关的命令 * * @throws JSchException */ public static String execCmd(String command) throws JSchException { String resultStr = ""; BufferedReader reader = null; Channel channel = null; try { if (command != null) { channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); // ((ChannelExec) channel).setErrStream(System.err); channel.connect(); InputStream in = channel.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); String buf = null; while ((buf = reader.readLine()) != null) { resultStr = resultStr + "\n" + buf; System.out.println("resultStr---" + resultStr); } } } catch (IOException e) { e.printStackTrace(); } catch (JSchException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } channel.disconnect(); } return resultStr; } public static void main(String[] args) throws SftpException { try { // 1.连接到指定的服务器 connect("root", "mdl_12345", "172.*.*.45", 22); // 2.执行相关的命令,获取主机名称list String hostsStr = execCmd("virsh list"); List<String> hostNameList = getHostNameList(hostsStr); List<KVM> hostInfoList = new ArrayList<KVM>(); // 3.执行相关的命令,获取每台主机基本信息 for(int i=0;i<hostNameList.size();i++){ String commandStr = "virsh dominfo" + " " + hostNameList.get(i); String hostInfoStr = execCmd(commandStr); hostInfoList.add(getHostBaseInfo(hostInfoStr)); } Gson gs = new Gson(); String mStr = gs.toJson(hostInfoList); System.out.println(mStr); // 4.关闭连接 close(); } catch (JSchException e) { e.printStackTrace(); } } /** * 返回主机名的list * @param str * @return */ private static List<String> getHostNameList(String str){ String[] strArr = str.split("\\n"); List<String> hostList = new ArrayList<String>(); List<String> mList; for(int j=3;j<strArr.length;j++){ mList = new ArrayList<String>(); String[] arr = strArr[j].split(" "); for(int i=0;i<arr.length;i++){ if(!arr[i].equals("")){ mList.add(arr[i]); } } hostList.add(mList.get(1)); } return hostList; } /** * 返回主机的基本信息 * @param str * @return */ private static KVM getHostBaseInfo(String str){ KVM kvm = new KVM(); String[] strArr = str.split("\\n"); kvm.setUuid(formatStr(strArr[3])); kvm.setInstanceRunningId(formatStr(strArr[1]));// 运行时ID kvm.setInstanceName(formatStr(strArr[2]));// 虚拟机实例名称 kvm.setInstanceState(formatStr(strArr[5]));// 虚拟机状态 kvm.setOSType(formatStr(strArr[4]));// 系统类型 kvm.setCPUNumber(formatStr(strArr[6]));// CPU数量(个) kvm.setCPUTime(formatStr(strArr[7]));// CPU时间(s) kvm.setMAXMemory(formatStr(strArr[8]));// 最大使用内存 kvm.setMemoryUsed(formatStr(strArr[9]));// 已用内存 // String DiskNumber = "";//硬盘数量(个) return kvm; } /** * 去除字符串中间的空格 将中文:替换为英文: * * @param str * @return */ private static String formatStr(String str) { String result; if (str.indexOf(":") > 0) { result = str.replaceAll(" ", "").replaceAll(":", ":"); } else { result = str.replaceAll(" ", ""); } return result.split("\\:")[1]; }}
阅读全文
0 0
- java使用ssh调用shell命令获取KVM数据(KVM需要通过libvirt管理)
- java使用ssh调用shell命令获取KVM数据(KVM需要通过libvirt管理)(二)
- 使用libvirt管理kvm(API篇)
- 使用libvirt管理kvm(virsh篇)
- 使用libvirt管理kvm(virsh篇)
- 使用libvirt管理KVM虚拟机
- 使用libvirt管理kvm虚拟机
- 使用libvirt管理kvm虚拟机
- 使用libvirt管理kvm虚拟机
- 使用libvirt管理kvm虚拟机
- 通过python-libvirt管理KVM虚拟机-1
- 通过python-libvirt管理KVM虚拟机-2
- 使用 libvirt创建和管理KVM虚拟机
- 使用 libvirt创建和管理KVM虚拟机
- 虚拟化:使用libvirt管理kvm虚拟机
- 使用libvirt创建和管理KVM虚拟机
- 使用 libvirt创建和管理KVM虚拟机
- KVM Snapshot使用qemu-img, libvirt命令
- 000022:创建不同数值类型变量求和输出
- mycat心跳任务流程图
- CodeForces
- git代码回滚
- kotlin之新手入门(1)
- java使用ssh调用shell命令获取KVM数据(KVM需要通过libvirt管理)
- 教你如何用信用卡取现免手续费!
- windows时区数据
- ID3决策树的算法原理与python实现
- WebView进阶(一) :Android WebView与JS互相调用
- window对象方法
- jdk-AbstractQueuedSynchronizer(三)
- linux服务器安全优化设置
- react native 适配机顶盒、智能电视 遥控器解决焦点问题