通过ssh访问linux文件

本文介绍如何使用Java通过SSH连接Linux服务器,并获取Hadoop相关配置文件的方法。通过ganymed SSH2库实现连接,再利用自定义工具类执行Linux命令,最终读取和解析配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       使用java代码链接Linux服务器,无非使用SSH借用用户名密码进行链接,ganymed jar包提供了ssh的连接方式,下面我们借助Ambari开源项目代码,使用实例获得hadoop的一些配置文件并使用xml工具类进行解析。

       pom文件内容,引入ganymed和文本处理类commons-lang3:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>java</artifactId>
        <groupId>com.study.java</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>utils</artifactId>
    <properties>
        <java.version>1.8</java.version>
        <ganymed-ssh2.version>262</ganymed-ssh2.version>
        <apache-commons.version>3.5</apache-commons.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>${ganymed-ssh2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${apache-commons.version}</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

     CommandUtils类,getProcess获得进程信息和cat获得xml文件内容:

public class CommandUtils {

  private static DecimalFormat dFormat =new DecimalFormat("#.0");

  /**
   * 获得进程信息,从进程中得到服务部署地址
   * @param conn
   * @param processName
   * @param pwd
   * @return
   */
  public static String getProcess(Connection conn , String processName , String pwd ){
    String head = processName.substring(0,1);
    String end = processName.substring(1);
    String shellString = String.format( CtrCommond.LinuxCmd.PID , pwd,head,end);
    return  CtrCommond.doCommond(conn,shellString );
  }

  /**
   * 使用cat获得到文件内容信息
   * @param conn
   * @param configDir
   * @param pwd
   * @return
   */
  public static String catConfig(Connection conn , String configDir , String pwd ){

    String shellString = String.format( CtrCommond.LinuxCmd.Cat , pwd,configDir);
    return  CtrCommond.doCommond(conn,shellString );
  }

}
  CtrCommond类用于linux连接和docommand执行命令:
/**
 * Created by whp on 2017/12/28.
 */
public class CtrCommond {

  private static final Logger LOG = LoggerFactory.getLogger(CtrCommond.class);

  private static final String id_rsa_path =null;
  /**
   * 获取服务器链接
   */
  public static Connection getConn(String hostName, String userName, int sshPort, String passWord) {
    try {
      Connection conn = new Connection(hostName, sshPort);
      //连接到主机
      conn.connect();
      //使用用户名和密码校验
      boolean isconn = conn.authenticateWithPassword(userName, passWord);

      if (!isconn) {
        LOG.info("用户名称或者是密码不正确");
      } else {
        return conn;
      }
    } catch (IOException e) {
      LOG.error("获取服务器链接出现异常:" + e.toString());
      return null;
    }
    return null;
  }

  public static Connection getConn(String hostName, String userName, int sshPort,
                                   char[] pemPrivateKey, String password) {
    try {
      Connection conn = new Connection(hostName, sshPort);
      //连接到主机
      conn.connect();
      //使用PublicKey校验
      boolean isconn = conn.authenticateWithPublicKey(userName, pemPrivateKey, password);
      if (!isconn) {
        LOG.info("用户名称或者是密码不正确");
      } else {
        return conn;
      }
    } catch (IOException e) {
      LOG.error("获取服务器链接出现异常:" + e.toString());
      return null;
    }
    return null;
  }

  public static Connection getConn(String hostName, String userName, int sshPort,
                                   File pemFile, String password) {
    try {
      Connection conn = new Connection(hostName, sshPort);
      //连接到主机
      conn.connect();
      boolean isconn = conn.authenticateWithPublicKey(userName, pemFile, password);
      if (!isconn) {
        LOG.info("用户名称或者是密码不正确");
      } else {
        return conn;
      }
    } catch (IOException e) {
      LOG.error("获取服务器链接出现异常:" + e.toString());
      return null;
    }
    return null;
  }

  /**
   * 远程执行命令
   */
  public static String doCommond(Connection conn, String cmd) {
    String result = "";
    try {
      if (conn == null) {
        LOG.info("请先链接服务器");
      } else {
        Session sess = conn.openSession();
        sess.execCommand(cmd);
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
        while (true) {
          String line = stdoutReader.readLine();
          if (line == null) {
            break;
          }
          result += line + StaticKeys.SPLIT_BR;
        }
        //连接的Session和Connection对象都需要关闭
        stdoutReader.close();
        sess.close();
      }
    } catch (IOException e) {
      LOG.error("执行linux命令错误:" + e.toString());
    }
    if (result.endsWith(StaticKeys.SPLIT_BR)) {
      result = result.substring(0, result.length() - StaticKeys.SPLIT_BR.length());
    }

    if (!StringUtils.isEmpty(result)) {
      if (cmd.contains("DEV") || cmd.contains("iostat")) {
        if (result.contains("</br></br>")) {
          result = result.substring(result.lastIndexOf("</br></br>") + 10);
        }
      }
      if (cmd.contains("mpstat")) {
        if (result.contains("</br></br>")) {
          result = result.substring(result.lastIndexOf("</br></br>") + 10);
          int s = result.indexOf("</br>") + 5;
          s = result.indexOf("</br>", s);
          result = result.substring(0, s);
        }
      }
    }
    return result;
  }

  public class LinuxCmd {

    /**
     * 根据进程名称查询进程
     */
    public static final String PID = "echo %s | sudo -S ps aux | grep -i [%s]%s ";


    public static final String Cat = "echo %s | sudo -S cat %s ";

    public static final String VIEW_MEM = "free -m";//查看内存状态

    public static final String SYSTEM_RELEASE = "cat /etc/system-release";//查看系统版本

    public static final String UNAME_A = "uname -a";//查看系统详细信息

    public static final String DF_HL = "df -k | grep '^\\/dev/' | awk '{print $2,$3,$4}'";//查看磁盘空间

    //物理cpu个数
    public static final String WULI_CPU_NUM = "cat /proc/cpuinfo| grep \"physical id\"| sort| uniq| wc -l";

    //每个cpu的核数
    public static final String WULI_CPU_CORE_NUM = "cat /proc/cpuinfo| grep \"cpu cores\"| uniq";

    //cpu型号信息
    public static final String CPU_XINGHAO = "cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c";

    //cpu使用情况
    public static final String VMSTAT = "top -b -n 1 | sed -n '3p'";

    //查看服务器网络吞吐率
    public static final String SAR_DEV_1 = "sar -n DEV 1 3";

    //查看磁盘IO使用情况
    public static final String DISK_IO = "iostat -xkz 1 1";

    //tcp状态
    public static final String SAR_TCP_1 = "sar -n TCP 1 3";

    //查看系统负载状态
    public static final String UPTIME = "uptime";

    //根据PID查看进程状态
    public static final String dd = "ps aux|head -1;ps aux|grep {pid}";

    public static final String rpcinfo = "rpcinfo -p";//看rpc服务开放

    public static final String lsmod = "lsmod";//检查系统内核模块

    public static final String passwd_update_time = "ls -l /etc/passwd";//查看passwd文件修改时间

    public static final String crontab = "cat /etc/crontab";//查看计划任务

    public static final String promisc = "ip link | grep promisc";//检查网络:ip link | grep PROMISC(正常网卡不该在promisc模式,可能存在sniffer)

    public static final String date = "date \"+%Y-%m-%d %H:%M:%S\"";
    public static final String ntp = "ntpdate ";

    public static final String reboot = "reboot ";

    public static final String shutdown = "shutdown -h now ";
  }

}

     PathUtils类,对于访问路径进行格式化:

/**
 * @author whp 18-7-16
 */
public class PathUtils {

    /**
     * 进程配置转换
     * @param process
     * @param serviceType
     * @return
     */
    public static String parse(String process  , String serviceType) {
        String homeDir ="";
        if(process!=null ) {
            String []javaConfig =  process.split(" ");
            for(String config : javaConfig ) {
                if(config.contains("-Dhadoop.home.dir") && serviceType.equals("hadoop")) {
                    String []dir = config.split("=");
                    homeDir = dir[1];
                    break;
                }else if(config.contains("-Dhbase.home.dir") && serviceType.equals("hbase")) {
                    String []dir = config.split("=");
                    homeDir = dir[1];
                    break;
                }else if( serviceType.equals("hive")) {
                    if(config.contains("-D") || config.contains("-X"))
                        continue;
                    String pattern = "(.*)\\/lib\\/.*$";
                    Pattern r = Pattern.compile(pattern);
                    Matcher m = r.matcher(config);
                    if(m.find()){
                        homeDir= m.group(1);
                        break;
                    }
                }
            }
        }
        return homeDir;
    }
}
/**
 */
public class StaticKeys {
	
	public static String SPLIT_BR = "</br>";//换行标识
	
	public static String SPLIT_KG = " ";//空格
	
	public static String SPLIT_DH = ",";//逗号

	public static String SPLIT_MH = ":";//逗号

	public static String SPLIT_SXG = "//";//双反斜杠

	public static String SPLIT_LINE = "\n";//换行标识

	public static String MAC ="";//本机mac地址
	
}

       xml格式化类,xml格式化借助于java提供的dom4j,使用document将xml转化为element对象,然后遍历elements将其转化为list,并将element的name作为key值进行保存。

public class XmlMapUtils {
  public static void main(String[] args) throws DocumentException, IOException {
//		String textFromFile = FileUtils.readFileToString(new File("D:/workspace/workspace_3.7/xml2map/src/xml2json/sample.xml"),"UTF-8");
//		Map<String, Object> map = xml2map(textFromFile, false);
//		// long begin = System.currentTimeMillis();
//		// for(int i=0; i<1000; i++){
//		// map = (Map<String, Object>) xml2mapWithAttr(doc.getRootElement());
//		// }
//		// System.out.println("耗时:"+(System.currentTimeMillis()-begin));
//		JSON json = JSONObject.fromObject(map);
//		System.out.println(json.toString(1)); // 格式化输出
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("q", "");
    map.put("w", "");
    map.put("e", "");
    Document doc = map2xml(map, "root");
    //Document doc = map2xml(map); //map中含有根节点的键
    System.out.println(formatXml(doc));
  }

  /**
   * xml转map 不带属性
   * @param xmlStr
   * @param needRootKey 是否需要在返回的map里加根节点键
   * @return
   * @throws DocumentException
   */
  public static Map xml2map(String xmlStr, boolean needRootKey) throws DocumentException {
    xmlStr = xmlStr.replace("</br>","");
    Document doc = DocumentHelper.parseText(xmlStr);
    Element root = doc.getRootElement();
    Map<String, Object> map = (Map<String, Object>) xml2map(root);
    if(root.elements().size()==0 && root.attributes().size()==0){
      return map;
    }
    if(needRootKey){
      //在返回的map里加根节点键(如果需要)
      Map<String, Object> rootMap = new HashMap<String, Object>();
      rootMap.put(root.getName(), map);
      return rootMap;
    }
    return map;
  }

  /**
   * xml转map 带属性
   * @param xmlStr
   * @param needRootKey 是否需要在返回的map里加根节点键
   * @return
   * @throws DocumentException
   */
  public static Map xml2mapWithAttr(String xmlStr, boolean needRootKey) throws DocumentException {
    Document doc = DocumentHelper.parseText(xmlStr);
    Element root = doc.getRootElement();
    Map<String, Object> map = (Map<String, Object>) xml2mapWithAttr(root);
    if(root.elements().size()==0 && root.attributes().size()==0){
      return map; //根节点只有一个文本内容
    }
    if(needRootKey){
      //在返回的map里加根节点键(如果需要)
      Map<String, Object> rootMap = new HashMap<String, Object>();
      rootMap.put(root.getName(), map);
      return rootMap;
    }
    return map;
  }

  /**
   * xml转map 不带属性
   * @param e
   * @return
   */
  private static Map xml2map(Element e) {
    Map map = new LinkedHashMap();
    List list = e.elements();
    if (list.size() > 0) {
      for (int i = 0; i < list.size(); i++) {
        Element iter = (Element) list.get(i);
        List mapList = new ArrayList();

        if (iter.elements().size() > 0) {
          Map m = xml2map(iter);
          if (map.get(iter.getName()) != null) {
            Object obj = map.get(iter.getName());
            if (!(obj instanceof List)) {
              mapList = new ArrayList();
              mapList.add(obj);
              mapList.add(m);
            }
            if (obj instanceof List) {
              mapList = (List) obj;
              mapList.add(m);
            }
            map.put(iter.getName(), mapList);
          } else
            map.put(iter.getName(), m);
        } else {
          if (map.get(iter.getName()) != null) {
            Object obj = map.get(iter.getName());
            if (!(obj instanceof List)) {
              mapList = new ArrayList();
              mapList.add(obj);
              mapList.add(iter.getText());
            }
            if (obj instanceof List) {
              mapList = (List) obj;
              mapList.add(iter.getText());
            }
            map.put(iter.getName(), mapList);
          } else
            map.put(iter.getName(), iter.getText());
        }
      }
    } else
      map.put(e.getName(), e.getText());
    return map;
  }

  /**
   * xml转map 带属性
   * @param
   * @return
   */
  private static Map xml2mapWithAttr(Element element) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();

    List<Element> list = element.elements();
    List<Attribute> listAttr0 = element.attributes(); // 当前节点的所有属性的list
    for (Attribute attr : listAttr0) {
      map.put("@" + attr.getName(), attr.getValue());
    }
    if (list.size() > 0) {

      for (int i = 0; i < list.size(); i++) {
        Element iter = list.get(i);
        List mapList = new ArrayList();

        if (iter.elements().size() > 0) {
          Map m = xml2mapWithAttr(iter);
          if (map.get(iter.getName()) != null) {
            Object obj = map.get(iter.getName());
            if (!(obj instanceof List)) {
              mapList = new ArrayList();
              mapList.add(obj);
              mapList.add(m);
            }
            if (obj instanceof List) {
              mapList = (List) obj;
              mapList.add(m);
            }
            map.put(iter.getName(), mapList);
          } else
            map.put(iter.getName(), m);
        } else {

          List<Attribute> listAttr = iter.attributes(); // 当前节点的所有属性的list
          Map<String, Object> attrMap = null;
          boolean hasAttributes = false;
          if (listAttr.size() > 0) {
            hasAttributes = true;
            attrMap = new LinkedHashMap<String, Object>();
            for (Attribute attr : listAttr) {
              attrMap.put("@" + attr.getName(), attr.getValue());
            }
          }

          if (map.get(iter.getName()) != null) {
            Object obj = map.get(iter.getName());
            if (!(obj instanceof List)) {
              mapList = new ArrayList();
              mapList.add(obj);
              // mapList.add(iter.getText());
              if (hasAttributes) {
                attrMap.put("#text", iter.getText());
                mapList.add(attrMap);
              } else {
                mapList.add(iter.getText());
              }
            }
            if (obj instanceof List) {
              mapList = (List) obj;
              // mapList.add(iter.getText());
              if (hasAttributes) {
                attrMap.put("#text", iter.getText());
                mapList.add(attrMap);
              } else {
                mapList.add(iter.getText());
              }
            }
            map.put(iter.getName(), mapList);
          } else {
            // map.put(iter.getName(), iter.getText());
            if (hasAttributes) {
              attrMap.put("#text", iter.getText());
              map.put(iter.getName(), attrMap);
            } else {
              map.put(iter.getName(), iter.getText());
            }
          }
        }
      }
    } else {
      // 根节点的
      if (listAttr0.size() > 0) {
        map.put("#text", element.getText());
      } else {
        map.put(element.getName(), element.getText());
      }
    }
    return map;
  }

  /**
   * map转xml map中没有根节点的键
   * @param map
   * @param rootName
   * @throws DocumentException
   * @throws IOException
   */
  public static Document map2xml(Map<String, Object> map, String rootName) throws DocumentException, IOException  {
    Document doc = DocumentHelper.createDocument();
    Element root = DocumentHelper.createElement(rootName);
    doc.add(root);
    map2xml(map, root);
    //System.out.println(doc.asXML());
    //System.out.println(formatXml(doc));
    return doc;
  }

  /**
   * map转xml map中含有根节点的键
   * @param map
   * @throws DocumentException
   * @throws IOException
   */
  public static Document map2xml(Map<String, Object> map) throws DocumentException, IOException  {
    Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
    if(entries.hasNext()){ //获取第一个键创建根节点
      Map.Entry<String, Object> entry = entries.next();
      Document doc = DocumentHelper.createDocument();
      Element root = DocumentHelper.createElement(entry.getKey());
      doc.add(root);
      map2xml((Map)entry.getValue(), root);
      //System.out.println(doc.asXML());
      //System.out.println(formatXml(doc));
      return doc;
    }
    return null;
  }

  /**
   * map转xml
   * @param map
   * @param body xml元素
   * @return
   */
  private static Element map2xml(Map<String, Object> map, Element body) {
    Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
    while (entries.hasNext()) {
      Map.Entry<String, Object> entry = entries.next();
      String key = entry.getKey();
      Object value = entry.getValue();
      if(key.startsWith("@")){	//属性
        body.addAttribute(key.substring(1, key.length()), value.toString());
      } else if(key.equals("#text")){	//有属性时的文本
        body.setText(value.toString());
      } else {
        if(value instanceof List ){
          List list = (List)value;
          Object obj;
          for(int i=0; i<list.size(); i++){
            obj = list.get(i);
            //list里是map或String,不会存在list里直接是list的,
            if(obj instanceof Map){
              Element subElement = body.addElement(key);
              map2xml((Map)list.get(i), subElement);
            } else {
              body.addElement(key).setText((String)list.get(i));
            }
          }
        } else if(value instanceof Map ){
          Element subElement = body.addElement(key);
          map2xml((Map)value, subElement);
        } else {
          body.addElement(key).setText(value.toString());
        }
      }
      //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    }
    return body;
  }

  /**
   * 格式化输出xml
   * @param xmlStr
   * @return
   * @throws DocumentException
   * @throws IOException
   */
  public static String formatXml(String xmlStr) throws DocumentException, IOException {
    Document document = DocumentHelper.parseText(xmlStr);
    return formatXml(document);
  }

  /**
   * 格式化输出xml
   * @param document
   * @return
   * @throws DocumentException
   * @throws IOException
   */
  public static String formatXml(Document document) throws DocumentException, IOException  {
    // 格式化输出格式
    OutputFormat format = OutputFormat.createPrettyPrint();
    //format.setEncoding("UTF-8");
    StringWriter writer = new StringWriter();
    // 格式化输出流
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    document.getRootElement().asXML();
    // 将document写入到输出流
    xmlWriter.write(document);
    xmlWriter.close();
    return writer.toString();
  }

}

    test测试类:

public class LinuxCommondTest {
    private final String HOST="192.168.1.221";
    private final int PORT=22;
    private final String USER_NAME="root";
    private final String PASSWORD="123qwe";
    private final String PROCESS_NAME="namenode";

    @Test
    public void testLoadXML(){
        Connection conn= CtrCommond.getConn(HOST,USER_NAME,PORT,PASSWORD);
        String process= CommandUtils.getProcess(conn,PROCESS_NAME,PASSWORD);
        System.out.println("===process=="+process);
        String homeDir= PathUtils.parse(process,"hadoop");
        System.out.println("===homDir=="+homeDir);
        String []hadoopDefaultConfig = {"hdfs-site.xml","core-site.xml","yarn-site.xml","mapred-site.xml"};
        for(String defaultConfig : hadoopDefaultConfig) {
            String configDir = homeDir + "/etc/hadoop/" + defaultConfig;
            String xmlFile = CommandUtils.catConfig(conn, configDir, PASSWORD);
            System.out.println(xmlFile);
        }
    }
}

    文章涉及到xml转化,ssh访问linux系统,command执行linux命令的代码编写,详细工程请参考本人github项目:

https://github.com/whpHarper/java下面utils子工程。

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值