生活随笔
收集整理的這篇文章主要介紹了
通过java使用ssh访问远程Linux
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
需要做一個監(jiān)控遠(yuǎn)程Linux磁盤空間的東西,絞盡腦汁終于發(fā)現(xiàn)一個東西。ch.ethz.ssh2。
它可以通過用戶名和密碼登錄可以ssh登錄的機(jī)器,并且可以執(zhí)行命令,并將命令顯示的東西返回來。
上代碼了:
Java代碼??
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代碼??
Connection?con?=?new?Connection(String?ip);??
?接收一個遠(yuǎn)程地址做參數(shù),默認(rèn)端口是22。如果不是這個端口,需要指定。比如我用的虛擬機(jī),使用了端口轉(zhuǎn)發(fā),所以寫成
Java代碼??
Connection?con?=?new?Connection(mc.getIpAddress(),2222);??
?因為的端口是2222.
2.session.getStdout() 的返回值是一個InputStream,但是需要包裝后才能用。
剛開始我寫成了
Java代碼??
InputStream?stdout?=?session.getStdout();?? dr?=?new?BufferedReader(new?InputStreamReader(stdout));??
?怎么也娶不到東西。
后來寫為
Java代碼??
InputStream?stdout??=?new?StreamGobbler(session.getStdout());??
?才好了。StreamGobbler是ch.ethz.ssh2自己的一個類,文檔如下:
Java代碼??
? ? ? ? ??
?
?=========================================另外補充一點Java查看本地磁盤信息的方法:
這也是在查找過程中找到的。
?
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()獲得的是這個盤的總?cè)萘?#xff0c;getFreeSpace()獲得的是剩余容量,還有個方法是getUsableSpace(),這個并不表示已經(jīng)用了多少,而是磁盤可用空間。通常情況下,這個值和剩余容量是相等的。
總結(jié)
以上是生活随笔為你收集整理的通过java使用ssh访问远程Linux的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。