日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

通过java使用ssh访问远程Linux

發(fā)布時間:2024/1/23 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过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代碼??
  • /**?
  • ?*?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查看本地磁盤信息的方法:

    這也是在查找過程中找到的。

    ?

    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)容還不錯,歡迎將生活随笔推薦給好友。