gpt4 book ai didi

JAVA 从 linux 执行 powershell 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:39 26 4
gpt4 key购买 nike

我想使用 JAVA 从 linux 执行 powershell 脚本。

Powershell脚本在Windows系统中。我必须使用传递 2 个参数的 java bu 从 linux 系统调用它。

这可能吗??

谢谢

最佳答案

得到解决方案...

  1. 第一次下载 FreeSSHD http://www.freesshd.com/?ctt=download在您的窗口(服务器)中。确保以管理员身份运行它。

    设置 FreeSSHD 跟随这个网址 http://www.techrepublic.com/blog/tr-dojo/set-up-a-free-ssh-server-on-windows-7-with-freesshd/设置后,您可以从 linux 或使用 putty ssh 那个 windows 系统。

  2. 使用java从linux到远程windows系统执行powershell脚本

package com.sysvana.router.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Test {
static String hostname = "10.1.10.60";
static String username = "administrator";
static String password = "P@ssw0rd";
public static void main(String[] args) throws IOException {
Connection conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword (username, password);
if (isAuthenticated == false){
System.out.println("authentication failed");
}
System.out.println(isAuthenticated);
Session sess = conn.openSession ();
sess.execCommand ("powershell C:/Users/Administrator/Desktop/test.ps1");
InputStream stdout = new StreamGobbler (sess.getStdout ());
BufferedReader br = new BufferedReader (new InputStreamReader (stdout));
while (true)
{
String line = br.readLine ();
if (line == null) break;
System.out.println (line);
}
System.out.println ("Exit code" + sess.getExitStatus ());
sess.close ();
conn.close ();
}
}

使用 Ganymed SSH-2 jar http://www.ganymed.ethz.ch/ssh2/

关于JAVA 从 linux 执行 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24353639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com