【问题标题】:Executing multiple commands using j2ssh使用 j2ssh 执行多个命令
【发布时间】:2012-11-24 09:53:06
【问题描述】:

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

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 命令列出文件,但没有成功

MySSHClient sshc = new MySSHClient(<hostName>, <userName>, <passwd>);
System.out.println( sshc.execCmd("cd test") );
System.out.println( sshc.execCmd("ls") );

有什么帮助吗?

【问题讨论】:

  • 你知道它是否没有执行命令,或者没有输出结果(你执行然后打开输出流)。尝试使用“触摸”命令或类似命令在远程主机上创建文件,看看是否可行
  • @Brian Agnew 它确实执行了命令并输出了结果,但问题是它丢失了目录并因此显示了上一个目录本身的内容
  • 根据此链接javaconfessions.com/2008/09/getting-started-with-j2ssh.html 在“关于执行多个命令的说明”部分中,它说您不能执行一个命令来更改目录,然后再执行另一个命令来执行该目录中的脚本,因为当会话关闭并且新命令在默认工作目录中重新启动时,更改目录会丢失。当然,您总是可以将 cd 命令放入脚本中?或者使用 shell 来执行这两个命令。我不明白的是如何使用脚本

标签: command j2ssh


【解决方案1】:

不要使用executeCommand,而是尝试在输出流中写入您的命令 - session.getOutputStream() 最后加上'\n'。 然后读取输入流。

例如:

session.getOutputStream().write((command + "\n").getBytes());

【讨论】:

    【解决方案2】:

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

    System.out.println( sshc.execCmd("cd test && ls") );
    

    &amp;&amp; 优于 ;,因为如果 cd 失败,则不会执行 ls

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-10
        • 1970-01-01
        • 2012-08-25
        • 1970-01-01
        • 2015-08-27
        • 2013-01-08
        相关资源
        最近更新 更多