我想使用JAVA从linux执行powershell脚本。
powershell脚本位于Windows系统中。我必须使用java bu传递2个参数从linux系统调用它。
有可能??
感谢
答案 0 :(得分:0)
得到了解决方案......
首先在Windows(服务器)中下载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系统。
使用java
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/