2015-01-21 31 views
0

我是AWS新手,任何人都可以幫助我如何使用Java代碼在虛擬機上創建用戶。我已經創建了windows ec2實例,並且能夠使用sshClient進行連接,但無法創建用戶。我沒有任何關於如何完成的想法。如何使用java在windows ec2上創建用戶

回答

0

[我新的堆棧溢出,請原諒我,如果誤格式化]
回答我的問題......它可以幫助別人.. 您可以使用EXECUTE命令來創建遠程Windows用戶... 實施例:

public boolean executeCommands(String commands, SSHRequestParam param) 
     throws Exception,IOException { 
    boolean response = false; 
    try { 

     Connection conn = getConnection(param); 
     Session sess = conn.openSession(); 

     sess.execCommand(commands); 
     sess.waitForCondition(ChannelCondition.EXIT_SIGNAL, 0); 

     InputStream stdout = new StreamGobbler(sess.getStdout()); 

     BufferedReader br = new BufferedReader(
       new InputStreamReader(stdout)); 
     while (true) { 
      String line = br.readLine(); 
      if (line == null) 
       break; 
      response=true; 
      System.out.println(line); 
     } 

     InputStream stderr = new StreamGobbler(sess.getStderr()); 
     BufferedReader br1 = new BufferedReader(new InputStreamReader(
       stderr)); 

     while (true) { 
      String line = br1.readLine(); 
      if (line == null) 
       break; 
      response=false; 
      System.out.println(line); 
     } 

     /* Show exit status, if available (otherwise "null") */ 

     System.out.println("ExitCode: " + sess.getExitStatus()); 

     /* Close this session */ 

     System.out.println("closing session"); 
     sess.close(); 
     closeConnection(conn); 

    } catch (IOException e) { 
     System.out.println("Exception..while executing command.: " + commands); 
     e.printStackTrace(); 
    } 
    System.out.println("Command executed : "+response); 
    return response; 
} 

public void createUser(String userName, String password,SSHRequestParam param) 
     throws Exception,IOException { 
     String command = "net user " +userName+" "+password+" /add" ; 
     if(executeCommands(command,param)) 
      System.out.println("User created successfully"); 
     else 
      System.out.println("User didn't create...."); 
} 

在上面的例子中PARAM變量存儲參數到連接到服務器,即主機名,用戶名,密碼....

相關問題