JAVA 从 linux 执行 powershell 脚本

标签 java linux windows powershell powershell-2.0

我想使用 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/

相关文章:

java - 从文件中随机选择行

php - 在 www-data (apache2, php) 下启动 shell 进程

linux - 在 Raspberry Pi 中使用 bcm2835 库作为非 root 的 PWM

windows - Grep 不区分大小写不工作

java - 小消息的套接字缓冲区大小

java - 服务器端 css 选择器

java - 寻找标准库或技术以获得 OBJECT for Java 的漂​​亮打印表示

c - 使用管道时出现错误,我的管道未声明

Windows批量复制文件从子文件夹到一个文件夹

c - sscanf 修饰符 %Fd 扫描什么类型?