【问题标题】:execute powershell command through remote server with java使用java通过远程服务器执行powershell命令
【发布时间】:2015-12-23 23:44:21
【问题描述】:

我创建了一个允许我执行命令的函数。
由于 setcommand 执行的第一个命令是“powershell”运行良好,我得到了 shell 的返回:
“Windows PowerShell 版权所有 (C) 2009 Microsoft Corporation。 Tous droits r‚serv‚s。"
我的问题是在使用 out.write 函数运行 powershell 后运行命令。

//The command is equal to "powershell" and run powershell correctly
private static void executeCommand(String command) {
    try {
    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);

     OutputStream out = channel.getOutputStream();
     InputStream in = channel.getInputStream();

    channel.connect();
    out.write(("mkdir C:\\test" + "\n").getBytes());
    out.flush();

    byte[] tmp = new byte[1024];
    while (channel.getExitStatus() == -1) {
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0)
                break;
            System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
            System.out.println("exit-status: " + channel.getExitStatus());
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
            System.out.println(ee);
        }
    }
    channel.disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

您有任何解决我的问题的方法或任何其他方式来完成这项工作吗?

编辑:

    Process p;

    try {

        p = new ProcessBuilder()
        .inheritIO()
        .command("powershell", "$user='root'; $pass='test'; $passwd=ConvertTo-SecureString -AsPlainText $pass -Force; $cred=New-Object System.Management.Automation.PSCredential -ArgumentList $user, $passwd; Invoke-command -ComputerName 10.64.2.35 -ScriptBlock {Get-ChildItem C:\\} -credential $cred").start();
        p.waitFor();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【问题讨论】:

    标签: java spring ssh channel execcommand


    【解决方案1】:

    为什么不使用pocessbuilder api?

    示例代码 - 相应地更新您的代码。

     Process p = new ProcessBuilder()
                .inheritIO()
                .command("invoke-command", "-remoteServername", "ServerXYZ",
                        "-filepath", "C:\\scripts\\script.ps1").start();
        p.waitFor();
    

    【讨论】:

    • 感谢您的回复 :) 选项不是 -ComputerName 吗? ServerXYZ 对应主机对吗?如果我必须进行身份验证,我是否只需将我的凭据放入 command() 函数中的命令中?
    • 请参考javadocs...命令只是参数。关键是 .inheritIO().command 调用。 public ProcessBuilder command(List commandArgs) 这里的 commandArgs 是包含程序及其参数的列表。我会说尝试使用其他带有 cmd.exe 的盒子来开始。
    • 帖子已更新。似乎可以工作,但问题是调用命令不起作用,我认为这是因为我之前必须使用 Set-item 函数。而且这个功能不起作用。
    • 是的,错误在invoke-command之后,它说连接无法完成并且trustedhost未启用
    猜你喜欢
    • 2013-11-22
    • 2021-08-08
    • 2013-05-04
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    相关资源
    最近更新 更多