簡體   English   中英

JAVA從Linux執行Powershell腳本

[英]JAVA Execute powershell script from linux

我想使用JAVA從Linux執行Powershell腳本。

Powershell腳本在Windows系統中。 我必須使用傳遞2個參數的java bu從linux系統中調用它。

可能嗎??

謝謝

得到了解決方案...

  1. 1在您的Windows(服務器)中下載FreeSSHD http://www.freesshd.com/?ctt=download 確保以管理員身份運行它。

    對於安裝FreeSSHD,請遵循以下URL http://www.techrepublic.com/blog/tr-dojo/set-up-a-free-ssh-server-on-windows-7-with-freesshd/在安裝后,您可以ssh從linux或使用膩子的Windows系統。

  2. 使用Java從Linux執行Powershell腳本到遠程Windows系統

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM