简单的对 linux服务器服务检查的java程序

package com.Linux;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;
import org.junit.Assert;


/**
* commons-net-2.0.jar是工程依赖包
*/

public class NetTelnet {
TelnetClient telnet = new TelnetClient();// 初始化Telnet客户端连接
private InputStream is;
private PrintStream out;

// telnet连接初始化需要的参数
private String ip;// 远端主机IP
private int port;// telnet连接的端口号
private String user;// 用户名
private String password;// 密码
private char promopt = '$';// 命令提示符 普通用户命令以$号结束

public NetTelnet(String ip, int port, String user, String password) {
this.ip = ip;
this.port = port;
this.user = user;
this.password = password;

initLink(ip, port, user, password, promopt);
}

private void initLink(String ip, int port, String user, String password,char promopt) {
try {
telnet.connect(ip, port);// 连接到远端主机
is = telnet.getInputStream();
out=new PrintStream(telnet.getOutputStream());

// 根据是否是root用户设置命令结束符号
this.promopt = user.equals("root") ? '#' : '$';
login(user, password);
} catch (SocketException e) {
Assert.fail("可以ping通,但本机telent or xinetd服务没有启动");
} catch (IOException e) {
Assert.fail("读取远程机器显示数据出错");
//e.printStackTrace();
}

}

// 登陆主机
private void login(String user, String password) {

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//System.out.println("Timeout");
Assert.fail("连接超时");
}
readUntil("login:");// 得到远端发来的是输入用户名的指令
write(user);// 输入用户名
readUntil("Password:");
write(password);
readUntil(promopt + "");// 注意这种转化类型的用法

}

// 获取远端主机发送来的指令
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
char read = (char) is.read();
while (true) {
sb.append(read);
if (read == lastChar) {
if (sb.toString().contains(pattern)) {
return sb.toString();
}
}
read = (char) is.read();
}
} catch (IOException e) {
e.printStackTrace();
}

return "出错鸟";

}

// 用户发送过去的指令或者数据
public void write(String value) {
out.println(value);
out.flush();

}

// 向远程主机发送命令
public String sendCommand(String command) {
write(command);
return readUntil(promopt + "");
}

// 关闭连接
public void disConnect() {
try {
telnet.disconnect();
} catch (IOException e) {
//e.printStackTrace();
Assert.fail("关闭连接出错");
}
}

//public static void main(String[] args) {
//try {
//
//System.out.println("启动Telnet...");
//String ip = "192.168.50.148";
//int port = 23;
//String user = "root";
//String password = "11111111";
//NetTelnet telnet = new NetTelnet(ip, port, user, password);
//System.out.println("已经连接上");
//telnet.sendCommand("ps -ef |grep memcached");
//
//String r1 = telnet.sendCommand("ps -ef |grep memcached");
//String r2 = telnet.sendCommand("ps -ef |grep nginx");
//String r3 = telnet.sendCommand("ps -ef |grep php-fpm");
String rnginx =telnet.sendCommand("ps -C nginx");
ps -C nginx|cut -c25- (只显示一列)
//String rnginx =telnet.sendCommand("ps -C nginx|awk -- '{print $4}'");
//
//System.out.println("显示结果");
//rnginx=rnginx.trim();
//if(rnginx.contains("ngin1")){
// System.out.println("包含");
//}
//System.out.println(rnginx);
System.out.println(r2);
System.out.println(r3);
//telnet.disConnect();
//} catch (Exception e) {
//e.printStackTrace();
//}
//}


}


package com.Linux;

import java.net.InetAddress;


import org.junit.Test;

import static org.junit.Assert.assertThat;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.core.StringContains.containsString;

public class Check50_148 {
@Test
public void Service5_148(){
try {
InetAddress address = InetAddress.getByName("192.168.5.148");
assertThat("Ping不通", true, is(address.isReachable(5000)));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("启动Telnet...");
String ip = "192.168.50.148";
int port = 23;
String user = "root";
String password = "11111111";
NetTelnet telnet = new NetTelnet(ip, port, user, password);
System.out.println("已经连接上,开始进行服务检查");

String All =telnet.sendCommand("ps -A|awk -- '{print $4}'");
assertThat("xinetd服务没起来",All, containsString("xinetd\r\n"));
assertThat("nginx服务没起来",All, containsString("nginx\r\n"));
assertThat("sshd服务没起来",All, containsString("sshd\r\n"));
assertThat("redis-server服务没起来",All, containsString("redis-server\r\n"));
assertThat("php-fpm服务没起来",All, containsString("php-fpm\r\n"));
assertThat("mysqld服务没起来",All, containsString("mysqld\r\n"));
assertThat("memcached服务没起来",All, containsString("memcached\r\n"));
System.out.println("检查完毕无异常");
}
}




这事一个简单的对 linux服务器服务检查的程序
需要引入commons-net-3.1.jar , junit4.10.jar , hamcrest-core-1.3.jar , hamcrest-library-1.3.jar 第一个必须导入,后面3个如果不需要 assertthat断言的话可以去掉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值