java 连接远程服务器_java实现连接远程服务器并执行命令的基本原理
一、所需jar包
需要借助Ganymed SSH的jar包: ?ganymed-ssh2-build210.jar
二、實現(xiàn)原理
Ganymed SSH-2 java在整個訪問過程中擔(dān)當(dāng)SSH的客戶端,由于Linux系統(tǒng)自帶SSH服務(wù),所以可以直接訪問Linux系統(tǒng)并執(zhí)行相關(guān)命令,而?Windows系統(tǒng)則需要首先安裝SSH服務(wù)。
三、Win下SSH服務(wù)的安裝配置
當(dāng)遠程服務(wù)器為Windows系統(tǒng)時,?需要在遠程服務(wù)器中安裝Windows版的SSH服務(wù),比如:freesshd。
1.安裝完freesshd后,首選在[Users]下添加用來遠程連接的win系統(tǒng)用戶,此處采用密碼認證的方式,允許使用shell:
2.然后再在【Authentication】下設(shè)置允許密碼認證方式:
3.到[Server status]下查看SSH服務(wù)器狀態(tài),確保啟動即可。最后點擊【確定】即可。
四、java代碼實現(xiàn)遠程連接服務(wù)器并執(zhí)行命令
/*
*?To?change?this?license?header,?choose?License?Headers?in?Project?Properties.
*?To?change?this?template?file,?choose?Tools?|?Templates
*?and?open?the?template?in?the?editor.
*/
package?test;
import?java.io.IOException;
import?ch.ethz.ssh2.Connection;
import?ch.ethz.ssh2.Session;
import?ch.ethz.ssh2.StreamGobbler; import?java.io.BufferedReader; import?java.io.InputStream; import?java.io.InputStreamReader; /* ??@author:?Liu?Yuanyuan ??purpose:?test?connecting?remote?computer?and?execute?linux?command */ public?class?TestRemoteConnect? { ????public?static?void?main(String[]?args)? ????{ ????????String?hostname?=?"192.168.100.50"; ????????int?port?=?22;//22?usually?the?default?port ????????String?username?=?"root"; ????????String?password?=?"highgo"; ????????//指明連接主機的IP地址?? ????????Connection?conn?=?new?Connection(hostname,port); ????????Session?ssh?=?null; ????????try ????????{ ????????????//連接到主機?? ????????????conn.connect(); ????????????//使用用戶名和密碼校驗?? ????????????boolean?isconn?=?conn.authenticateWithPassword(username,?password); ????????????if?(!isconn)? ????????????{ ????????????????System.out.println("用戶名稱或者是密碼不正確"); ????????????}? ????????????else? ????{ ????????????????System.out.println("已經(jīng)連接OK"); ???????????????? ????????????????//以下是linux的示例 ????????????????//將本地conf/server_start.sh傳輸?shù)竭h程主機的/opt/pg944/目錄下 ????????????????SCPClient?clt?=?conn.createSCPClient();???????????????? ????????????????clt.put("conf/server_start.sh",?"/opt/pg944/");?????? ?????????????????????????? ????????????????//執(zhí)行命令 ????????????????ssh?=?conn.openSession();? ????????????????ssh.execCommand("sh?/root/hello.sh"); ????????????????//ssh.execCommand("perl?/root/hello.pl");?? ????????????????//只允許使用一行命令,即ssh對象只能使用一次execCommand這個方法,多次使用則會出現(xiàn)異常.????? ????????????????//使用多個命令用分號隔開?? ????????????????//ssh.execCommand("cd?/root;?sh?hello.sh");? ????????/*?執(zhí)行windows系統(tǒng)命令的示例 ????????Session?sess?=?conn.openSession(); ????????????????sess.execCommand("ipconfig"); ????????????????*/ //將Terminal屏幕上的文字全部打印出來?????????????????? ????????????????InputStream?is?=?new?StreamGobbler(ssh.getStdout()); ????????????????BufferedReader?brs?=?new?BufferedReader(new?InputStreamReader(is)); ????????????????while?(true)? { ????????????????????String?line?=?brs.readLine(); ????????????????????if?(line?==?null)? ????????????{ ????????????????????????break; ????????????????????} ????????????????????System.out.println(line); ????????????????} ????????????}???????????? ????????}? ????????catch?(IOException?e)? ????????{ ????????????System.out.println(e.getMessage()); ????????????e.printStackTrace(); ????????}? ????????finally? { ????????????//連接的Session和Connection對象都需要關(guān)閉?? ????????????if(ssh!=null) ????????????{ ????????????????ssh.close(); ????????????} ????????????if(conn!=null) ????????????{ ????????????????conn.close(); ????????????} ????????} ????} }
五、其他的實現(xiàn)方式:
總結(jié)
以上是生活随笔為你收集整理的java 连接远程服务器_java实现连接远程服务器并执行命令的基本原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL-locate()函数
- 下一篇: 边缘检测的简单例子(MATLAB)