利用apache commons-net 开源包,使用telnet方式获取AIX主机信息
一。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主机信息的采集
因为提示符已经写死了,如果采用本例,请先按照自己的真实环境修改提示符和用户名和密码 等基本信息
[*]package test.collector.telnet;
[*]
[*]import java.io.InputStream;
[*]import java.io.PrintStream;
[*]
[*]import org.apache.commons.net.telnet.TelnetClient;
[*]
[*]/**
[*] * 利用apache net 开源包,使用telnet方式获取AIX主机信息
[*] *
[*] * @author zhaoyl
[*] * @date 20008.7.21
[*] * @version 1.2
[*] */
[*]public class NetTelnet {
[*]
[*] // Telnet对象
[*] private TelnetClient telnet = new TelnetClient();
[*]
[*] private InputStream in;
[*]
[*] private PrintStream out;
[*]
[*] // 提示符。具体请telnet到AIX主机查看
[*] private char prompt = '#';
[*] // telnet端口
[*] private String port;
[*] // 用户
[*] private String user;
[*] // 密码
[*] private String password;
[*] // IP地址
[*] private String ip;
[*]
[*] public NetTelnet() {
[*]
[*] try {
[*] // AIX主机IP
[*] this.ip = "10.1.2.222";
[*] this.password = "loeisdke";
[*] this.user = "whdiwpasdq232sd2323";
[*] this.port = "23";
[*] telnet.connect(ip, Integer.parseInt(port));
[*]
[*] in = telnet.getInputStream();
[*] out = new PrintStream(telnet.getOutputStream());
[*] // 登录
[*] readUntil("login: ");
[*] write(user);
[*] readUntil("Password: ");
[*] write(password);
[*] readUntil(prompt + " ");
[*] } catch (Exception e) {
[*] e.printStackTrace();
[*] }
[*] }
[*]
[*] /**
[*] * 读取分析结果
[*] *
[*] * @param pattern
[*] * @return
[*] */
[*] public String readUntil(String pattern) {
[*] try {
[*] char lastChar = pattern.charAt(pattern.length() - 1);
[*] StringBuffer sb = new StringBuffer();
[*]
[*] char ch = (char) in.read();
[*] while (true) {
[*]
[*] sb.append(ch);
[*] if (ch == lastChar) {
[*] if (sb.toString().endsWith(pattern)) {
[*] return sb.toString();
[*] }
[*] }
[*] ch = (char) in.read();
[*] }
[*] } catch (Exception e) {
[*] e.printStackTrace();
[*] }
[*] return null;
[*] }
[*]
[*] /**
[*] * 写
[*] *
[*] * @param value
[*] */
[*] public void write(String value) {
[*] try {
[*] out.println(value);
[*] out.flush();
[*]
[*] } catch (Exception e) {
[*] e.printStackTrace();
[*] }
[*] }
[*]
[*] /**
[*] * 向目标发送命令字符串
[*] *
[*] * @param command
[*] * @return
[*] */
[*] public String sendCommand(String command) {
[*] try {
[*] write(command);
[*] return readUntil(prompt + " ");
[*] } catch (Exception e) {
[*] e.printStackTrace();
[*] }
[*] return null;
[*] }
[*]
[*] /**
[*] * 关闭连接
[*] *
[*] */
[*] public void disconnect() {
[*] try {
[*] telnet.disconnect();
[*] } catch (Exception e) {
[*] e.printStackTrace();
[*] }
[*] }
[*]
[*] public static void main(String[] args) {
[*] try {
[*] NetTelnet telnet = new NetTelnet();
[*] // 通过aix的命令“查找主机名称”获取数据
[*] // 命令是 "hostname"
[*] // 不熟悉命令的参考<<AIX网络管理手册>>
[*] String result = telnet.sendCommand("hostname");
[*]
[*] System.out.println(result);
[*] // 最后一定要关闭
[*] telnet.disconnect();
[*] } catch (Exception e) {
[*] e.printStackTrace();
[*] }
[*] }
[*]}
[*]
页:
[1]