使用JSch通过Java通过ssh将命令发送到远程服务器

sna*_*rio 12 ssh jsch

我正在尝试建立一个类,以便我可以ssh到远程服务器(我有IP,用户名和密码),然后发送一个命令,如"echo"test""然后接收输出(例如, "测试").我正在使用JSch来做这件事,但我不明白该怎么做.

import com.jcraft.jsch.*;

public class ConnectSSH {

public int execute (String command) {

    JSch jsch   = new JSch();
    String ip   = "00.00.00.00;
    String user = "root";
    String pass = "password";
    int port    = 22;

    try {                
        Session session = jsch.getSession(user, ip, port);   
        session.setPassword(pass);
        session.connect();

             ...
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做,连接后我卡住了.

任何意见是极大的赞赏.

Sai*_*nse 25

试试这个:

JSch jsch=new JSch();
Session session=jsch.getSession(remoteHostUserName, RemoteHostName, 22);
session.setPassword(remoteHostpassword);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();

ChannelExec channel=(ChannelExec) session.openChannel("exec");
BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.setCommand("pwd;");
channel.connect();

String msg=null;
while((msg=in.readLine())!=null){
  System.out.println(msg);
}

channel.disconnect();
session.disconnect();
Run Code Online (Sandbox Code Playgroud)


小智 9

shamnu的答案是对的.我无法添加评论,所以这里有几个例子来提高他的答案.一个是如何远程执行'ls -l',另一个'mkdir',以及另一个本地到远程拷贝.全部使用jsch的版本0.1.51(http://www.jcraft.com/jsch/)完成.

  public void remoteLs() throws JSchException, IOException {
    JSch js = new JSch();
    Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    s.setPassword("mypassword");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    s.setConfig(config);
    s.connect();

    Channel c = s.openChannel("exec");
    ChannelExec ce = (ChannelExec) c;

    ce.setCommand("ls -l");
    ce.setErrStream(System.err);

    ce.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }

    ce.disconnect();
    s.disconnect();

    System.out.println("Exit code: " + ce.getExitStatus());

  }



  public void remoteMkdir() throws JSchException, IOException {
    JSch js = new JSch();
    Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    s.setPassword("mypassword");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    s.setConfig(config);
    s.connect();

    Channel c = s.openChannel("exec");
    ChannelExec ce = (ChannelExec) c;

    ce.setCommand("mkdir remotetestdir");
    ce.setErrStream(System.err);

    ce.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }

    ce.disconnect();
    s.disconnect();

    System.out.println("Exit code: " + ce.getExitStatus());

  }

  public void remoteCopy() throws JSchException, IOException, SftpException {
    JSch js = new JSch();
    Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    s.setPassword("mypassword");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    s.setConfig(config);
    s.connect();

    Channel c = s.openChannel("sftp");
    ChannelSftp ce = (ChannelSftp) c;

    ce.connect();

    ce.put("/home/myuser/test.txt","test.txt");

    ce.disconnect();
    s.disconnect();    
  }
Run Code Online (Sandbox Code Playgroud)


Dam*_*ght 2

我建议查看 JCraft 网站上的隐藏示例:http://www.jcraft.com/jsch/examples/UserAuthKI.java

他们的示例提示输入用户名、主机名、密码,因此可以立即进行测试。我在我的网络上运行它并且能够连接到 AIX 服务器而无需更改任何代码。

请注意,他们的示例有一个问题(这可能就是它被隐藏的原因)..它永远不会关闭通道。如果您发送“退出”,服务器将断开您的连接,但通道对象保持打开状态,并且您的 Java 程序永远不会退出。我在这里提供了一个修复程序:Neverending of Reading server response using jSch