通过ssh访问linux文件

深碍√TFBOYSˉ_ 2022-05-20 10:24 294阅读 0赞
  1. 使用java代码链接Linux服务器,无非使用SSH借用用户名密码进行链接,ganymed jar包提供了ssh的连接方式,下面我们借助Ambari开源项目代码,使用实例获得hadoop的一些配置文件并使用xml工具类进行解析。
  2. pom文件内容,引入ganymed和文本处理类commons-lang3
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <project xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  7. <parent>
  8. <artifactId>java</artifactId>
  9. <groupId>com.study.java</groupId>
  10. <version>1.0-SNAPSHOT</version>
  11. </parent>
  12. <modelVersion>4.0.0</modelVersion>
  13. <artifactId>utils</artifactId>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <ganymed-ssh2.version>262</ganymed-ssh2.version>
  17. <apache-commons.version>3.5</apache-commons.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>ch.ethz.ganymed</groupId>
  22. <artifactId>ganymed-ssh2</artifactId>
  23. <version>${ganymed-ssh2.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.commons</groupId>
  27. <artifactId>commons-lang3</artifactId>
  28. <version>${apache-commons.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>dom4j</groupId>
  32. <artifactId>dom4j</artifactId>
  33. <version>1.6.1</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>junit</groupId>
  37. <artifactId>junit</artifactId>
  38. <version>4.12</version>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. </project>
  43. CommandUtils类,getProcess获得进程信息和cat获得xml文件内容:
  44. public class CommandUtils {
  45. private static DecimalFormat dFormat =new DecimalFormat("#.0");
  46. /**
  47. * 获得进程信息,从进程中得到服务部署地址
  48. * @param conn
  49. * @param processName
  50. * @param pwd
  51. * @return
  52. */
  53. public static String getProcess(Connection conn , String processName , String pwd ){
  54. String head = processName.substring(0,1);
  55. String end = processName.substring(1);
  56. String shellString = String.format( CtrCommond.LinuxCmd.PID , pwd,head,end);
  57. return CtrCommond.doCommond(conn,shellString );
  58. }
  59. /**
  60. * 使用cat获得到文件内容信息
  61. * @param conn
  62. * @param configDir
  63. * @param pwd
  64. * @return
  65. */
  66. public static String catConfig(Connection conn , String configDir , String pwd ){
  67. String shellString = String.format( CtrCommond.LinuxCmd.Cat , pwd,configDir);
  68. return CtrCommond.doCommond(conn,shellString );
  69. }
  70. }
  71. CtrCommond类用于linux连接和docommand执行命令:
  72. /**
  73. * Created by whp on 2017/12/28.
  74. */
  75. public class CtrCommond {
  76. private static final Logger LOG = LoggerFactory.getLogger(CtrCommond.class);
  77. private static final String id_rsa_path =null;
  78. /**
  79. * 获取服务器链接
  80. */
  81. public static Connection getConn(String hostName, String userName, int sshPort, String passWord) {
  82. try {
  83. Connection conn = new Connection(hostName, sshPort);
  84. //连接到主机
  85. conn.connect();
  86. //使用用户名和密码校验
  87. boolean isconn = conn.authenticateWithPassword(userName, passWord);
  88. if (!isconn) {
  89. LOG.info("用户名称或者是密码不正确");
  90. } else {
  91. return conn;
  92. }
  93. } catch (IOException e) {
  94. LOG.error("获取服务器链接出现异常:" + e.toString());
  95. return null;
  96. }
  97. return null;
  98. }
  99. public static Connection getConn(String hostName, String userName, int sshPort,
  100. char[] pemPrivateKey, String password) {
  101. try {
  102. Connection conn = new Connection(hostName, sshPort);
  103. //连接到主机
  104. conn.connect();
  105. //使用PublicKey校验
  106. boolean isconn = conn.authenticateWithPublicKey(userName, pemPrivateKey, password);
  107. if (!isconn) {
  108. LOG.info("用户名称或者是密码不正确");
  109. } else {
  110. return conn;
  111. }
  112. } catch (IOException e) {
  113. LOG.error("获取服务器链接出现异常:" + e.toString());
  114. return null;
  115. }
  116. return null;
  117. }
  118. public static Connection getConn(String hostName, String userName, int sshPort,
  119. File pemFile, String password) {
  120. try {
  121. Connection conn = new Connection(hostName, sshPort);
  122. //连接到主机
  123. conn.connect();
  124. boolean isconn = conn.authenticateWithPublicKey(userName, pemFile, password);
  125. if (!isconn) {
  126. LOG.info("用户名称或者是密码不正确");
  127. } else {
  128. return conn;
  129. }
  130. } catch (IOException e) {
  131. LOG.error("获取服务器链接出现异常:" + e.toString());
  132. return null;
  133. }
  134. return null;
  135. }
  136. /**
  137. * 远程执行命令
  138. */
  139. public static String doCommond(Connection conn, String cmd) {
  140. String result = "";
  141. try {
  142. if (conn == null) {
  143. LOG.info("请先链接服务器");
  144. } else {
  145. Session sess = conn.openSession();
  146. sess.execCommand(cmd);
  147. InputStream stdout = new StreamGobbler(sess.getStdout());
  148. BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
  149. while (true) {
  150. String line = stdoutReader.readLine();
  151. if (line == null) {
  152. break;
  153. }
  154. result += line + StaticKeys.SPLIT_BR;
  155. }
  156. //连接的Session和Connection对象都需要关闭
  157. stdoutReader.close();
  158. sess.close();
  159. }
  160. } catch (IOException e) {
  161. LOG.error("执行linux命令错误:" + e.toString());
  162. }
  163. if (result.endsWith(StaticKeys.SPLIT_BR)) {
  164. result = result.substring(0, result.length() - StaticKeys.SPLIT_BR.length());
  165. }
  166. if (!StringUtils.isEmpty(result)) {
  167. if (cmd.contains("DEV") || cmd.contains("iostat")) {
  168. if (result.contains("</br></br>")) {
  169. result = result.substring(result.lastIndexOf("</br></br>") + 10);
  170. }
  171. }
  172. if (cmd.contains("mpstat")) {
  173. if (result.contains("</br></br>")) {
  174. result = result.substring(result.lastIndexOf("</br></br>") + 10);
  175. int s = result.indexOf("</br>") + 5;
  176. s = result.indexOf("</br>", s);
  177. result = result.substring(0, s);
  178. }
  179. }
  180. }
  181. return result;
  182. }
  183. public class LinuxCmd {
  184. /**
  185. * 根据进程名称查询进程
  186. */
  187. public static final String PID = "echo %s | sudo -S ps aux | grep -i [%s]%s ";
  188. public static final String Cat = "echo %s | sudo -S cat %s ";
  189. public static final String VIEW_MEM = "free -m";//查看内存状态
  190. public static final String SYSTEM_RELEASE = "cat /etc/system-release";//查看系统版本
  191. public static final String UNAME_A = "uname -a";//查看系统详细信息
  192. public static final String DF_HL = "df -k | grep '^\\/dev/' | awk '{print $2,$3,$4}'";//查看磁盘空间
  193. //物理cpu个数
  194. public static final String WULI_CPU_NUM = "cat /proc/cpuinfo| grep \"physical id\"| sort| uniq| wc -l";
  195. //每个cpu的核数
  196. public static final String WULI_CPU_CORE_NUM = "cat /proc/cpuinfo| grep \"cpu cores\"| uniq";
  197. //cpu型号信息
  198. public static final String CPU_XINGHAO = "cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c";
  199. //cpu使用情况
  200. public static final String VMSTAT = "top -b -n 1 | sed -n '3p'";
  201. //查看服务器网络吞吐率
  202. public static final String SAR_DEV_1 = "sar -n DEV 1 3";
  203. //查看磁盘IO使用情况
  204. public static final String DISK_IO = "iostat -xkz 1 1";
  205. //tcp状态
  206. public static final String SAR_TCP_1 = "sar -n TCP 1 3";
  207. //查看系统负载状态
  208. public static final String UPTIME = "uptime";
  209. //根据PID查看进程状态
  210. public static final String dd = "ps aux|head -1;ps aux|grep {pid}";
  211. public static final String rpcinfo = "rpcinfo -p";//看rpc服务开放
  212. public static final String lsmod = "lsmod";//检查系统内核模块
  213. public static final String passwd_update_time = "ls -l /etc/passwd";//查看passwd文件修改时间
  214. public static final String crontab = "cat /etc/crontab";//查看计划任务
  215. public static final String promisc = "ip link | grep promisc";//检查网络:ip link | grep PROMISC(正常网卡不该在promisc模式,可能存在sniffer)
  216. public static final String date = "date \"+%Y-%m-%d %H:%M:%S\"";
  217. public static final String ntp = "ntpdate ";
  218. public static final String reboot = "reboot ";
  219. public static final String shutdown = "shutdown -h now ";
  220. }
  221. }
  222. PathUtils类,对于访问路径进行格式化:
  223. /**
  224. * @author whp 18-7-16
  225. */
  226. public class PathUtils {
  227. /**
  228. * 进程配置转换
  229. * @param process
  230. * @param serviceType
  231. * @return
  232. */
  233. public static String parse(String process , String serviceType) {
  234. String homeDir ="";
  235. if(process!=null ) {
  236. String []javaConfig = process.split(" ");
  237. for(String config : javaConfig ) {
  238. if(config.contains("-Dhadoop.home.dir") && serviceType.equals("hadoop")) {
  239. String []dir = config.split("=");
  240. homeDir = dir[1];
  241. break;
  242. }else if(config.contains("-Dhbase.home.dir") && serviceType.equals("hbase")) {
  243. String []dir = config.split("=");
  244. homeDir = dir[1];
  245. break;
  246. }else if( serviceType.equals("hive")) {
  247. if(config.contains("-D") || config.contains("-X"))
  248. continue;
  249. String pattern = "(.*)\\/lib\\/.*$";
  250. Pattern r = Pattern.compile(pattern);
  251. Matcher m = r.matcher(config);
  252. if(m.find()){
  253. homeDir= m.group(1);
  254. break;
  255. }
  256. }
  257. }
  258. }
  259. return homeDir;
  260. }
  261. }
  262. /**
  263. */
  264. public class StaticKeys {
  265. public static String SPLIT_BR = "</br>";//换行标识
  266. public static String SPLIT_KG = " ";//空格
  267. public static String SPLIT_DH = ",";//逗号
  268. public static String SPLIT_MH = ":";//逗号
  269. public static String SPLIT_SXG = "//";//双反斜杠
  270. public static String SPLIT_LINE = "\n";//换行标识
  271. public static String MAC ="";//本机mac地址
  272. }
  273. xml格式化类,xml格式化借助于java提供的dom4j,使用documentxml转化为element对象,然后遍历elements将其转化为list,并将elementname作为key值进行保存。
  274. public class XmlMapUtils {
  275. public static void main(String[] args) throws DocumentException, IOException {
  276. // String textFromFile = FileUtils.readFileToString(new File("D:/workspace/workspace_3.7/xml2map/src/xml2json/sample.xml"),"UTF-8");
  277. // Map<String, Object> map = xml2map(textFromFile, false);
  278. // // long begin = System.currentTimeMillis();
  279. // // for(int i=0; i<1000; i++){
  280. // // map = (Map<String, Object>) xml2mapWithAttr(doc.getRootElement());
  281. // // }
  282. // // System.out.println("耗时:"+(System.currentTimeMillis()-begin));
  283. // JSON json = JSONObject.fromObject(map);
  284. // System.out.println(json.toString(1)); // 格式化输出
  285. Map<String,Object> map = new HashMap<String, Object>();
  286. map.put("q", "");
  287. map.put("w", "");
  288. map.put("e", "");
  289. Document doc = map2xml(map, "root");
  290. //Document doc = map2xml(map); //map中含有根节点的键
  291. System.out.println(formatXml(doc));
  292. }
  293. /**
  294. * xml转map 不带属性
  295. * @param xmlStr
  296. * @param needRootKey 是否需要在返回的map里加根节点键
  297. * @return
  298. * @throws DocumentException
  299. */
  300. public static Map xml2map(String xmlStr, boolean needRootKey) throws DocumentException {
  301. xmlStr = xmlStr.replace("</br>","");
  302. Document doc = DocumentHelper.parseText(xmlStr);
  303. Element root = doc.getRootElement();
  304. Map<String, Object> map = (Map<String, Object>) xml2map(root);
  305. if(root.elements().size()==0 && root.attributes().size()==0){
  306. return map;
  307. }
  308. if(needRootKey){
  309. //在返回的map里加根节点键(如果需要)
  310. Map<String, Object> rootMap = new HashMap<String, Object>();
  311. rootMap.put(root.getName(), map);
  312. return rootMap;
  313. }
  314. return map;
  315. }
  316. /**
  317. * xml转map 带属性
  318. * @param xmlStr
  319. * @param needRootKey 是否需要在返回的map里加根节点键
  320. * @return
  321. * @throws DocumentException
  322. */
  323. public static Map xml2mapWithAttr(String xmlStr, boolean needRootKey) throws DocumentException {
  324. Document doc = DocumentHelper.parseText(xmlStr);
  325. Element root = doc.getRootElement();
  326. Map<String, Object> map = (Map<String, Object>) xml2mapWithAttr(root);
  327. if(root.elements().size()==0 && root.attributes().size()==0){
  328. return map; //根节点只有一个文本内容
  329. }
  330. if(needRootKey){
  331. //在返回的map里加根节点键(如果需要)
  332. Map<String, Object> rootMap = new HashMap<String, Object>();
  333. rootMap.put(root.getName(), map);
  334. return rootMap;
  335. }
  336. return map;
  337. }
  338. /**
  339. * xml转map 不带属性
  340. * @param e
  341. * @return
  342. */
  343. private static Map xml2map(Element e) {
  344. Map map = new LinkedHashMap();
  345. List list = e.elements();
  346. if (list.size() > 0) {
  347. for (int i = 0; i < list.size(); i++) {
  348. Element iter = (Element) list.get(i);
  349. List mapList = new ArrayList();
  350. if (iter.elements().size() > 0) {
  351. Map m = xml2map(iter);
  352. if (map.get(iter.getName()) != null) {
  353. Object obj = map.get(iter.getName());
  354. if (!(obj instanceof List)) {
  355. mapList = new ArrayList();
  356. mapList.add(obj);
  357. mapList.add(m);
  358. }
  359. if (obj instanceof List) {
  360. mapList = (List) obj;
  361. mapList.add(m);
  362. }
  363. map.put(iter.getName(), mapList);
  364. } else
  365. map.put(iter.getName(), m);
  366. } else {
  367. if (map.get(iter.getName()) != null) {
  368. Object obj = map.get(iter.getName());
  369. if (!(obj instanceof List)) {
  370. mapList = new ArrayList();
  371. mapList.add(obj);
  372. mapList.add(iter.getText());
  373. }
  374. if (obj instanceof List) {
  375. mapList = (List) obj;
  376. mapList.add(iter.getText());
  377. }
  378. map.put(iter.getName(), mapList);
  379. } else
  380. map.put(iter.getName(), iter.getText());
  381. }
  382. }
  383. } else
  384. map.put(e.getName(), e.getText());
  385. return map;
  386. }
  387. /**
  388. * xml转map 带属性
  389. * @param
  390. * @return
  391. */
  392. private static Map xml2mapWithAttr(Element element) {
  393. Map<String, Object> map = new LinkedHashMap<String, Object>();
  394. List<Element> list = element.elements();
  395. List<Attribute> listAttr0 = element.attributes(); // 当前节点的所有属性的list
  396. for (Attribute attr : listAttr0) {
  397. map.put("@" + attr.getName(), attr.getValue());
  398. }
  399. if (list.size() > 0) {
  400. for (int i = 0; i < list.size(); i++) {
  401. Element iter = list.get(i);
  402. List mapList = new ArrayList();
  403. if (iter.elements().size() > 0) {
  404. Map m = xml2mapWithAttr(iter);
  405. if (map.get(iter.getName()) != null) {
  406. Object obj = map.get(iter.getName());
  407. if (!(obj instanceof List)) {
  408. mapList = new ArrayList();
  409. mapList.add(obj);
  410. mapList.add(m);
  411. }
  412. if (obj instanceof List) {
  413. mapList = (List) obj;
  414. mapList.add(m);
  415. }
  416. map.put(iter.getName(), mapList);
  417. } else
  418. map.put(iter.getName(), m);
  419. } else {
  420. List<Attribute> listAttr = iter.attributes(); // 当前节点的所有属性的list
  421. Map<String, Object> attrMap = null;
  422. boolean hasAttributes = false;
  423. if (listAttr.size() > 0) {
  424. hasAttributes = true;
  425. attrMap = new LinkedHashMap<String, Object>();
  426. for (Attribute attr : listAttr) {
  427. attrMap.put("@" + attr.getName(), attr.getValue());
  428. }
  429. }
  430. if (map.get(iter.getName()) != null) {
  431. Object obj = map.get(iter.getName());
  432. if (!(obj instanceof List)) {
  433. mapList = new ArrayList();
  434. mapList.add(obj);
  435. // mapList.add(iter.getText());
  436. if (hasAttributes) {
  437. attrMap.put("#text", iter.getText());
  438. mapList.add(attrMap);
  439. } else {
  440. mapList.add(iter.getText());
  441. }
  442. }
  443. if (obj instanceof List) {
  444. mapList = (List) obj;
  445. // mapList.add(iter.getText());
  446. if (hasAttributes) {
  447. attrMap.put("#text", iter.getText());
  448. mapList.add(attrMap);
  449. } else {
  450. mapList.add(iter.getText());
  451. }
  452. }
  453. map.put(iter.getName(), mapList);
  454. } else {
  455. // map.put(iter.getName(), iter.getText());
  456. if (hasAttributes) {
  457. attrMap.put("#text", iter.getText());
  458. map.put(iter.getName(), attrMap);
  459. } else {
  460. map.put(iter.getName(), iter.getText());
  461. }
  462. }
  463. }
  464. }
  465. } else {
  466. // 根节点的
  467. if (listAttr0.size() > 0) {
  468. map.put("#text", element.getText());
  469. } else {
  470. map.put(element.getName(), element.getText());
  471. }
  472. }
  473. return map;
  474. }
  475. /**
  476. * map转xml map中没有根节点的键
  477. * @param map
  478. * @param rootName
  479. * @throws DocumentException
  480. * @throws IOException
  481. */
  482. public static Document map2xml(Map<String, Object> map, String rootName) throws DocumentException, IOException {
  483. Document doc = DocumentHelper.createDocument();
  484. Element root = DocumentHelper.createElement(rootName);
  485. doc.add(root);
  486. map2xml(map, root);
  487. //System.out.println(doc.asXML());
  488. //System.out.println(formatXml(doc));
  489. return doc;
  490. }
  491. /**
  492. * map转xml map中含有根节点的键
  493. * @param map
  494. * @throws DocumentException
  495. * @throws IOException
  496. */
  497. public static Document map2xml(Map<String, Object> map) throws DocumentException, IOException {
  498. Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
  499. if(entries.hasNext()){ //获取第一个键创建根节点
  500. Map.Entry<String, Object> entry = entries.next();
  501. Document doc = DocumentHelper.createDocument();
  502. Element root = DocumentHelper.createElement(entry.getKey());
  503. doc.add(root);
  504. map2xml((Map)entry.getValue(), root);
  505. //System.out.println(doc.asXML());
  506. //System.out.println(formatXml(doc));
  507. return doc;
  508. }
  509. return null;
  510. }
  511. /**
  512. * map转xml
  513. * @param map
  514. * @param body xml元素
  515. * @return
  516. */
  517. private static Element map2xml(Map<String, Object> map, Element body) {
  518. Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
  519. while (entries.hasNext()) {
  520. Map.Entry<String, Object> entry = entries.next();
  521. String key = entry.getKey();
  522. Object value = entry.getValue();
  523. if(key.startsWith("@")){ //属性
  524. body.addAttribute(key.substring(1, key.length()), value.toString());
  525. } else if(key.equals("#text")){ //有属性时的文本
  526. body.setText(value.toString());
  527. } else {
  528. if(value instanceof List ){
  529. List list = (List)value;
  530. Object obj;
  531. for(int i=0; i<list.size(); i++){
  532. obj = list.get(i);
  533. //list里是map或String,不会存在list里直接是list的,
  534. if(obj instanceof Map){
  535. Element subElement = body.addElement(key);
  536. map2xml((Map)list.get(i), subElement);
  537. } else {
  538. body.addElement(key).setText((String)list.get(i));
  539. }
  540. }
  541. } else if(value instanceof Map ){
  542. Element subElement = body.addElement(key);
  543. map2xml((Map)value, subElement);
  544. } else {
  545. body.addElement(key).setText(value.toString());
  546. }
  547. }
  548. //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  549. }
  550. return body;
  551. }
  552. /**
  553. * 格式化输出xml
  554. * @param xmlStr
  555. * @return
  556. * @throws DocumentException
  557. * @throws IOException
  558. */
  559. public static String formatXml(String xmlStr) throws DocumentException, IOException {
  560. Document document = DocumentHelper.parseText(xmlStr);
  561. return formatXml(document);
  562. }
  563. /**
  564. * 格式化输出xml
  565. * @param document
  566. * @return
  567. * @throws DocumentException
  568. * @throws IOException
  569. */
  570. public static String formatXml(Document document) throws DocumentException, IOException {
  571. // 格式化输出格式
  572. OutputFormat format = OutputFormat.createPrettyPrint();
  573. //format.setEncoding("UTF-8");
  574. StringWriter writer = new StringWriter();
  575. // 格式化输出流
  576. XMLWriter xmlWriter = new XMLWriter(writer, format);
  577. document.getRootElement().asXML();
  578. // 将document写入到输出流
  579. xmlWriter.write(document);
  580. xmlWriter.close();
  581. return writer.toString();
  582. }
  583. }
  584. test测试类:
  585. public class LinuxCommondTest {
  586. private final String HOST="192.168.1.221";
  587. private final int PORT=22;
  588. private final String USER_NAME="root";
  589. private final String PASSWORD="123qwe";
  590. private final String PROCESS_NAME="namenode";
  591. @Test
  592. public void testLoadXML(){
  593. Connection conn= CtrCommond.getConn(HOST,USER_NAME,PORT,PASSWORD);
  594. String process= CommandUtils.getProcess(conn,PROCESS_NAME,PASSWORD);
  595. System.out.println("===process=="+process);
  596. String homeDir= PathUtils.parse(process,"hadoop");
  597. System.out.println("===homDir=="+homeDir);
  598. String []hadoopDefaultConfig = {"hdfs-site.xml","core-site.xml","yarn-site.xml","mapred-site.xml"};
  599. for(String defaultConfig : hadoopDefaultConfig) {
  600. String configDir = homeDir + "/etc/hadoop/" + defaultConfig;
  601. String xmlFile = CommandUtils.catConfig(conn, configDir, PASSWORD);
  602. System.out.println(xmlFile);
  603. }
  604. }
  605. }
  606. 文章涉及到xml转化,ssh访问linux系统,command执行linux命令的代码编写,详细工程请参考本人github项目:

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

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读