java ssh 访问linux,通过java使用ssh访问远程Linux

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

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

上代码了:

Java代码

ae85d055f6d177c5f734b3b0777b56e5.png

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.

Java代码

ae85d055f6d177c5f734b3b0777b56e5.png

Connection con = new Connection(String ip);

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

Java代码

ae85d055f6d177c5f734b3b0777b56e5.png

Connection con = new Connection(mc.getIpAddress(),2222);

因为的端口是2222.

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

刚开始我写成了

Java代码

ae85d055f6d177c5f734b3b0777b56e5.png

InputStream stdout = session.getStdout();

dr = new BufferedReader(new InputStreamReader(stdout));

怎么也娶不到东西。

后来写为

Java代码

ae85d055f6d177c5f734b3b0777b56e5.png

InputStream stdout  = new StreamGobbler(session.getStdout());

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

Java代码

ae85d055f6d177c5f734b3b0777b56e5.png

/**

* A StreamGobbler 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查看本地磁盘信息的方法:

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

Java代码

ae85d055f6d177c5f734b3b0777b56e5.png

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(),这个并不表示已经用了多少,而是磁盘可用空间。通常情况下,这个值和剩余容量是相等的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值