欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java通過(guò)ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例

 更新時(shí)間:2017年02月09日 16:54:35   投稿:lqh  
這篇文章主要介紹了java通過(guò)ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例方法的相關(guān)資料

java通過(guò)ssh連接服務(wù)器執(zhí)行shell命令詳解

java通過(guò)ssh連接服務(wù)器執(zhí)行shell命令:JSch 是SSH2的一個(gè)純Java實(shí)現(xiàn)。它允許你連接到一個(gè)sshd 服務(wù)器,使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)鹊取D憧梢詫⑺墓δ芗傻侥阕约旱?程序中。同時(shí)該項(xiàng)目也提供一個(gè)J2ME版本用來(lái)在手機(jī)上直連SSHD服務(wù)器。

SSH是Secure Shell的縮寫,一種建立在應(yīng)用層和傳輸層基礎(chǔ)上的安全協(xié)議。SSH在連接和傳送過(guò)程中會(huì)加密所有數(shù)據(jù),可以用來(lái)在不同系統(tǒng)或者服務(wù)器之間進(jìn)行安全連接。SSH提供兩種的安全驗(yàn)證方式:基于密碼的認(rèn)證和基于密匙的認(rèn)證。其中,基于密碼的認(rèn)證比較簡(jiǎn)單,只要知道遠(yuǎn)程主機(jī)的用戶名和密碼,就可以進(jìn)行登錄?;诿艹椎恼J(rèn)證比較麻煩,而且連接比較耗時(shí),這里不詳細(xì)介紹。

有很多基于SSH協(xié)議的客戶端,例如:PuTTY、OpenSSH、Xshell 4等,可以遠(yuǎn)程連接幾乎所有UNIX平臺(tái)。同時(shí),可以通過(guò)Linux命令行ssh uername@host連接到某主機(jī)。

在項(xiàng)目中,如何利用代碼實(shí)現(xiàn)SSH,遠(yuǎn)程執(zhí)行Shell腳本呢?JSch是Java Secure Channel的縮寫,是一個(gè)SSH2功能的純Java實(shí)現(xiàn),具體信息可以參考JSch官網(wǎng)。它允許你連接到一個(gè)SSH服務(wù)器,并且可以使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)?,同時(shí)你也可以集成它的功能到你自己的應(yīng)用程序。在使用前,需要下載并導(dǎo)入JSch包:jsch-0.1.50.jar。

示例程序

package com.stormma.demo;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
 
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
 
public class Shell {
  //遠(yuǎn)程主機(jī)的ip地址
  private String ip;
  //遠(yuǎn)程主機(jī)登錄用戶名
  private String username;
  //遠(yuǎn)程主機(jī)的登錄密碼
  private String password;
  //設(shè)置ssh連接的遠(yuǎn)程端口
  public static final int DEFAULT_SSH_PORT = 22; 
  //保存輸出內(nèi)容的容器
  private ArrayList<string> stdout;
 
  /**
   * 初始化登錄信息
   * @param ip
   * @param username
   * @param password
   */
  public Shell(final String ip, final String username, final String password) {
     this.ip = ip;
     this.username = username;
     this.password = password;
     stdout = new ArrayList<string>();
  }
  /**
   * 執(zhí)行shell命令
   * @param command
   * @return
   */
  public int execute(final String command) {
    int returnCode = 0;
    JSch jsch = new JSch();
    MyUserInfo userInfo = new MyUserInfo();
 
    try {
      //創(chuàng)建session并且打開連接,因?yàn)閯?chuàng)建session之后要主動(dòng)打開連接
      Session session = jsch.getSession(username, ip, DEFAULT_SSH_PORT);
      session.setPassword(password);
      session.setUserInfo(userInfo);
      session.connect();
 
      //打開通道,設(shè)置通道類型,和執(zhí)行的命令
      Channel channel = session.openChannel("exec");
      ChannelExec channelExec = (ChannelExec)channel;
      channelExec.setCommand(command);
 
      channelExec.setInputStream(null);
      BufferedReader input = new BufferedReader(new InputStreamReader
          (channelExec.getInputStream()));
 
      channelExec.connect();
      System.out.println("The remote command is :" + command);
 
      //接收遠(yuǎn)程服務(wù)器執(zhí)行命令的結(jié)果
      String line;
      while ((line = input.readLine()) != null) { 
        stdout.add(line); 
      } 
      input.close(); 
 
      // 得到returnCode
      if (channelExec.isClosed()) { 
        returnCode = channelExec.getExitStatus(); 
      } 
 
      // 關(guān)閉通道
      channelExec.disconnect();
      //關(guān)閉session
      session.disconnect();
 
    } catch (JSchException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return returnCode;
  }
  /**
   * get stdout
   * @return
   */
  public ArrayList<string> getStandardOutput() {
    return stdout;
  }
 
  public static void main(final String [] args) { 
    Shell shell = new Shell("xxx.xxx.xxx.xxx", "username", "password");
    shell.execute("uname -s -r -v");
 
    ArrayList<string> stdout = shell.getStandardOutput();
    for (String str : stdout) { 
      System.out.println(str); 
    } 
  } 
}

MyUserInfo

package com.stormma.demo;
 
import com.jcraft.jsch.UserInfo;
 
public class MyUserInfo implements UserInfo {
 
  @Override
  public String getPassphrase() {
    // TODO Auto-generated method stub
    System.out.println("MyUserInfo.getPassphrase()");
    return null;
  }
 
  @Override
  public String getPassword() {
    // TODO Auto-generated method stub
    System.out.println("MyUserInfo.getPassword()");
    return null;
  }
 
  @Override
  public boolean promptPassphrase(String arg0) {
    // TODO Auto-generated method stub
    System.out.println("MyUserInfo.promptPassphrase()");
    System.out.println(arg0);
    return false;
  }
 
  @Override
  public boolean promptPassword(String arg0) {
    // TODO Auto-generated method stub
    System.out.println("MyUserInfo.promptPassword()"); 
    System.out.println(arg0);
    return false;
  }
 
  @Override
  public boolean promptYesNo(String arg0) {
    // TODO Auto-generated method stub'
     System.out.println("MyUserInfo.promptYesNo()"); 
     System.out.println(arg0); 
     if (arg0.contains("The authenticity of host")) { 
       return true; 
     } 
    return true;
  }
 
  @Override
  public void showMessage(String arg0) {
    // TODO Auto-generated method stub
    System.out.println("MyUserInfo.showMessage()"); 
  }
 
}

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論