首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用j2ssh执行多个命令

使用j2ssh执行多个命令
EN

Stack Overflow用户
提问于 2012-12-06 19:13:47
回答 3查看 5.2K关注 0票数 3

我想知道如何使用j2ssh执行多个命令。我从网上得到的代码如下:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.sshtools.j2ssh.io.IOStreamConnector;
import com.sshtools.j2ssh.io.IOStreamConnectorState;
import com.sshtools.j2ssh.connection.*;

import com.sshtools.j2ssh.SshClient;

import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.session.SessionChannelClient;

import com.sshtools.j2ssh.configuration.SshConnectionProperties;

import com.sshtools.j2ssh.transport.ConsoleHostKeyVerification;

public class MySSHClient {

  SshClient ssh = null;
  SshConnectionProperties properties = null;
  SessionChannelClient session = null;

  public MySSHClient(String hostName, String userName, String passwd )
  {

    try
    {
      // Make a client connection
      ssh = new SshClient();
      properties = new SshConnectionProperties();
      properties.setHost(hostName);

      // Connect to the host
      ssh.connect(properties, new ConsoleHostKeyVerification());

      // Create a password authentication instance
      PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();

      pwd.setUsername(userName);
      pwd.setPassword(passwd);

      // Try the authentication
      int result = ssh.authenticate(pwd);

      // Evaluate the result
      if (result==AuthenticationProtocolState.COMPLETE) {

        System.out.println("Connection Authenticated");
      }
    }
    catch(Exception e)
    {
      System.out.println("Exception : " + e.getMessage());
    }

  }//end of method.


  public String execCmd(String cmd)
  {
    String theOutput = "";
    try
    {
      // The connection is authenticated we can now do some real work!
      session = ssh.openSessionChannel();

      if ( session.executeCommand(cmd) )
      {
        IOStreamConnector output = new IOStreamConnector();
        java.io.ByteArrayOutputStream bos =  new
        java.io.ByteArrayOutputStream();
        output.connect(session.getInputStream(), bos );
        session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
        theOutput = bos.toString();
      }
      //else
      //throw Exception("Failed to execute command : " + cmd);
      //System.out.println("Failed to execute command : " + cmd);
    }
    catch(Exception e)
    {
      System.out.println("Exception : " + e.getMessage());
    }

    return theOutput;
  }


}

我尝试转到一个目录,然后使用ls命令列出文件,但不起作用

代码语言:javascript
复制
MySSHClient sshc = new MySSHClient(<hostName>, <userName>, <passwd>);
System.out.println( sshc.execCmd("cd test") );
System.out.println( sshc.execCmd("ls") );

有什么需要帮忙的吗?

EN

回答 3

Stack Overflow用户

发布于 2013-04-01 22:55:54

尝试用分号分隔命令。例如:System.out.println( sshc.execCmd("cd test; ls") );

票数 0
EN

Stack Overflow用户

发布于 2013-04-01 23:18:19

在命令之间添加and运算符(&&):

代码语言:javascript
复制
System.out.println( sshc.execCmd("cd test && ls") );

&&;更好,因为如果cd失败,就不会执行ls

票数 0
EN

Stack Overflow用户

发布于 2014-08-13 17:29:54

尝试在output executeCommand - session.getOutputStream()中编写命令,并在末尾添加'\n',而不是使用流。然后读取输入流。

例如:

代码语言:javascript
复制
session.getOutputStream().write((command + "\n").getBytes());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13742427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档