且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

利用apache commons-net 开源包,使用telnet方式获取AIX主机信息

更新时间:2022-07-02 13:58:33

一。AIX简介

 

AIX全名为Advanced Interactive Executive,俗称“An IBM uniX”或“Advanced IBM uniX”。

作为综合评价第一的unix操作系统(D.H. Brown咨询公司,1998 ),AIX是真正的第二代unix,具有性能卓越、易于使用、扩充性强、适合企业关键应用等众多特点。

支持300种以上的IBM软件和超过13000家独立软件厂商的软件产品。

是非常优秀的操作系统

在银行、电力系统、电信移动等企业应用很广泛

下面,我们介绍下对AIX系统的信息采集

二。

下面是一个利用apache commons-net 开源包, 使用telnet方式连接的工具类

实现对AIX主机信息的采集

因为提示符已经写死了,如果采用本例,请先按照自己的真实环境修改提示符和用户名和密码 等基本信息

 

  1. package test.collector.telnet;
  2. import java.io.InputStream;
  3. import java.io.PrintStream;
  4. import org.apache.commons.net.telnet.TelnetClient;
  5. /**
  6.  * 利用apache net 开源包,使用telnet方式获取AIX主机信息
  7.  * 
  8.  * @author zhaoyl
  9.  * @date 20008.7.21
  10.  * @version 1.2
  11.  */
  12. public class NetTelnet {
  13.     // Telnet对象
  14.     private TelnetClient telnet = new TelnetClient();
  15.     private InputStream in;
  16.     private PrintStream out;
  17.     // 提示符。具体请telnet到AIX主机查看
  18.     private char prompt = '#';
  19.     // telnet端口
  20.     private String port;
  21.     // 用户
  22.     private String user;
  23.     // 密码
  24.     private String password;
  25.     // IP地址
  26.     private String ip;
  27.     public NetTelnet() {
  28.         try {
  29.             // AIX主机IP
  30.             this.ip = "10.1.2.222";
  31.             this.password = "loeisdke";
  32.             this.user = "whdiwpasdq232sd2323";
  33.             this.port = "23";
  34.             telnet.connect(ip, Integer.parseInt(port));
  35.             in = telnet.getInputStream();
  36.             out = new PrintStream(telnet.getOutputStream());
  37.             // 登录
  38.             readUntil("login: ");
  39.             write(user);
  40.             readUntil("Password: ");
  41.             write(password);
  42.             readUntil(prompt + " ");
  43.         } catch (Exception e) {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47.     /**
  48.      * 读取分析结果
  49.      * 
  50.      * @param pattern
  51.      * @return
  52.      */
  53.     public String readUntil(String pattern) {
  54.         try {
  55.             char lastChar = pattern.charAt(pattern.length() - 1);
  56.             StringBuffer sb = new StringBuffer();
  57.             char ch = (char) in.read();
  58.             while (true) {
  59.                 sb.append(ch);
  60.                 if (ch == lastChar) {
  61.                     if (sb.toString().endsWith(pattern)) {
  62.                         return sb.toString();
  63.                     }
  64.                 }
  65.                 ch = (char) in.read();
  66.             }
  67.         } catch (Exception e) {
  68.             e.printStackTrace();
  69.         }
  70.         return null;
  71.     }
  72.     /**
  73.      * 写
  74.      * 
  75.      * @param value
  76.      */
  77.     public void write(String value) {
  78.         try {
  79.             out.println(value);
  80.             out.flush();
  81.         } catch (Exception e) {
  82.             e.printStackTrace();
  83.         }
  84.     }
  85.     /**
  86.      * 向目标发送命令字符串
  87.      * 
  88.      * @param command
  89.      * @return
  90.      */
  91.     public String sendCommand(String command) {
  92.         try {
  93.             write(command);
  94.             return readUntil(prompt + " ");
  95.         } catch (Exception e) {
  96.             e.printStackTrace();
  97.         }
  98.         return null;
  99.     }
  100.     /**
  101.      * 关闭连接
  102.      * 
  103.      */
  104.     public void disconnect() {
  105.         try {
  106.             telnet.disconnect();
  107.         } catch (Exception e) {
  108.             e.printStackTrace();
  109.         }
  110.     }
  111.     public static void main(String[] args) {
  112.         try {
  113.             NetTelnet telnet = new NetTelnet();
  114.             // 通过aix的命令“查找主机名称”获取数据
  115.             // 命令是 "hostname"
  116.             // 不熟悉命令的参考<<AIX网络管理手册>>
  117.             String result = telnet.sendCommand("hostname");
  118.             System.out.println(result);
  119.             // 最后一定要关闭
  120.             telnet.disconnect();
  121.         } catch (Exception e) {
  122.             e.printStackTrace();
  123.         }
  124.     }
  125. }