需要做一个监控远程Linux磁盘空间的东西,绞尽脑汁终于发现一个东西。ch.ethz.ssh2。
它可以通过用户名和密码登录可以ssh登录的机器,并且可以执行命令,并将命令显示的东西返回来。
上代码了:
- Connection con = null;
- Session session = null;
- BufferedReader dr = null;
- try {
- String ipd = mc.getIpAddress();
- if(ipd.equals("127.0.0.1")){
- con = new Connection(mc.getIpAddress(),2222);
- }else{
- con = new Connection(mc.getIpAddress());
- }
- ConnectionInfo info = con.connect();
- boolean result = con.authenticateWithPassword(mc.getUserName(), mc.getPassword());
- session = con.openSession();
- session.execCommand("df -T");
- InputStream stdout = session.getStdout();
- stdout = new StreamGobbler(session.getStdout());
- dr = new BufferedReader(new InputStreamReader(stdout));
- String line;
- while ((line=dr.readLine()) != null) {
- System.out.println(line);
- if(line.startsWith("/dev/")){
- Pattern p = Pattern.compile("[\\s]+");
- String[] arrs = p.split(line);
- for (String s : arrs) {
- System.out.println(s);
- }
- if(!arrs[1].startsWith("iso")){
- if(Long.parseLong(arrs[4])<5L*1024*1024 || Double.parseDouble(arrs[5])>0.9d){
- doAfterThing(mc, arrs[0]);
- }
- }
- }
- }
- } catch (Exception e) {
- System.err.println(e.getMessage());
- } finally {
- try {
- dr.close();
- session.close();
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
要注意的地方有两点:
1.
- Connection con = new Connection(String ip);
接收一个远程地址做参数,默认端口是22。如果不是这个端口,需要指定。比如我用的虚拟机,使用了端口转发,所以写成
- Connection con = new Connection(mc.getIpAddress(),2222);
因为的端口是2222.
2.session.getStdout() 的返回值是一个InputStream,但是需要包装后才能用。
刚开始我写成了
- InputStream stdout = session.getStdout();
- dr = new BufferedReader(new InputStreamReader(stdout));
怎么也娶不到东西。
后来写为
- InputStream stdout = new StreamGobbler(session.getStdout());
才好了。StreamGobbler是ch.ethz.ssh2自己的一个类,文档如下:
- /**
- * A <code>StreamGobbler</code> is an InputStream that uses an internal worker
- * thread to constantly consume input from another InputStream. It uses a buffer
- * to store the consumed data. The buffer size is automatically adjusted, if needed.
- */
=========================================另外补充一点Java查看本地磁盘信息的方法:
这也是在查找过程中找到的。
- File[] roots = File.listRoots();
- System.err.println("the count of roots is : "+roots.length);
- double constm = 1024 * 1024 * 1024 ;
- double total = 0d;
- for (File _file : roots) {
- System.out.println(_file.getPath());
- double total0 = _file.getTotalSpace()/constm,free0=_file.getFreeSpace()/constm,used0=total0-free0;
- System.out.println("totol space = " + total0+" G");
- System.out.print("the free space = " + free0+" G");
- System.out.println("---------- "+free0*100/total0+"% ----------");
- System.out.print("the used space = " + used0+" G");
- System.out.println("---------- "+used0*100/total0+"% ----------");
- System.out.println();
- total+=_file.getTotalSpace();
- }
- System.out.println("the total space of the machine = "+doubleFormat(total/constm));
代码很简单,不过有一点要注意:getTotalSpace()获得的是这个盘的总容量,getFreeSpace()获得的是剩余容量,还有个方法是getUsableSpace(),这个并不表示已经用了多少,而是磁盘可用空间。通常情况下,这个值和剩余容量是相等的。