通过java使用ssh访问远程Linux

需要做一个监控远程Linux磁盘空间的东西,绞尽脑汁终于发现一个东西。ch.ethz.ssh2。

它可以通过用户名和密码登录可以ssh登录的机器,并且可以执行命令,并将命令显示的东西返回来。

上代码了:

Java代码   收藏代码
  1. Connection con = null;  
  2.             Session session = null;  
  3.             BufferedReader dr = null;  
  4.             try {  
  5.                 String ipd = mc.getIpAddress();  
  6.                 if(ipd.equals("127.0.0.1")){  
  7.                     con = new Connection(mc.getIpAddress(),2222);  
  8.                 }else{  
  9.                     con = new Connection(mc.getIpAddress());  
  10.                 }  
  11.                 ConnectionInfo info = con.connect();  
  12.                 boolean result = con.authenticateWithPassword(mc.getUserName(), mc.getPassword());  
  13.                 session = con.openSession();  
  14.                 session.execCommand("df -T");  
  15.                 InputStream stdout = session.getStdout();  
  16.                 stdout = new StreamGobbler(session.getStdout());  
  17.                 dr = new BufferedReader(new InputStreamReader(stdout));  
  18.                 String line;  
  19.                 while ((line=dr.readLine()) != null) {  
  20.                     System.out.println(line);  
  21.                     if(line.startsWith("/dev/")){  
  22.                         Pattern p = Pattern.compile("[\\s]+");  
  23.                         String[] arrs = p.split(line);  
  24.                         for (String s : arrs) {  
  25.                             System.out.println(s);  
  26.                         }  
  27.                         if(!arrs[1].startsWith("iso")){  
  28.                             if(Long.parseLong(arrs[4])<5L*1024*1024 || Double.parseDouble(arrs[5])>0.9d){  
  29.                                 doAfterThing(mc, arrs[0]);  
  30.                             }  
  31.                         }  
  32.                     }  
  33.                 }  
  34.             } catch (Exception e) {  
  35.                 System.err.println(e.getMessage());  
  36.             } finally {  
  37.                 try {  
  38.                     dr.close();  
  39.                     session.close();  
  40.                     con.close();  
  41.                 } catch (Exception e) {  
  42.                     e.printStackTrace();  
  43.                 }  
  44.             }  

 要注意的地方有两点:

1.

Java代码   收藏代码
  1. Connection con = new Connection(String ip);  

 接收一个远程地址做参数,默认端口是22。如果不是这个端口,需要指定。比如我用的虚拟机,使用了端口转发,所以写成

Java代码   收藏代码
  1. Connection con = new Connection(mc.getIpAddress(),2222);  

 因为的端口是2222.

2.session.getStdout() 的返回值是一个InputStream,但是需要包装后才能用。

刚开始我写成了

Java代码   收藏代码
  1. InputStream stdout = session.getStdout();  
  2. dr = new BufferedReader(new InputStreamReader(stdout));  

 怎么也娶不到东西。

后来写为

Java代码   收藏代码
  1. InputStream stdout  = new StreamGobbler(session.getStdout());  

 才好了。StreamGobbler是ch.ethz.ssh2自己的一个类,文档如下:

Java代码   收藏代码
  1. /** 
  2.  * A <code>StreamGobbler</code> is an InputStream that uses an internal worker 
  3.  * thread to constantly consume input from another InputStream. It uses a buffer 
  4.  * to store the consumed data. The buffer size is automatically adjusted, if needed. 
  5. */  

 

 =========================================另外补充一点Java查看本地磁盘信息的方法:

这也是在查找过程中找到的。

 

Java代码   收藏代码
  1. File[] roots = File.listRoots();  
  2. System.err.println("the count of roots is : "+roots.length);  
  3. double constm = 1024 * 1024 * 1024 ;  
  4.         double total = 0d;  
  5.         for (File _file : roots) {  
  6.             System.out.println(_file.getPath());  
  7.             double total0 = _file.getTotalSpace()/constm,free0=_file.getFreeSpace()/constm,used0=total0-free0;  
  8.             System.out.println("totol space = " + total0+" G");  
  9.             System.out.print("the free space = " + free0+" G");  
  10.             System.out.println("---------- "+free0*100/total0+"% ----------");  
  11.             System.out.print("the used space = " + used0+" G");  
  12.             System.out.println("---------- "+used0*100/total0+"% ----------");  
  13.             System.out.println();  
  14.             total+=_file.getTotalSpace();  
  15.         }  
  16.         System.out.println("the total space of the machine = "+doubleFormat(total/constm));  

 代码很简单,不过有一点要注意:getTotalSpace()获得的是这个盘的总容量,getFreeSpace()获得的是剩余容量,还有个方法是getUsableSpace(),这个并不表示已经用了多少,而是磁盘可用空间。通常情况下,这个值和剩余容量是相等的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值