【问题标题】:Not able to run putty script via JAVA JSch无法通过 JAVA JSch 运行 putty 脚本
【发布时间】:2017-02-25 08:56:20
【问题描述】:

我必须将文件从一个位置移动到另一个位置并运行名为“Irel_Wrapper”的脚本

在腻子中我做了 /home/location mv 文件名。

所以在 Java 中我使用了 channel(Exec) 并且我能够执行上述场景,即将文件作为“mv”命令移动是一个 putty 命令。

但我无法运行脚本 Irel_Wrapper(我的猜测是因为它不是 putty 本机命令,我无法在 java 中使用 channel(Exec) 执行此操作。

我基本上需要用 cd 命令打开一个位置并运行 irel_wrapper 脚本。我尝试了一种方法,但我无法实现。 我得到的错误:未找到 Ksh Irel。

我的代码:

public ArrayList<String> deployIrelWrapper() throws JSchException, IOException {
    ConnectAndCreateSession();
    String GrepComandConsole = null;
    StringBuilder sb = new StringBuilder();
    Channel channel1 = session.openChannel("exec");
    String command = "cd /pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel && irel_wrapper";
    BufferedReader br = null;
    java.io.InputStream in = channel1.getInputStream();

    ((ChannelExec) channel1).setCommand(command);
    ((ChannelExec) channel1).setErrStream(System.err);
    System.out.println("Connect to Channel...");
    channel1.connect();
    System.out.println("****Channel Connected****");
    System.out.println();
    String line;
    try {
        br = new BufferedReader(new InputStreamReader(in));
        while ((line = br.readLine()) != null) {
            sb.append(line + " ");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    ArrayList<String> ResultSet = new ArrayList<>();
    ArrayList<String> ResultSetOutput = new ArrayList<>();
    System.out.println("Expected output");
    String words = sb.toString();
    String[] result = words.split(" ");
    for (String ss : result) {
        ResultSet.add(ss);
    }
    for (int i = 0; i < ResultSet.size(); i++) {
        GrepComandConsole = ResultSet.toArray()[i].toString();
        ResultSetOutput.add(ResultSet.toArray()[i].toString());
        System.out.println(ResultSetOutput);
    }
    channel1.disconnect();
    session.disconnect();

    return ResultSetOutput;
}

请不要重复此问题。 我可以执行 mv command cd.. 或 SFTP transfer.. 之类的命令,但是在使用 CD 命令打开路径后执行脚本我无法获取它。我正在使用 Jsch 库。

如何手动完成:
我登录腻子。
打开路径:cd home/apt/cdr/irel/
运行 irel_wrapper : irel_wrapper
然后将填充一些其他详细信息的列表,例如 GSM3A、Voic、NET3B 等。
下一个最终命令:irel_wrapper GSM3A

这就是我们通过 putty 手动完成的方式,我尝试使用 java 和 Jsch 将其自动化。 SO SFTP 和其他简单的命令,如 mv 和 ls 我能够实现.. 除了这个 Irel 包装器.. 但是当我手动执行它时它工作正常。因此我猜没有拼写错误..

【问题讨论】:

  • Ksh Irel not found 表示在远程系统上执行irel_wrapper 的shell 找不到这个“Irel”程序。您可能拼错了命令的名称,或者它可能实际上不存在于系统中,或者它可能不在您的命令路径中。您能否编辑您的问题以包含此 irel_wrapper 脚本的文本,并告诉我们此“Irel”程序实际存储在远程系统上的哪个位置?
  • 它工作正常!但我无法在输出之间进行迭代..我的意思是我是 cd 命令的输出,单独打印在控制台上..但是你引用的代码从登录开始打印出所有内容,方式是 putty 如何显示其输出..你能帮我一个模型代码来单独显示指挥官的输出吗??

标签: java jsch


【解决方案1】:

问题是默认情况下 ssh 连接不启动 shell。为了在 shell 中执行脚本,您需要启动一个终端。

Channel channel = session.openChannel("shell");

您将它作为“exec”启动,它只允许您运行程序,但不能运行 shell 脚本。此处讨论了有关它们之间差异的更多信息:

What is the difference between the 'shell' channel and the 'exec' channel in JSch

抱歉,您搜索了这么久。节日快乐。

【讨论】:

  • 这是不正确的。 exec 通道使服务器运行$SHELL -c 'command-requested-by-client'。 “command-requested-by-client”可以是任何有效的 shell 命令,例如 OP 请求的 cd /path &amp;&amp; run-something 表单。
  • 它工作正常!但我无法在输出之间进行迭代..我的意思是我是 cd 命令的输出,单独打印在控制台上..但是你引用的代码从登录开始打印出所有内容,方式是 putty 如何显示其输出..你能帮我一个模型代码单独显示指挥官的输出吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多