2017-07-11 7 views
0

対話型ではないpsexecに問題があります。Java psexec対話式リモートコマンドライン

package testProject; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.PrintStream; 


public class ConTest { 

    private ProcessBuilder process; 
    private Process connection; 
    private String main_connection;; 

    public ConTest(String host, String user, String password) { 
     process = new ProcessBuilder("cmd.exe"); 
     process.redirectErrorStream(true); 
     main_connection="<path to psexec>\psexec.exe \\\\" + host + 
       " -accepteula -nobanner -u " + user + " -p " + password +" cmd"; 
    } 

    public void runCommand(String command) throws Exception{ 

     /* Variable Declaration */ 
     String   readline; 
     PrintStream  output; 
     BufferedReader input; 

     /* Variable Initialization */ 
     connection = process.start(); 
     output = new PrintStream(connection.getOutputStream()); 
     input = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

     /* Running the commands on the Host */ 

     output.println(main_connection); 
     output.println(command); 
     output.println("exit"); 
     output.close(); 

     /*print the output from the command*/ 
     while ((readline = input.readLine()) != null) { 
      System.out.println(readline); 
     } 

     input.close(); 
     connection.waitFor(); 
    } 
} 

そして私は、次の

package testProject; 

public class mainClass { 

    public mainClass() { 
    } 

    public static void main(String[] args) throws Exception { 

     ConTest con = new ConTest(<IP>, <Admin>, <Password>); 
     con.runCommand("ping localhost"); 

    } 
} 

出力を使用してそれを呼んでいる:それは私のConnectionクラスであるとすぐに、それはここで

プロンプトコマンドを開くには、コマンドを実行しているように返しますホストに接続していることを示していますが、その後に接続を切断すると、ping localhostコマンド

出力は

ですピング続い
C:><path to psexec>\psexec.exe \\<IP> -accepteula -nobanner -u <Admin> -p <Password> cmd 
Microsoft Windows [Version 6.1.7601]Connecting to <IP>... 


Starting PSEXESVC service on <IP>... 


Connecting with PsExec service on <IP>... 


Starting cmd on <IP>... 



cmd exited on <IP> with error code 0. 

C:\>ping localhost 

は私がパイプダウン以上のコマンドを送信する際に、それらがリモートマシンではない、私のローカルマシン上で実行されている出力ストリームの焦点を促しコマンドを維持するにはどうすればよい

統計しますか?

答えて

0

私はpsexecの代わりにpaexecを使用していましたが、これは私にインタラクティブなセッションを提供してくれました。これは将来誰かを助けることを願っています

関連する問題